You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
adding echo to an audio file
8 views (last 30 days)
Show older comments
I have to create a function
output = echo_gen(input, fs, delay, gain);
Where input is a column vector, fs is sampling rate, delay is delay and gain is the gain of the echo, which is less than 1.
The output vector will be longer than the input vector if the delay is not zero (round to the nearest number of points needed to get the delay, as opposed to floor or ceil). A sound recording has values between -1 and 1, so if the echo causes some values to be outside of this range, you will need to normalize the entire vector, so that all values adhere to this requirement.
I have no idea how to approach this, as I have only ever worked with basic matrices as opposed to actual signals in MATLAB.
1 Comment
Priyamvada Shankar
on 25 Mar 2019
If anyone knows the correct code please do reply
Answers (2)
Walter Roberson
on 15 Feb 2019
multiply delay in seconds by sampling frequency in samples per second to get number of samples for delay. floor() to get integer count. Call it ds. if ds is 0 then declare an error .
now [1, zeroes(1,ds-1), gain] are coefficients for a filter you can pass the signal through .
After filtering then if max(abs(signal)) is greater than 1 you need to divide by that value to rescale.
27 Comments
Walter Roberson
on 15 Feb 2019
conv(in, [1,zeroes(1,ds-1),gain])
you might need to reverse that so gain then zeroes then 1
Walter Roberson
on 15 Feb 2019
oh wait I am not sure this will feed back the echo. Best search Answers to see how other people implement it .
Perturabo
on 19 Feb 2019
Edited: Perturabo
on 19 Feb 2019
here is what i tried
function output = echo_gen(in,fs,delay,gain)
samples = delay*fs;
ds = floor(samples);
s = zeros(length(in)+ds,1);
signal = [in;zeros(length(s)-ds,1)];
echo_signal = [zeros(length(s)-ds,1);in*gain];
output1 = signal + echo_signal;
for i = 1:length(output1)
if output1(i)>1
output1(i) = output1(i)/max(output1);
end
end
output = output1;
end
code to call the function
% Load splat which adds y and Fs to the workspace
load splat
% Call echo_gen to create the new audio data
output = echo_gen(y, Fs, 0.25, 0.6);
% Create a time axis. The time between points is 1/Fs;
dt = 1/Fs;
t = 0:dt:dt*(length(output)-1);
% Plot the new data to see visualize the echo
plot(t, output)
% sound (newY, Fs) % Uncomment in MATLAB to listen to the new sound data
but the vector size is way too off. I have been on it for so many days now and its become too frustrating. What am I doing wrong?
Here are the output and the problem function btw
Jesus Sanchez
on 19 Feb 2019
Have you tried to directly generate the echo signal? Following this form:
[input_signal ; delay ; echo signal]. Here I assumed delay is the time you want to wait until the echo is heard.
In code:
samples = delay*fs;
ds = floor(samples);
output = [in ; zeros(ds,1) ; in*gain];
another alternative, if you want to separate the echo signal:
samples = delay*fs;
ds = floor(samples);
echo_signal = in*gain;
output = [in ; zeros(ds,1) ; echo_signal];
Walter Roberson
on 23 Mar 2019
How many channels in your signal? Using zeros(ds,1) implies you are expecting only one channel.
paul mary
on 24 Mar 2019
Hello Priyamvada, I finished this task and the total mooc course. I can give you some hints.
- echoSignal = Signal + echo, so think about
for i =
output() = output() + y * amp;
end
- if the echo causes some values to be outside of this range, you will need to normalize the entire vector, That is, if there is a value greater than one, divide every element of that signal by the maximum value.
if max(output)>1
end
- round to the nearest number of points needed to get the delay, as opposed to floor or ceil. It's very important.
I hope you can solve it
Walter Roberson
on 25 Mar 2019
You have not replied to my repeated questions about the shape of the input. You have not posted any error messages. You have not indicated how you know that this is not a correct solution.
Priyamvada Shankar
on 25 Mar 2019
function output = echo_gen(x,fs,delay,gain)
d= delay*fs;
y=zeros(size(x));
y(1:d)=x(1:d);
for i=d+1:length(x)
y(i)=x(i)+gain*x(i-d);
end
for j = 1:length(y)
if max(y)>1
y(j) = y(j)/max(y);
end
output=y;
end
can you please check this code...this is the error showing here
Variable output must be of size [5 1]. It is currently of size [4 1]. Check where the variable is assigned a value.
Tested with the vector [-0.5; 0; 0.5; 0] and the following parameters: fs = 1, delay: 1.0 seconds, amp = 0.5
Assessment result: incorrectUsing splat sound file
If you get an error stating that your output vector is the wrong size, and the size differs by 1 from the expected value, you're probably very close. Check
That you are rounding to the nearest number of points required to create the delay
The echo signal begins after the delay has completed
Walter Roberson
on 25 Mar 2019
I do not see any round() in your code.
paul mary
on 25 Mar 2019
I suggest you read carefully the hints of our tutor in the discussion forum. which part use round(). test with
v = (1:9)'/10;
Fs = 2;
delay = 0.5
amp = 0.5;
out = echo_gen(v, Fs, delay, amp)
out =
0.0769
0.1923
0.3077
0.4231
0.5385
0.6538
0.7692
0.8846
1.0000
0.3462
Walter Roberson
on 25 Mar 2019
It is d that needs to be rounded.
Walter Roberson
on 25 Mar 2019
Reminder: I am almost always helping multiple people simultaneously -- when, that is, I am not off to cook or eat, or doing housework, or shopping for food, or doing household repairs, or out for medical appointments, or keeping up with the news, or taking a bit of time to myself to watch Netflix, or ...
Priyamvada Shankar
on 26 Mar 2019
I'm sorry @Walter ,i'm bothering a lot....but i worked for a whole day and my brain really gave up...so it's a request, please please help me because i don't think this code gonna work....for me it's kind of frustating now
function output = echo_gen(x,fs,delay,gain)
d= round(delay*fs);
y=zeros(size(x+1));
y(1:d)=x(1:d);
for i=d+1:length(x)
y(i)=x(i)+gain*x(i-d);
end
for j = 1:length(y)
if max(y)>1
y(j) = y(j)/max(y);
end
output=y;
output=normalize(output,'range',[-1 1]);
end
this is the ASSESSMENT i got
Variable output has an incorrect value.
Tested with the vector [-0.5; 0; 0.5; 0] and the following parameters: fs = 1, delay: 0.0 seconds, amp = 0.5
Assessment result: incorrectUsing splat sound file
Variable output must be of size [10820 1]. It is currently of size [10001 1]. Check where the variable is assigned a value.
Tested with the splat file and the following parameters: fs = 8192, delay: 0.1 seconds, amp = 0.0
Walter Roberson
on 26 Mar 2019
They want you to generate the output for (original length + delay) samples. After the original length you can treat the input as 0.
You ignored what I told you about normalization.
Walter Roberson
on 26 Mar 2019
Reminder: "Walter Roberson" is a solo operation, not a multi-person company. I assist multiple people as best I can until I burn out for the night, and then I read or play computer games, and then I sleep. If you need priority service from me, then you can get your university to hire me to assist you; my rates for 24 hour service contracts start at only $US30000 per day.
Priyamvada Shankar
on 26 Mar 2019
But I didn't understand how to do normalisation here
Walter Roberson
on 26 Mar 2019
I explained normalization at https://www.mathworks.com/matlabcentral/answers/451977-add-echo-to-audio-signal-in-matlab#comment_685188
Walter Roberson
on 3 Apr 2019
Hint:
A = zeros(1,15)
A(1:10) = sqrt(1:10)
B = zeros(1,15)
B(5+(1:10)) = sqrt(1:10)
Priyamvada Shankar
on 3 Apr 2019
function output = echo_gen(in,fs,delay,gain)
samples = round(fs*delay) ;
ds = floor(samples);
signal = zeros(length(in)+ds,1);
signal(1:length(in))=in;
echo_signal =zeros(length(in)+ds,1);
echo_signal(1+(1:length(in*gain)))=in;
output= signal + echo_signal;
p= max(abs(output));
if p>1
output=output ./ p;
else
output = output;
end
end
A few simple cases
Assessment result: incorrectUsing splat sound file
Variable output has an incorrect value.
Tested with the splat file and the following parameters: fs = 8192, delay: 0.1 seconds, amp = 0.0
Walter Roberson
on 3 Apr 2019
function output = echo_gen(in,fs,delay,gain)
samples = round(fs*delay) ;
ds = floor(samples);
signal = zeros(length(in)+ds,1);
signal(1:length(in))=in;
echo_signal =zeros(length(in)+ds,1);
echo_signal(ds+(1:length(in*gain)))=in*gain;
output= signal + echo_signal;
p= max(abs(output));
if p>1
output=output ./ p;
else
output = output;
end
end
Walter Roberson
on 3 Apr 2019
We gave you lots of hints. We should not have had to give you this code, but it was getting too frustrating watching you thrash as we described what you had to do in more and more detail. I worry that you are now just going to get as badly stuck on the next assignment.
Grace Matassa
on 11 Apr 2020
is it possible to play the delayed sound outloud using the sound function?
if yes would it be sound(output) be the command?
Walter Roberson
on 11 Apr 2020
You should also pass the sampling frequency to sound()
Walter Roberson
on 13 Apr 2020
what error are you encountering?
Walter Roberson
on 13 Apr 2020
You should not be concerned about whether the echo part exceeds +/- 1, you should be concerned about whether the output does. for example original data 0.7 echo 0.4, echo is not outside the range +/- 1 but the sum of the two is outside the range.
Romelyn Ramos
on 18 Mar 2021
function [output]= echo_gen(input,fs,delay,amp)
[r,c] = size(input);
extraEchoTime = round(delay*fs);
echoSignal = zeros(r+extraEchoTime,1);
addEchoSignal = echoSignal ;
for i=1:r
echoSignal(extraEchoTime+i,1) =input(i,1)*amp ;
addEchoSignal(i) = input(i);
end
addEchoSignal = addEchoSignal + echoSignal ;
range = abs(addEchoSignal) ;
maxrange = max(range);
if maxrange>1
addEchoSignal = addEchoSignal/maxrange;
end
output = addEchoSignal ;
end
See Also
Categories
Find more on Time-Frequency Analysis 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)