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.
Great code, works perfectly!
ReplyDeleteThe code works perfectly and it is easier to get the echo variances by adjusting the delay. Is it possible to save the echo file
ReplyDeleteregards
Write a function called echo_gen that adds an echo effect to an audio recording. The function is to be called like this:
ReplyDeleteoutput = 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.
MATLAB has several sample audio files included that you can try: splat, gong, and handel are a few examples. Try the following:
load gong % loads two variables, y and Fs
sound(y, Fs) % Outputs sound
To hear the sound you will need to use desktop MATLAB or MATLAB Online.
(Note that we are assuming mono audiofiles. You can load your own audio files using the audioread function in MATLAB. If the audio data has two columns, it is a stereo file, so use only one column of the data when testing your file.)
From coursera
DeleteCan you write code for it
ReplyDelete
ReplyDeletefunction 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
Why did you use floor function after round function?
DeleteThank u very Much
ReplyDeleteThis is such a great resource that you are providing and you give it away for free. Most of us come in touch with digital signs on a regular basis these days. Whether it's a huge outdoor digital screen or a menu board in a mall. These are some of the signage systems that we're most familiar with.
ReplyDeleteCan you help me with this? have to write a matlab function that adds echo to a wave file using a circular buffer implemented in Matlab with the help of a 2 dimensional array. The array of the circular buffer can be declared using function zeros(circularBuffSize, 2).
ReplyDeleteIn Matlab the array of an index begins at 1. The echo function should take the following parameters: input wavefile path, output wavefile path, size of the circular buffer. The index of an array should be implemented using a uint32 variable, var = uint32(1) (an uint32 variable initialized with 1). Parse the wave samples using a for loop.
I should use this pic but I don't understand how to interpret it.
https://ibb.co/RvHPk46
This comment has been removed by the author.
ReplyDeletecan u explain me the loop
ReplyDeletey = zeros(size(x));
ReplyDeletey(1:D) = x(1:D);
for i=D+1:length(x)
y(i) = x(i) + alpha*x(i-D);
end
these lines explain why we did this?
magic rofaaayyyy
DeleteGreat Blog. trap house
ReplyDeleteI just started using matlab and this code isn't working ...
ReplyDelete