Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between corrcoef and xcorr

748 views
Skip to first unread message

silvia_benetti@web.de B.

unread,
Sep 2, 2009, 8:05:21 AM9/2/09
to
Hallo,
I am analysing data series for my master thesis.
I need to calculate the autocorrelation coefficient for a time series, but I get different results using xcorr and corrcoef. Can someone tell me that difference between these two algorithms? I read old threads on this topic, but didn't understand much.

Thanks in advance,

Silvia

Evan Ruzanski

unread,
Nov 24, 2009, 8:13:18 PM11/24/09
to
Hi Silvia,

'corrcoef' subtracts the mean value of each vector considered in the cross-correlation computation and 'xcorr' does not. Using the 'coeff' option with xcorr only scales the correlation values computed using xcorr.m but doesn't subtract the mean value. In statistical terms, corrcoef detrends the data before performing the correlation computation.

As you probably have already seen, there can be huge difference between the output these 2 approaches. If you subtract the mean from each vector before using xcorr, you will get the same (zero-lag) result as corrcoef:

>> test1

test1 =

1 2 4

>> test2

test2 =

9 1 1

>> xc1 = xcorr(test1,test2,'coeff')

xc1 =

0.0240 0.0719 0.3593 0.5270 0.8623

>> test3 = test1 - mean(test1)

test3 =

-1.3333 -0.3333 1.6667

>> test4 = test2 - mean(test2)

test4 =

5.3333 -2.6667 -2.6667

>> xc2 = xcorr(test3,test4,'coeff')

xc2 =

0.2520 0.3150 -0.7559 -0.4410 0.6299

>> xc3 = corrcoef(test1,test2)

xc3 =

1.0000 -0.7559
-0.7559 1.0000

>>

Hope this helps, good luck on your thesis...
Evan

"silvia_...@web.de B." <silvia_...@web.de> wrote in message <h7ln21$pnk$1...@fred.mathworks.com>...

Evan Ruzanski

unread,
Nov 24, 2009, 9:05:23 PM11/24/09
to
Hello again,

Just wanted to add to my last post that:

xcov(test1,test2,'coeff')

also gives the same result as:

xcorr(test1 - mean(test1),test2 - mean(test2),'coeff').

ER

"silvia_...@web.de B." <silvia_...@web.de> wrote in message <h7ln21$pnk$1...@fred.mathworks.com>...

0 new messages