I am newbie here, so hi everyone, and sorry for my level of used english, I hope everything will be clear :-)
I am doing a statistical project where two (and more) time series are compared. A friend of mine advised me to use autocorrelation function, so I did so, but there a problem appeard. If I plot the following figures
>> [c,lags] = xcorr(data,'coeff');
>> stem(lags(find(0==lags):end),c(find(0==lags):end));
http://www.sdilej.eu/pics/f3d123d0d7cda0a2c0af242c74979aff.jpg
>> figure; autocorr(data,length(data)-1);
http://www.sdilej.eu/pics/2972dad0604d5e15e1de66737a1fac39.jpg
>> [c,lags] = xcov(data,'coeff');
>> figure; stem(lags(find(0==lags):end),c(find(0==lags):end));
http://www.sdilej.eu/pics/4596b60fc2a31611118eeab7831e878a.jpg
then first and second figures should be the same, since both functions xcorr and autocorr returns autocorrelation sequence of 'data'. However they are not, and even second and third are the same, where xcov returns autocovariance of 'data'. So why this is so? Bug?
note: 'find(0==lags' is used because I do not need symmetric figures, only plots for nonnegative lags. And figures are plotted through the maximal possible number of lags.
Any help would be appreciated. Thanks. Jan
Hi Jan, I can't seem to view your figures, but I suspect the issue is one that is documented (not a bug). xcorr() does not subtract the mean from the input, while both autocorr() and xcov() do. That is most likely why you see agreement between autocorr() and xcov() with the 'coeff' option and see some disagreement with xcorr() with the 'coeff' option, presuming that you are using an input with nonzero mean. Try this:
x = randn(1000,1);
y = filter([1 -1 1],1,x);
y=y-mean(y);
[c,l] = xcorr(y,50,'coeff');
[C,L] = xcov(y,50,'coeff');
[acf,lags,bounds] = autocorr(y,50);
subplot(311);
stem(l(51:end),c(51:end)); title('xcorr');
subplot(312)
stem(L(51:end),C(51:end)); title('xcov');
subplot(313)
stem(lags,acf); title('autocorr')
Hope that helps,
Wayne
Thank you Wayne,
yes, you are absolutely correct! it is documented: "The function xcov subtracts the mean and then calls xcorr". Now, xcorr gives expected figure after subtracting the mean from the 'data'.
However, now I do not see the difference between xcov and xcorr, since both functions compute cross-correlation (resp. autocorrelation), but thanks again :)
Hi Jan, if the sequence(s) are zero mean, then xcov and xcorr are equivalent.
Wayne