why are my cross correlation values so high?

I am trying to find the best lag for relating to vectors of data (attached in the mat files as vectors x and y). I want to do a cross correlation to actually calculate the best lag. I am using xcorr to look at the cross correlation values ( c ) for different lags. lag= -5 has the highest value but the correlations are all over 3000. This seems really high. Can anyone explain this value to me. I've read the documentation for xcorr and I'm lost.
[c,lags] = xcor(x,y)

8 Comments

What values were you expecting?
xcorr() doesn't normalize the output (unless you ask it to). A nice explanation is here:
Thank you Adam! This was helpful, especially since I had also found the corrcoef function and was confused about why the output was different. I think what I actually want is the normalized correlation coefficient. My goal is to determine the lag between my two variables (highest correlation). So using xcorr I think I should actually write: xcorr(x,y,7,'coeff') ?
"My goal is to determine the lag between my two variables (highest correlation)."
You don't need to normalize to do that. That's what xcorr(x,y) is designed to do.
For example, both versions below result in the same lag.
n = 0:15;
x = 0.84.^n;
y = circshift(x,5);
figure()
subplot(2,1,1)
[c,lags] = xcorr(x,y);
stem(lags,c)
title('Not normalized')
subplot(2,1,2)
[c,lags] = xcorr(x,y,'coeff');
stem(lags,c)
title('Normalized')
I was actually using the xcorr function and the crosscorr function. I ran them like this: (comparison plot attached)
[c,lags] = xcorr(x,y,20,'coeff');
[xcf,LAGS,bounds]= crosscorr(x,y);
The link compares xcorr to corrcoef and I understand how those relate, but why are the correlation outputs of xcorr and crosscorr different?
The answer to that is explained well in the first link I shared above. Corrcoeff subtracts the mean off of each input and normalizes each to be a unit vector. Xcorr with the 'coeff' option normalizes, but doesn't subtract off the mean. So, if you subtract the mean from the inputs and then send the results through xcorr() with coef normalization, you should get the same results. That's all demonstrated in that first link.
I was looking at xcorr versus crosscorr not xcorr versus corrceoff. The link was very helpful for understanding how to interpret xcorr and corrcoeff.
Oops! I misread. Sorry.
No problem. I really appreciated your help! I am still not sure why the correlation outputs are so different between xcorr and crosscorr.

Sign in to comment.

Answers (2)

the cyclist
the cyclist on 26 Sep 2019
Edited: the cyclist on 26 Sep 2019
The values are right in line with what I would expect. A typical value would be
(typical x value) * (typical y value) * (length of vectors)
so
5 * 8 * 78 = 3120
See the "More About" section of the documentation.
If you were expecting it to be normalized, then look at the scaleopt input.

1 Comment

Thank you! I did indeed want the normalized ('coeff') option!

Sign in to comment.

Cross correlation doesn't always have it's peak where the "lag" between two signals is, as a little thought will reveal. It shows you the sum of the multiplication of the overlapped terms as one signal slides past the other. For example if one signal has high values somewhere in one segment, but otherwise looks pretty much the same (just shifted), your peak correlation value won't be at the lag you think it should be. It might show you where the high values are, not where the bulk of the signal overlaps best.
There is another concept called normalized cross correlation you might want to look at. There is a 2-D version in the Image Processing Toolbox, normxcorr2(), and I attach an example for finding a template in a 2-D color image. I don't know if there is a 1-D version but often image processing functions will work on 1-D signals as well as 2-D signals.

Asked:

on 26 Sep 2019

Commented:

on 5 Oct 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!