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)
title('Pure Sinewave');
title('y(n), Pure Sinewave + Noise');
Rxy=xcorr(x,y); % Estimate the cross correlation
title('Cross correlation Rxy');

Here is the output

enter image description here


1 Answer 1


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.

enter image description here

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.

  • $\begingroup$ 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 $\endgroup$
    – bb0667
    Aug 31, 2022 at 12:56
  • $\begingroup$ I edited the post. Let me know if you have more questions! $\endgroup$
    – Gillespie
    Aug 31, 2022 at 13:35
  • $\begingroup$ 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? $\endgroup$
    – bb0667
    Sep 1, 2022 at 11:42
  • $\begingroup$ 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. $\endgroup$
    – Gillespie
    Sep 1, 2022 at 15:54
  • 1
    $\begingroup$ Thank you again for your reply. Opened my mind! @Gillespie $\endgroup$
    – bb0667
    Sep 2, 2022 at 6:18

Your Answer

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

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