adding echo to an audio file
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Walter Roberson
2019-2-15
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 个评论
Walter Roberson
2019-2-15
conv(in, [1,zeroes(1,ds-1),gain])
you might need to reverse that so gain then zeroes then 1
Walter Roberson
2019-2-15
oh wait I am not sure this will feed back the echo. Best search Answers to see how other people implement it .
Perturabo
2019-2-19
编辑:Perturabo
2019-2-19
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
2019-2-19
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
2019-2-19
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
2019-3-23
How many channels in your signal? Using zeros(ds,1) implies you are expecting only one channel.
paul mary
2019-3-24
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
2019-3-25
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
2019-3-25
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
paul mary
2019-3-25
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
2019-3-25
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
2019-3-26
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
2019-3-26
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
2019-3-26
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.
Walter Roberson
2019-3-26
I explained normalization at https://www.mathworks.com/matlabcentral/answers/451977-add-echo-to-audio-signal-in-matlab#comment_685188
Walter Roberson
2019-4-3
Hint:
A = zeros(1,15)
A(1:10) = sqrt(1:10)
B = zeros(1,15)
B(5+(1:10)) = sqrt(1:10)
Priyamvada Shankar
2019-4-3
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
2019-4-3
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
2019-4-3
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
2020-4-11
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
2020-4-13
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
2021-3-18
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time-Frequency Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)