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.







9 comments:

  1. Hi!!!
    I'm trying to test your code, I receive this error:
    A(I,J): column index out of bounds; value 2 out of
    bound 1

    How could I fix it?

    Thanks a lot,
    Giacomo

    ReplyDelete
    Replies
    1. Could you please tell me what is A(i,j)??

      Delete
    2. I don't know, I've just copy-paste your code and changed the .wav file, but appear that error.

      Delete
    3. make sure that you rename it properly as signalsExp1.wav and wav file is in the working dir.
      test these functions seperately
      [g,fs] = wavread('filename.wav'); and
      wavplay(g,44100);

      wavplay should play any .wav file in matlab. test these functions separately. It might help you to figure out the error.

      Delete
    4. can any on tell me that where is the working directory ?

      Delete
    5. working directory can be anywhere you like. make sure that you are keeping your .m file in working directory.

      Delete
    6. I have the same error: A(I,J): column index out of bounds; value 2 out of
      bound 1

      have you fixed it?

      Delete
    7. There is no variable named "A" in this code. So i am also confused about your problem. FYI i used matlab 2012a version and it's working fine.

      Delete
  2. can you explain the code sir please

    ReplyDelete