MATLAB: Interpreting xcorr results compared to corrcoef

coeffcorrelationlagsxcorr

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?

Best Answer

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