adding echo to an audio file

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 comentario

Priyamvada Shankar
Priyamvada Shankar el 25 de Mzo. de 2019
If anyone knows the correct code please do reply

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 15 de Feb. de 2019

1 voto

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 comentarios

Walter Roberson
Walter Roberson el 15 de Feb. de 2019
conv(in, [1,zeroes(1,ds-1),gain])
you might need to reverse that so gain then zeroes then 1
Walter Roberson
Walter Roberson el 15 de Feb. de 2019
oh wait I am not sure this will feed back the echo. Best search Answers to see how other people implement it .
Perturabo
Perturabo el 19 de Feb. de 2019
Editada: Perturabo el 19 de Feb. de 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
Jesus Sanchez el 19 de Feb. de 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];
Perturabo
Perturabo el 19 de Feb. de 2019
output = [in;zeros(ds,1);echo_signal];
would produce error since vectors don't have same dimensions. i mean the zeros and the other two. That's why the whole [in;zeros(length(a)-length(b),1] thing.
Walter Roberson
Walter Roberson el 23 de Mzo. de 2019
How many channels in your signal? Using zeros(ds,1) implies you are expecting only one channel.
paul mary
paul mary el 24 de Mzo. de 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
Walter Roberson el 25 de Mzo. de 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.
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
Walter Roberson el 25 de Mzo. de 2019
I do not see any round() in your code.
paul mary
paul mary el 25 de Mzo. de 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
Walter Roberson el 25 de Mzo. de 2019
It is d that needs to be rounded.
Walter Roberson
Walter Roberson el 25 de Mzo. de 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 ...
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
Walter Roberson el 26 de Mzo. de 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
Walter Roberson el 26 de Mzo. de 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
Priyamvada Shankar el 26 de Mzo. de 2019
But I didn't understand how to do normalisation here
Hint:
A = zeros(1,15)
A(1:10) = sqrt(1:10)
B = zeros(1,15)
B(5+(1:10)) = sqrt(1:10)
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
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
Walter Roberson el 3 de Abr. de 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.
Joseph Budd
Joseph Budd el 2 de Oct. de 2019
nice
Grace Matassa
Grace Matassa el 11 de Abr. de 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
Walter Roberson el 11 de Abr. de 2020
You should also pass the sampling frequency to sound()
Walter Roberson
Walter Roberson el 13 de Abr. de 2020
what error are you encountering?
Walter Roberson
Walter Roberson el 13 de Abr. de 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.

Iniciar sesión para comentar.

Romelyn Ramos
Romelyn Ramos el 18 de Mzo. de 2021

0 votos

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

Categorías

Más información sobre Signal Processing Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2018a

Preguntada:

el 15 de Feb. de 2019

Respondida:

el 18 de Mzo. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by