|On this page…|
The cross-correlation sequence for two wide-sense stationary random process, x(n) and y(n) is:
where * 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 xn and yn is the deterministic cross-correlation sequence (also called the time-ambiguity function)
where we assume for this discussion that xn and yn are indexed from 0 to N-1, and from -(N-1) to N-1.
The functions xcorr and xcov estimate the cross-correlation and cross-covariance sequences of random processes. They also handle autocorrelation and autocovariance as special cases. The xcorr function evaluates the sum shown above with an efficient FFT-based algorithm, given inputs xn and yn stored in length N vectors x and y. Its operation is equivalent to convolution with one of the two subsequences reversed in time.
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.
The 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 x and y.
An estimate of a quantity is biased if its expected value is not equal to the quantity it estimates. The expected value of the output of xcorr is
xcorr provides the unbiased estimate, dividing by N-|m|, when you specify an '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 xcorr computes them using only a few data points. A possible trade-off is to simply divide by N using the 'biased' flag:
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. The syntax
divides the output by norm(x)*norm(y) so that, for autocorrelations, the sample at zero lag is 1.
For a multichannel signal, xcorr and xcov estimate 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, xcorr(S) returns a (2M-1)-by-N2 matrix with the autocorrelations and cross-correlations of the channels of S in its N2 columns. If S is a three-channel signal
S = [s1 s2 s3]
then the result of xcorr(S) is organized as
R = [Rs1s1 Rs1s2 Rs1s3 Rs2s1 Rs2s2 Rs2s3 Rs3s1 Rs3s2 Rs3s3]
Two related functions, cov and 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.