Interpreting xcorr results compared to corrcoef

조회 수: 35 (최근 30일)
JM
JM 2019년 8월 16일
편집: zmkal 2024년 1월 14일
I am having trouble understanding why I am getting different outputs when using coeff and a normalized xcorr, specifically at 0 lags.
Shouldn't 0 lags produce the same r value when using an xcorr compared to coeff since the time series is matched up in both cases?
Note - I am using "coeff" because the two sets of data are equal in length, but the ampltitudes are much different.
Below illustrates the example:
I have two sets of data (A and B) that are 1000 datapoints each.
I use corrcoef(A,B) and then I use xcorr(A,B,500,'coeff").
corrcoef outputs a single value that is rather low such as r = 0.15.
xcorr outputs r values at lags -500 to 500, and they are all higher than r = 0.15.
However my question is, shouldn't r at 0 lag be = 0.15 since the data is already lined up in time?

채택된 답변

David Goodmanson
David Goodmanson 2019년 8월 17일
Hi Jonathan,
For the input column vectors, corrcoeff subtracts the mean off of each one and normalizes each to be a unit vector. xcorr with the 'coeff' option normalizes, but doesn't subtract off the mean.
a = (1:1000)';
b = mod((a.^(4/3)),1000);
corrcoef([a b])
ans =
1.0000 0.1745
0.1745 1.0000
xcorr(a,b,0,'coeff') % zero lag only
ans =
0.7865
aa = a-mean(a);
bb = b-mean(b);
xcorr(aa,bb,0,'coeff')
ans =
0.1745 % agrees
  댓글 수: 5
David Goodmanson
David Goodmanson 2020년 5월 7일
편집: David Goodmanson 2020년 5월 7일
Hello SZ, thanks for the comment. it's good to hear that a blast from the past still might be useful.
zmkal
zmkal 2024년 1월 12일
편집: zmkal 2024년 1월 14일
Hi @David Goodmanson, still helpful but has raised a few new questions for me.
Maybe someone or you can help?
First, I calculated an autocorrelation function using xcorr:
[akf, lags] = xcorr(A-mean(A), 'coeff');
and secondly via corrcoef:
B = A;
for i = 1:length(A)-1
B = circshift(B,1);
R = corrcoef(A,B);
corr(i,1) = R(2,1);
end
Of course, the first variant also shifts the function in the negative direction and the second variant only in the positive direction in the way I implemented it. However, from what I read above, I would have expected the functions to look the same in the range of the positive shift?
But they do not ....

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by