The cross-correlation sequence for two wide-sense stationary random process, x(n) and y(n) is
where the asterisk denotes the complex conjugate and the expectation is over the ensemble of realizations that constitute the random processes.
Note that cross-correlation is not commutative, but a Hermitian (conjugate) symmetry property holds such that:
The cross-covariance between x(n) and y(n) is:
For zero-mean wide-sense stationary random processes, the cross-correlation and cross-covariance are equivalent.
In practice, you must estimate these sequences, because it is possible to access only a finite segment of the infinite-length random processes. Further, it is often necessary to estimate ensemble moments based on time averages because only a single realization of the random processes are available. A common estimate based on N samples of x(n) and y(n) is the deterministic cross-correlation sequence (also called the time-ambiguity function)
where we assume for this discussion that x(n) and y(n) are indexed from 0 to N – 1, and from –(N – 1) to N – 1.
xcov estimate the cross-correlation and
cross-covariance sequences of random processes. They also handle autocorrelation
and autocovariance as special cases. The
evaluates the sum shown above with an efficient FFT-based algorithm,
given inputs x(n) and y(n) stored
in length N vectors
y. Its operation is equivalent to convolution with one of the two subsequences reversed
x = [1 1 1 1 1]'; y = x; xyc = xcorr(x,y)
Notice that the resulting sequence length is one less than twice the length of the input sequence. Thus, the Nth element is the correlation at lag 0. Also notice the triangular pulse of the output that results when convolving two square pulses.
xcov function estimates autocovariance
and cross-covariance sequences. This function has the same options
and evaluates the same sum as
xcorr, but first
removes the means of
An estimate of a quantity is biased if its expected value is
not equal to the quantity it estimates. The expected value of the
xcorr provides the unbiased estimate, dividing
by N – |m| when you specify
'unbiased' flag after the input sequences.
Although this estimate is unbiased, the end points (near
–(N – 1)
and N – 1)
suffer from large variance because
them using only a few data points. A possible trade-off is to simply
divide by N using the
With this scheme, only the sample of the correlation at zero lag (the Nth output element) is unbiased. This estimate is often more desirable than the unbiased one because it avoids random large variations at the end points of the correlation sequence.
xcorr provides one other normalization scheme.
divides the output by
that, for autocorrelations, the sample at zero lag is 1.
For a multichannel signal,
the autocorrelation and cross-correlation and covariance sequences
for all of the channels at once. If
S is an M-by-N signal
matrix representing N channels in its columns,
a (2M – 1)-by-N2 matrix
with the autocorrelations and cross-correlations of the channels of
its N2 columns. If
a three-channel signal
S = [s1 s2 s3]
then the result of
xcorr(S) is organized
R = [Rs1s1 Rs1s2 Rs1s3 Rs2s1 Rs2s2 Rs2s3 Rs3s1 Rs3s2 Rs3s3]
Two related functions,
corrcoef, are available in the standard MATLAB® environment.
They estimate covariance and normalized covariance respectively between
the different channels at lag 0 and arrange them in a square matrix.