In this post I explain how to add an echo to an audio signal using Matlab. If you closely look at the below code, you can understand, what kind of a process is there. Initially the original signal x is delayed by 0.5 seconds and then multiplied by the attenuation constant alpha(0.65) to reduce the amplitude of the echo signal. Finally the delayed and attenuated signal is added back to the original signal to get the echo effect of the audio signal. You can visualize this process using the below mentioned Matlab simulink model.
Matlab code
clear all;
%% Hallelujah Chorus
[x,Fs] = audioread('Hallelujah.wav');
sound(x,Fs);
pause(10);
delay = 0.5; % 0.5s delay
alpha = 0.65; % echo strength
D = delay*Fs;
y = zeros(size(x));
y(1:D) = x(1:D);
for i=D+1:length(x)
y(i) = x(i) + alpha*x(i-D);
end
%% using filter method.
% b = [1,zeros(1,D),alpha];
% y = filter(b,1,x);
%% echoed Hallelujah Chorus
sound(y,Fs);
* You can download Hallelujah.wav from here or else you can find more .wav files from WavSource.com. Varying the value of alpha would change the echo strength of the audio signal.
Original and echoed audio signals in Audacity |
You can create the Matlab simulink model for echo generation as follows.
Matlab simulink model for echo generation |
Model parameters
- From Multimedia File
samples per audio channel - 8192
Audio output sampling mode - Frame based
Audio output data type - double
- Gain
Gain - 0.65
- Delay
Delay length - 4096
Input Processing - Columns as channels (frame based)
* If you use any other .wav file other than Hallelujah.wav, then you should change the above parameters accordingly.