Need help with generating an echo for an Audio signal
12 views (last 30 days)
Show older comments
Hello, I want to generate an echo for an audio signal, then output both signals (the orginal, and the delayed( and attenuated) version).
Here is my code
%Reading the main audio
......
%Definding time
N = length(y);
t = (0:N-1)/Fs;
%Definding the echo
delay = 10000
Z = zeros(delay,1);
Ynew = [Z;y(1:end-delay)];
%Plotting both functions
subplot(2,1,1);
plot(t,y);
subplot(2,1,2);
plot(t,0.1*Ynew);
Now my problem that when i run the code, the echo functino stops at the same instance as the orginal one, but what i want is i want the domain of the echo signal to be more than the original one by the delay value. ( I want the Echo function to contiune after 3).
Accepted Answer
Voss
on 7 May 2022
Edited: Voss
on 7 May 2022
Avoid cutting off the echo, i.e., use y instead of y(1:end-delay)
%Definding the echo
delay = 10000
Z = zeros(delay,1);
% Ynew = [Z;y(1:end-delay)];
Ynew = [Z; y];
And, to combine them (so you play them together at once or do anything else), you can add them together, but they need to be the same length, so append zeros to the end of original signal first:
y_combined = [y; Z] + 0.1*Ynew; % with attenuated Ynew (probably define Ynew to be attenuated in the first place instead)
For clarity, here's a complete piece of code doing those things with a random signal:
% some random signal y
y = 0.002*randn(100000,1).*exp(1.5*mod(-(1:100000).'/20000,1)).*repelem(randi(10,5,1),20000,1);
Fs = 50000;
%Defining time
N = length(y);
t = (0:N-1)/Fs;
%Defining the echo
delay = 10000;
Z = zeros(delay,1);
% Ynew = [Z;y(1:end-delay)];
Ynew = [Z; 0.1*y]; % with attenuation built-in
% combining original and echo
y_combined = [y; Z] + Ynew;
% new time vector, for the new signal length:
t_new = (0:numel(Ynew)-1)/Fs;
%Plotting all signals
subplot(3,1,1);
plot(t,y);
xlim(t_new([1 end]))
ylabel('Original')
subplot(3,1,2);
plot(t_new,Ynew);
xlim(t_new([1 end]))
ylabel('Delayed')
subplot(3,1,3);
plot(t_new,y_combined);
xlim(t_new([1 end]))
ylabel('Combined')
xlabel('Time')
6 Comments
More Answers (1)
Jonas
on 7 May 2022
Edited: Jonas
on 7 May 2022
just append zeros to the original signal, prepend the same amount to a copy of your original to generate the echo and add them up then
[originalSig; delay]+myAttenuation*[delay; originalSig]
you can add the signals up like above to hear the combined version or you concatenate both arrays horizontally to hear the original on one ear and the delayed signal on the other.
3 Comments
Jonas
on 7 May 2022
Edited: Jonas
on 7 May 2022
ok you have your original signal y, which you want to overlay with an echo of itself. to prepare the echo addition, append the wanted zeros matrix called delay
preparedOriginal=[y;delay];
the echo has a specific delay, and attenuation and we add the delay before
attenuation=0.2;
echo=attenuation*[delay;y];
to hear the combined version use
sound(preparedOriginal+echo,fs)
with fs being the sampling frequency of your original signal
to hear the original on the left ear and the echo on the right, use
sound([preparedOriginal, echo],fs)
See Also
Categories
Find more on Measurements and Spatial Audio in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!