xcov and xcorr in Matlab

Matlab provides xcorr to calculate the cross/auto-correlation of two/one random variables.

[r,lags]=xcorr(x,y,’coeff’)

will calculate the cross-correlation of x and y using
R_m(x,y)=\Sigma_{n=0}^{N-m-1}x_{n+m}*y_n/\sqrt{\Sigma_{i=0}^{N-1}x_i^2*\Sigma_{i=0}^{N-1}y_i^2}

Here “coeff” optional keyword is used to normalize the result with L2-norm.

However
[c,lags]=xcov(x,y,’coeff’)

calculates the cross-variance of x and y using
C_m(x,y)=E[(x-E[x])_{m}*(y-E[y])]/\sqrt{Var[x]*Var[y]}=\frac{N-1}{N}\Sigma_{n=0}^{N-m-1}(x_{n+m}-\mu_x)*(y_n-\mu_y)/\sqrt{\Sigma_{i=0}^{N-1}(x_i-\mu_x)^2*\Sigma_{i=0}^{N-1}(y_i-\mu_y)^2}

Here “coeff” keyword will normalize the result with the variance of X and Y. The factor \frac{N-1}{N} comes from the unbiased variance

\sigma_x^2=\frac{1}{N-1}\Sigma_0^{N-1}(x_i-\mu_x)^2
\mu_x=\frac{1}{N}\Sigma_0^{n-1}x_i

Advertisements

About cgao

Cambridge, MA
This entry was posted in Academic. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s