Solved – Cross Correlation (xcorr in MATLAB) vs Pearson Correlation (corr in MATLAB)

correlationcross correlationMATLAB

Are the values of xcorr(x,y) in MATLAB correlation values or not? I'm asking this because in MATLAB xcorr(x,y,'coeff') normalizes values. Is it normalizing covariance values to get correlations?

I'm confused if cross correlation values are necessarily between -1 and 1 like Pearson correlation values.

Also, I see from running an example that xcorr(x,y,0,'coeff') != corr(x,y). Could someone explain this?

Best Answer

The function xcorr() is used for calculating the time cross correlation of time series (Or any indexed signal).
The corr() function is all about calculating the correlation of so supposedly 2 random variables.

While the 2 coincide if used correctly and wisely pay attention that they basically normalize differently the operation (Hence you don't get what you expect).

In code:

vA = randn([10, 1]);
vB = randn([10, 1]);
xcorr(vA, vB, 0. 'coeff')
ans =

    0.3070
A.' * vB / sqrt((vA.' * vA) * (vB.' * vB))

ans =

    0.3070
corr(vA, vB)

ans =

    0.3425
vAA = vA - mean(vA);
vBB = vB - mean(vB);
xcorr(vAA, vBB, 0, 'coeff')

ans =

    0.3425
corr(vA, vB)

ans =

    0.3425

As you can see from the code above what's you missing is that corr() removes the mean of the data while xcorr() doesn't.

So corr(vA, vB) is equivalent of xcorr(vA - mean(vA), vB - mean(vB), 0, 'coeff').

Related Question