xcorr versus finddelay for finding delay between signals

6.5k views Asked by At

Calculating the time delay between the two pulses illustrated here, using the function finddelay(pulse1,pulse2), I get the correct value of 8.73 nsec. However when I use xcorr(pulse1,pulse2), the result is about 11.2 nsec... What am I doing wrong here?

xcorr:

r=xcorr(pulse1,pulse2); 
[a,b]=max(r);
delay=20/4096*b;

here's the plot for the cross correlation

finddelay:

delay=finddelay(v1,v2);
t=20/4096*delay;

In the first case delay=2308 and in the second one delay=1788

2

There are 2 answers

0
Rash On BEST ANSWER

In your question you haven't mentioned sampling frequency since the finddelay and xcorr will give you the delay in number of samples.

Check this example,

Fs = 100;
L  = 1000;
t = (-L / 2 : 1 : L / 2) / Fs;
y1 = sinc(t);
y2 = sinc(t - 1);
plot(t,y1,'b'); hold on;
plot(t,y2,'m');
d1 = finddelay(y1,y2) / Fs;
[c,lags]=xcorr(y1,y2);
d2 = -(lags(c == max(c))) / Fs;  % minus is for nature of xcorr

enter image description here

Where the answer for both d1 and d2 is 1.

1
chipaudette On

As implied by the answer above, your error is with your use of xcorr. You need to ask for both the "c" values and for the "lags":

[c,lags]=xcorr(pulse1,pulse2);  %do the cross-correlation
[max_c,I]=max(x);  %find the best correlation
delay = lags(I);  %here is the delay in samples