# Why does cross correlation need minimum number of 5000 samples for correct time delay estimation?

1.wav

2.wav

I have two audio signals where x[n] is a farend signal and x1[n] is our nearend signal.

A part of the farend signal is coming into the nearend signal with a certain delay (echo.) I want to estimate that delay (echo delay estimation) for which I've used the 'cross correlation' xcorr function of MATLAB. When I take 5000 samples of both the nearend and farend signals and apply xcorr, it gives an accurate delay estimation in terms of index which in our case was 266.

Then I decreased the number of samples from 5000 to 4500 and used the same wav files for delay estimation using xcorr and it gave a delay of 167.

Why does the xcorr function of MATLAB only work accurately at 5000 or above samples? When we decrease the number of samples below 5000, why doesn't it give us an accurate delay estimation? Is there any particular reason behind this? Both of the audio wav files used, the graphs, and the MATLAB code are shared below.

Note:

Please consider the value of X in the graph for time delay.

Correct delay estimation code:

clear all
close all

x = x(1:5000);
x1 = x1(1:5000);

[a,b] = xcorr(x,x1);
[~, index] = max(a);
delay = b(index);


Wrong delay estimation code:

clear all
close all

x = x(1:4500);
x1 = x1(1:4500);

[a,b] = xcorr(x,x1);
[~, index] = max(a);
delay = b(index);


• Welcome to SE.SP! Have you tried pre-whitening the data? Just cross-correlating for the purposes of delay (or location, as in the link) estimation is generally not going to give you the right results.
– Peter K.
Aug 25 '21 at 15:07
• Could you share the audio files?
– Royi
Aug 25 '21 at 20:05
• Audio files are already attached with the name '1.wav' and '2.wav'. Aren't they accessible to you? Aug 26 '21 at 5:55
• No, go read that link you posted carefully; you're redirecting us to our own Google drives. Aug 31 '21 at 7:21
• @Keegs I've updated the links of those wav files. Sorry for the mistake at my end. Aug 31 '21 at 8:00

I had a look at the two .wav files.

The most likely cause of the "error" is that the file 1.wav has about 70 milliseconds (~3340 samples) of noise at the beginning.

Even 2.wav has a very long period of just noise at the beginning. It is just noise for about the first 30 milliseconds (about 1440 samples.)

With less than 5000 samples, you aren't comparing the two signals. You are just comparing the noise in the signals. 5000 samples gets you (just barely) enough of the signal for the correlation to be (approximately) correct.

Xcorr works correctly for your task, but you have to give it signals to compare rather than uncorrelated noise.

• Confirmed, the wav files have significant differences aside from just delay. There are multiple spikes in the correlation vs lag time. Sep 2 '21 at 10:07

Two things that might help, though I can't be sure without those files.

You need to be finding the max of the absolute value of the cross-correlation because the phase of the correlation output in this context is irrelevant.

You may also benefit from using the 'normalized' scaleopt argument to xcorr considering that's the way MATLAB's documentation recommends estimating delays.

Edit: Your issue stems that when you are analyzing larger portions of the data, your correlation function changes because the two signals are not matching. I have a notebook to demonstrate the problem here: https://colab.research.google.com/drive/17eBFrUPt93NmgWvZCVDmkBwEsegfQAvw?usp=sharing

Go to File->"Open in playground mode" if you want to mess around with it.

• I've tested both of your suggestions and these are not working below 5000 as usual. I took the max(abs(xcorr)) values and also used 'normalized' argument to xcorr but it is still only working at 5000 samples and above. Below 5000 it is not working. I took 3500 samples and it gave a delay of '-144'. At 4500 samples, it was giving a delay of '292' whereas the correct delay was '266'. Aug 31 '21 at 9:24
• So I will say my remark about using the absolute value, even though it didn't solve your issue is still the correct approach. Sep 2 '21 at 10:08
• yes, you're right. But in our case it is not giving the desired results. Sep 2 '21 at 10:23