Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Saturday, June 9, 2012

Simple MATLAB code to add an echo to an audio signal

This code can be used to add an echo to any audio signal.I have used .wav format of the audio signal as it is supported by MATLAB well.Here is the code.


%code to add an echo to an audio signal by charitha saumya

function echo_constant
[g,fs] = wavread('signalsExp1.wav');

 delay=2*fs;
delayedsignal=zeros(length(g),2);
for n=1:1:length(g)
    if(n<=delay )
        delayedsignal(n,1)=0;
        delayedsignal(n,2)=0;
    end
    if(n>delay)
        delayedsignal(n,1)=0.65*g(n-delay,1);
        delayedsignal(n,2)=0.65*g(n-delay,2);
    end
end

echosignal=zeros(length(g),2);
for n=1:1:length(g)
    echosignal(n,1)=(g(n,1)+delayedsignal(n,1))*0.5;
    echosignal(n,2)=(g(n,2)+delayedsignal(n,2))*0.5;
end
wavplay(echosignal,44100);
wavwrite(echosignal,44100,16,'echo_constant.wav');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

you should copy and paste the .wav file that you want to add an echo to, to the matlab working directory and rename it as signalsExp1.wav .unless the code won't work.The generated echoed signal will be saved in the working directory as echo_constant.wav.

How this code works?

Say s(t) is the original audio signal . The code generates a delayed version of the signal by multiplying it by α (α may be a constant or a function of time.but in this code α is just a constant) and delaying it by a fixed time period . So the delayed signal will be of the form,

Finally this delayed signal is added to the original audio signal to generate the echo signal.