MATLAB's crosscorr function and R Studio ccf show different results
6 views (last 30 days)
Show older comments
Hi, I'm using MATLAB's crosscorr function and R Studio's ccf. For the same data, the results differ. It appears that the lag axis is flipped in one of them.
I've reproduced the crosscorr documentation example in both and this is what I see. The data is in the attached .txt file.
vs R Studio
Why is this happening? Thanks.
2 Comments
Bjorn Gustavsson
on 14 Dec 2020
They "simply" seems to be flipped. That most likely is because they define the lags in opposite direction. If that's the case you should find the information somewhere in the documentation.
Answers (1)
Bjorn Gustavsson
on 15 Dec 2020
If you cannot find the information in the documentation, you'll have to go to the source. This is what I find in the xcorr function:
X = fft(x,m2,1);
Y = fft(y,m2,1);
if isreal(x) && isreal(y)
c1 = ifft(X.*conj(Y),[],1,'symmetric');
else
c1 = ifft(X.*conj(Y),[],1);
end
% Keep only the lags we want and move negative lags before positive
% lags.
c = [c1(m2 - mxl + (1:mxl)); c1(1:mxl+1)];
How that differs from the R-implementation I cannot tell. (But without any thinking) This looks like an fft-based convolution between x and y. Exactly which way around to go here requires attention to detail, but you can compare this with the R-version, and check which is what with the wikipedia-description: Cross-correlation.
HTH
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!