Cross correlation (xcorr) issue

3 views (last 30 days)
Tom Piggin
Tom Piggin on 1 Dec 2011
Hi, I am having some parculiar issues with a cross correlation i am trying to perform on two data sets. They are of differing values in terms of magntiude, but follow the same pattern i.e. the second dataset is identical to the first in terms of the shape of the pattern but it has a lower magnitude. if they were normalised they would be the same. Here is a portion of my code...
abc=[Aa(1:end-20)'];
def=[Bb(1:end-20)'];
[Allan(1,:), index_AM]=xcorr(abc,def,'coeff');
Aa and Bb are the separate datasets. So the result of this correlation gives a "delay" of zero even though dataset "def" is identical to "abc". if i add a manual delay in as follows...
def=[1:2, Bb(1:end-20)'];
abc=[Aa(1:end-20)',1:2];
then the result of my cross correlation "Allan(1,:)" gives me a triangular output with the peak at -2, which corresponds with the delay. This suggests that Aa and Bb are identical even though if i plot them together on the same axis it is clear they are not as one dataset lags the other. Hopefully someone can help...
Tom

Answers (2)

Wayne King
Wayne King on 1 Dec 2011
I'm not sure I understand your confusion. You start out saying that you have two datasets that differ in magnitude but follow the same pattern, this suggest that if you cross correlate them, it would give you a high normalized cross correlation sequence value at the correct lag, 0. For example:
x =[1:5 fliplr(1:5)];
y = 5+x;
[c,lags] = xcorr(y,x,'coeff');
stem(lags,c);
Since x and y only differ in amplitude at each sequence value, I see that the cross correlation sequence peaks at zero lag, just as expected. And further the cross correlation sequence value at zero lag is very close to 1.
Now if I delay y with respect to x by two samples.
y = [zeros(1,2) x(1:end-2)];
[c,lags] = xcorr(y,x,'coeff');
stem(lags,c);
I see that the cross correlation sequence peaks at lag 2 just as expected. That is exactly what the cross correlation sequence is supposed to do. It's telling you that if you lag one sequence with respect to another what is the cross correlation as a function of that lag.
  1 Comment
Wayne King
Wayne King on 1 Dec 2011
"This suggests that Aa and Bb are identical even though if i plot them together on the same axis it is clear they are not as one dataset lags the other." You have to understand the cross correlation DOES lag one sequence with respect to the other.

Sign in to comment.


Tom Piggin
Tom Piggin on 1 Dec 2011
Hi, sorry for the confusion. They are the same pattern, but one inherently lags the other and this lag also changes as we move through the dataset. However the cross correlation appears to suggest that the two sequences are perfectly correlated with zero lag, which when you plot them is clearly not the case. when i meant plot i meant the raw data before correlation.

Community Treasure Hunt

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

Start Hunting!