write function that generates an echo to audio

32 ビュー (過去 30 日間)
Franziska Oberhammer
Franziska Oberhammer 2020 年 6 月 3 日
編集済み: Stephan 2020 年 6 月 4 日
I have to write a function called echo_gen that adds an echo effect to an audio recording. The function is to be called like this:
output = echo_gen(input, fs, delay, amp);
where input is a column vector with values between -1 and 1 representing a time series of digitized sound data. The input argument fs is the sampling rate. The sampling rate specifies how many samples we have in the data each second. For example, an audio CD uses 44,100 samples per second. The input argument delay represent the delay of the echo in seconds. That is, the echo should start after delay seconds have passed from the start of the audio signal. Finally, amp specifies the amplification of the echo which normally should be a value less than 1, since the echo is typically not as loud as the original signal.
The output of the function is a column vector containing the original sound with the echo superimposed. 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 got blocked on the following code:
function output=echo_gen(input, fs, delay, amp)
len=length(input);
temp_output=[1:(len+delay*fs)]';
output=temp_output;
for i = 1:len
if i<fs*delay
temp_output(i)=input(i);
else
temp_output(i)= input(i)+input(i-round(fs*delay)+1)*amp;
end
end
Max= max(temp_output);
if Max>1
for e = 1:length(temp_output)
output(e)=temp_output(e)/Max;
end
end
end
can anybody tell me why it doesnt work? would be greate

回答 (1 件)

Stephan
Stephan 2020 年 6 月 4 日
編集済み: Stephan 2020 年 6 月 4 日
Hi Franziska,
there is no need for loops:
% Load sample sound
load TestSoundEcho.mat;
% Play sample sound
sound(data, fs);
% Wait 5 seconds
pause(5)
% Call the function to add echo
y_echo = echo_gen(data, fs, 0.5, 0.75);
% Play echo sound
sound(y_echo, fs);
function output=echo_gen(input, fs, delay, amp)
% Preallocate new vector
output=zeros((numel(input)+delay*fs),1);
% Add zeros to input to get both vectors same length
input = [input; zeros(delay*fs,1)];
% No change during the delay time
output(1:delay*fs) = input(1:delay*fs);
% Add Echo to the remaining part
output(delay*fs+1:end)= input(1:end-delay*fs)*amp + input(delay*fs+1:end);
% Normalize between -1 and 1
output = rescale(output,-1,1);
end
You may want to try this with the attached .mat-file. It is a sample sound file to test the code - for me it worked pretty well. Just save this file in your working path of your project.

カテゴリ

Help Center および File ExchangeAudio I/O and Waveform Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by