0
$\begingroup$

There are 2 audio signals, X & Y.
X is of 15 second duration. The duration of Y is not given.
Y is a part of signal X (ie a small part of audio X is extracted & made into audio Y)
I have to find the starting & ending location, in seconds, in the signal X from where signal Y is derived.
I was told to NOT use built-in MATLAB functions like conv, xcorr etc.

So I wrote a MATLAB code which compares sample by sample of X & Y starting from the 1st sample.

clear;
[A1,FS1,NBITS1]=wavread('x.wav');
[A2,FS2,NBITS2]=wavread('y.wav');

for i=1:(length(A1)-length(A2)),
    if(A1(i:i+length(A2)-1) == A2)
        found = 1;
        time1 = i/FS1;
        time2 = (i+length(A2))/FS1;
        break;
    else
        found=0;
    end
end   

A1 has 120000 samples, FS1 = 8000, NBITS1 = 16
A2 has 20000 samples, FS2 = 8000, NBITS2 = 16
Therefore,
120000/8000 = 15 sec(length of audio X)
20000/8000 = 2.5 sec(length of audio Y)

time1(starting time) = 6.5001 sec
time2(ending time) = 9.0001 sec

Just to make sure the code was correct, I plotted the impulse response of X & Y using fvtool & found that the impulse response of Y matches that of X between 6.5 to 9 seconds.

Now I want to verify the result using built-in MATLAB functions, namely xcorr
So I used

A3 = xcorr(A1,A2);  
plot(A3);  

enter image description here
A3 is 239999 samples long. Then using

[max idx] = max(A3)  

Using the above index location I found the location of maximum correlation value to be at sample 172000. which gives the midpoint at 172000/8000=21.5sec(clearly wrong). However, the location of the maximum correlation value must come to 7.7501 samples. How do I obtain that from the above value?

$\endgroup$
3
  • $\begingroup$ Are you sure it's not meant to be 7.7501 seconds ? $\endgroup$
    – Paul R
    Sep 17, 2014 at 8:10
  • $\begingroup$ @PaulR its meant to be 7.7501 sec but the 2nd method gives me 172000/8000=21.5 sec which is clearly wrong. $\endgroup$ Sep 17, 2014 at 9:52
  • $\begingroup$ OK - you need to fix the typo in your question then. $\endgroup$
    – Paul R
    Sep 17, 2014 at 9:53

1 Answer 1

1
$\begingroup$

Having looked at how xcorr is defined I think I know what is happening.

There are two issues:

  • The range of xcorr is $2N-1$ (N = length of signal X). The initial values are for negative shifts and the second half is positive shifts. Therefore subtract 120000 to get the correct shift.

  • xcorr pads Y with zeros to the same length as X. The first 20000 points will be sample Y and the rest of the data zeros. Therefore the value of the maximum correlation will be the start point, not the mid point of the signal (as I originally thought). To prove this think what would happen if Y was the beginning of signal X.

If we now do this on your data we get a start point of (172000-12000)/8000=6.5seconds as expected. The 0.0001 sec comes from how you've defined the first point in time.

However, this is a pretty bad way to do it as any noise in the signal Y will cause your algorithm to fail. I suspect the aim of this task is to manually right code to calculate the correlation function in a similar way to corr does and then find the maximum.

$\endgroup$
1
  • $\begingroup$ 172000/8000=21.5sec which is not the answer as 7.7501 is the correct answer $\endgroup$ Sep 17, 2014 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.