Adding echo to audio sample

I have recorded my own voice in Matlab and I intend to add some echo to it.I came up with one solution for getting the desired echo effect:

• Delay the sampled audio in the time domain and adding it to the original sample.

In order to do this I'm using Matlab and I have basically done the following:

recObj = audiorecorder(44100, 16, 2);%sampling rate of 44100Hz, stereo
recordblocking(recObj,length);%record audio for a fixed length duration
y = time_delay(getaudiodata(recObj) , 5000 );%set a delay of 5000 to original sampled audio data

function [ y ]  = time_delay ( x , R )
%this function sets a delay to x of R
y = zeros(length(x) + R , 1);
y(R + 1:length(y)) = x(1:length(x));


As you can see I have a recObj wich I can use for playing the recorded audio.In order to get a delayed version of the recorded audio I have used getaudiodata(recObj) and my own function time_delay.A problem that I have is that I don't know how to get a new delayed audiorecorder object from the delayed audio data retrieved by the time_delay function and I need an audiorecord object cause this is the only way of playing something. So, my question is:

• Is this a good approach to get the desired echo effect in an audio signal?
• If it is, then how do I set the delayed audio data to an
audiorecorder object?
• Get the data from recording object. Create sum of original and delayed version simply as a Matlab vector. Use an audioplayer object to play it. – Hilmar Mar 2 '13 at 13:22

Here is the code for adding the two signals(the delayed and not-delayed):

    x = getaudiodata(recObj); n1 = 1:size(x,1);%audiodata of original signal
y = time_delay(x , 50000 ); n2 = 1:size(y,1);%audiodata of delayed signal
mixed = sigadd(x,y,n1,n2); %audiodata of mixed signal
mixrecObj = audioplayer(mixed,44100);
play(mixrecObj);


This is the sigadd function:

function [ y,n ] = sigadd( x1,x2,n1,n2 )
%implements y[n] = x1[n] + x2[n]
%y = sum sequence over n wich includes n1 and n2
%x1 = first sequence over n1
%x2 = second sequence over n2
%

n = min(min(n1),min(n2)):max(max(n1),max(n2)); %duration of y(n)
y1 = zeros(1,length(n)); y2 = y1;
y1(find((n >= min(n1))&(n <= max(n1)) == 1)) = x1;
y2(find((n >= min(n2))&(n <= max(n2)) == 1)) = x2;
y = y1 + y2;
end


This works perfectly, but is not enough for getting a real echo effect. In order to archieve this we have to create more delayed signals(with diferent delays of course), and then add them all,like this:

x = getaudiodata(recObj); n1 = 1:size(x,1);
y = tim_delay(x , 5000 ); n2 = 1:size(y,1);
s = time_delay(x , 4000 ); n3 = 1:size(s,1);
d = time_delay(x , 3000 ); n4 = 1:size(d,1);
mixed1 = sigadd(s,d,n3,n4);n5 = 1:size(mixed1,2);
mixed2 = sigadd(x,y,n1,n2);n6 = 1:size(mixed2,2);
mixrecObj = audioplayer(totmixed,44100);
play(mixrecObj);


I have tested this one with the three delays plus the original and you can hear a pretty nice echo.

Note: In the third piece of code it would have be better if there were a function that returned the signal with the echo by passing an echo effect index as a parameter to it, but I left it that way for the sake of simplicity.

To record, it may be easier to use a simple recording program to get a. wav file, and then import that in to Matlab.

If Mathworks didn't remove the sound function, I would use that. It should just take a vector and a sampling rate to play a sound.

For implementing the delay, I would just make an h[n] that contains the impulse response of the echo operation and then filter your signal by that.

• First of all wav methods such as wavread,wavrecord,etc are getting obsolete,instead the documentation recommends you to use audiorecord. Anyway it would be good if you put an example – Daniel Conde Marin Mar 2 '13 at 16:31
• My cynical reaction to wavread/wavrecord/etc becoming "obsolete" is that Mathworks would like your code to be very specific to Matlab and not easily ported to something like Octave or Scipy. That said, I can't imagine Matlab ever being unable to read a wav file and create a vector/matrix. I'll try to add some examples soon. – Dave C Mar 4 '13 at 4:15