# Interpreting the cross correlation results

Signal Processing Asked by Sampath on September 7, 2020

I am new to this forum. I am just trying to understand how to calculate delay (in terms of samples) between two signals using cross-correlation function. I wrote a small Matlab code in order to understand this. But I am finding difficulty in understanding the results.

 clc;

fm = 1000; //message signal
fs = 10 * fm; //sampling frequency
ts = 1 /fs;
t = 0:ts:1-ts;

x=sin(2*pi*fm*t);
y=cos(2*pi*fm*t);

[r,s] = xcorr(x,y);

subplot(2,1,1);
plot(r);
subplot(2,1,2);
plot(s);


The results obtained using this code are as follows.

From the figure, we can observe that the peak is occurring at around 10,000 sample. So, can 10,000 samples be considered as a delay (in samples) between two signals? And does the corresponding time delay is 10000 * 1/10000 = 1 sec in this case?

Yes and no. In principle, you can use the peak of your correlation function. However, it is not at 10000. The correlation function is symmetric around 0, so your peak is actually much closer to zero than you think. This is one of the reasons why xcorr returns two paramters, one for the lags at which the function is calculated. The correct way of plotting the the correlation function would thus be a plot(s,r).

Secondly, you should try zooming in a bit. Since your time signals have 10000 samples, your acf has ~20000, making it hard to see the exact maximum. If you do zoom in you'll notice it's not exactly at zero but two samples to the right.

Why two samples? Because you have 10 samples per period and the shift between sine and cosine is a quarter period, so it's actually 2.5 samples. You notice the problem, with the sampling you've chosen it is a little hard to measure the delay precisely.

Try reducing your fm to a small value, like 10, this makes the correlation function easier to interpret. Here is an example:

In this example, the maximum occurs at sample 247, which is equivalent to 247*ts = 24.7 milliseconds.

In case you wonder about the triangular shape, this has to do with the fact that while at lag 0 (in the middle) we can "compare" the entirety of the two signals whereas for larger lags, we need to truncate both signals to shorter versions. The larger the lag, the fewer samples we have to correlate. For this reason, we typically check the ACF closer to lag zero.

Answered by Florian on September 7, 2020

## Related Questions

### Importance of Phase in FFT of an image

1  Asked on January 29, 2021 by mohit-lamba

### Extrapolation of band-limited frequency data

0  Asked on January 29, 2021 by junghak-ahn

### Quantify the difference between two audio spectrograms

2  Asked on January 28, 2021 by ictguy1

### How does an image rotation affect the magnitude of a DFT?

1  Asked on January 21, 2021 by astroturfdurf

### Fourier Transform of a Pulse Train (Frequency Comb Simulation)

1  Asked on January 20, 2021 by jbphysics

### How should a moving average handle missing data points?

5  Asked on January 20, 2021

### finding power spectral density from a vector

0  Asked on January 18, 2021 by raykh

### Can FFT convolution be faster than direct convolution for signals of large sizes?

3  Asked on January 15, 2021

### My 3×3 Weighted Median Filter doesn’t seem to be improving my image quality

2  Asked on January 15, 2021 by mr-johnny-doe

### Determine the haar wavelet function is linear or nonlinear

2  Asked on January 14, 2021 by ameer

### DCT and Convolution, [1994, Martucci]

0  Asked on January 14, 2021 by eduardo-reis

### Help in LBG Vector Quantization – Splitting Factor

0  Asked on January 13, 2021 by an6

### What is the value range of log magnitude spectrograms?

2  Asked on January 10, 2021 by tmueller

### How to compute autocorrelation with exponential IIR filter efficiently

0  Asked on January 8, 2021 by james-pinkerton

### How to reduce latency under mean filter for high noise?

3  Asked on January 8, 2021 by christopher-oezbek

### Calculating DTFT

1  Asked on January 4, 2021

### On generalizing Gabor-Heisenberg uncertainty in a noisy environment

1  Asked on January 3, 2021 by pierebean

### Is the phase difference between recorded sounds from left and right ear the same as the phase difference between their HRTF?

1  Asked on January 1, 2021 by atra-es