# How to interpret cross correlation output?

The output of cross corelation shows the similarity between the two signals. But in what respect is there a similarity? I didn't quite understand how I should interpret the output. How should I interpret the output? I'm really trying to make sense of the underlying logic and output here rather than the math statement. I am also sharing example MATLAB code.

N=1024; % Number of samples to generate
f1=1; % Frequency of the sinewave
FS=200; % Sampling frequency
n=0:N-1; % Sampling index
x=sin(2*pi*f1*n/FS); % Generate x(n)
y=x+randn(1,N); % Generate y(n)
subplot(3,1,1);
plot(x);
title('Pure Sinewave');
grid;
subplot(3,1,2);
plot(y);
title('y(n), Pure Sinewave + Noise');
grid;
Rxy=xcorr(x,y); % Estimate the cross correlation
subplot(3,1,3);
plot(Rxy);
title('Cross correlation Rxy');
grid;


Here is the output

Cross correlation computes the "correlation" (a measure of similarity) between two signals at different offsets (called lags) from each other. You can think of one signal being slid along the other and being multiplied and summed with it at each lag.

In your case you have a signal $$x$$ that you believe exists with in another signal $$y$$. The cross correlation between them will be maximum at the time delay (lag) where $$x$$ lines up with itself in $$y$$. In your case this appears to happen a little after the 1000th lag (which corresponds to a delay of zero). This allows you to find where $$x$$ exists within $$y$$. This is the principle behind matched filtering.

This is easier to see if you make use of the lags output from MATLAB's xcorr: [Rxy, lags] = xcorr(x, y); figure; plot(lags, Rxy). Also, MATLAB's default cross correlation calculation does not normalize for the magnitudes of the signals. You can make it normalize to between -1 and 1 by using the "coeff" parameter: [Rxy, lags] = xcorr(x, y, 'coeff'). This is equivalent to xcorr(x, y)./sqrt(sum(x.^2)*sum(y.^2)). See this link for more.

Now, you see that the maximum similarity between the signals is a little more than 0.5 on a scale of -1 to 1. This is because a lot of the energy in $$y$$ is noise, and $$x$$ and $$y$$ don't have the same energy. If the signals were identical with the same energy ($$y = x$$), the normalized correlation would be 1. If $$y = -x$$, the normalized correlation would be -1.

Now the reason your cross correlation looks cyclical is because $$x$$ is periodic, so at different lags it partially correlates with $$y$$. When they are perfectly aligned on top of each other the correlation is maximum. Then as $$x$$ begins to slide past $$y$$, the correlation decreases.

• You are right in what you wrote, but how can I understand the similarity from the signal in the graph. Could you please interpret the cross-correlated graph by explaining how the similarity was examined in depth? @Gillipse Aug 31, 2022 at 12:56
• I edited the post. Let me know if you have more questions! Aug 31, 2022 at 13:35
• Great explanation! Also thanks for adding the code to the explanation. Finally, does cross-correlation just say a ratio for similarity? Or can we plot the signal that looks like both signals here? Sep 1, 2022 at 11:42
• Glad it helped. Yes, cross-correlation basically just gives a measure of similarity at different delays. It doesn't calculate any kind of basis function. Sep 1, 2022 at 15:54
• Thank you again for your reply. Opened my mind! @Gillespie Sep 2, 2022 at 6:18