## 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$