Signal Processing Toolbox
  Go to function:
    Search    Help Desk 
xcorr    Examples   See Also

Cross-correlation function estimate.

Syntax

Description

xcorr estimates the cross-correlation sequence of a random process. Autocorrelation is handled as a special case.

The true cross-correlation sequence is

where xn and yn are stationary random processes, , and E {} is the expected value operator. xcorr must estimate the sequence because, in practice, only a finite segment of the infinite-length random process is available.

c = xcorr(x,y) returns the cross-correlation sequence in a length 2N-1 vector, where x and y are length N vectors (N>1). If x and y are not the same length, the shorter vector is zero-padded to the length of the longer vector.

By default, xcorr computes raw correlations with no normalization:

The output vector c has elements given by c(m) = cxy(m-N), m=1, ..., 2N-1.

In general, the correlation function requires normalization to produce an accurate estimate (see below).

c = xcorr(x) is the autocorrelation sequence for the vector x. If x is an N-by-P matrix, c is a matrix with 2N-1 rows whose P2 columns contain the cross-correlation sequences for all combinations of the columns of x.

c = xcorr(x,y,'option') specifies a normalization option for the cross-correlation, where 'option' is:

See reference [1] for more information on the properties of biased and unbiased correlation estimates.

c = xcorr(x,'option') specifies one of the above normalization options for the autocorrelation.

c = xcorr(x,y,maxlags) returns the cross-correlation sequence over the lag range [-maxlags:maxlags]. Output c has length 2*maxlags+1.

c = xcorr(x,maxlags) returns the autocorrelation sequence over the lag range [-maxlags:maxlags]. Output c has length 2*maxlags+1. If x is an N-by-P matrix, c is a matrix with 2*maxlags+1 rows whose P2 columns contain the autocorrelation sequences for all combinations of the columns of x.

c = xcorr(x,y,maxlags,'option') specifies both a maximum number of lags and a scaling option for the cross-correlation.

c = xcorr(x,maxlags,'option') specifies both a maximum number of lags and a scaling option for the autocorrelation.

[c,lags] = xcorr(...) returns a vector of the lag indices at which c was estimated, with the range [-maxlags:maxlags]. When maxlags is not specified, the range of lags is [-N+1:N-1].

In all cases, the cross-correlation or autocorrelation computed by xcorr has the 0-th lag in the middle of the sequence, at element or row maxlags+1 (element or row N if maxlags is not specified).

Examples

The second output, lags, is useful for plotting the cross-correlation or autocorrelation. For example, the estimated autocorrelation of zero-mean Gaussian white noise cww(m) can be displayed for -10 m 10 using

Swapping the x and y input arguments reverses (and conjugates) the output correlation sequence. For row vectors, the resulting sequences are reversed left to right; for column vectors, up and down. The following example illustrates this property (mat2str is used for a compact display of complex numbers).

For the case where input argument x is a matrix, the output columns are arranged so that extracting a row and rearranging it into a square array produces the cross-correlation matrix corresponding to the lag of the chosen row. For example, the cross-correlation at zero lag can be retrieved by

You can calculate the matrix of correlation coefficients that the MATLAB function corrcoef generates by substituting

in the last example. The function xcov subtracts the mean and then calls xcorr.

Use fftshift to move the second half of the sequence starting at the zeroth lag to the front of the sequence. fftshift swaps the first and second halves of a sequence.

Algorithm

For more information on estimating covariance and correlation functions, see [1] and [2].

Diagnostics

There must be at least one vector input argument; otherwise, xcorr gives the following error message:

The string 'option' must be the last argument; otherwise, xcorr gives the following error message:

If the second argument was entered as a scalar, it is taken to be maxlags and no succeeding input can be a scalar. When the second argument is a vector, the first must also be a signal vector. The third argument, when present, must be a scalar or a string. If they are not, xcorr gives the appropriate error message(s):

Normally the lengths of the vector inputs should be the same; if they are not, then the only allowable scaling option is 'none'. If it is not, xcorr gives the following error message:

See Also

conv
Convolution and polynomial multiplication.
corrcoef
Correlation coefficient matrix.
cov
Covariance matrix.
xcorr2
Two-dimensional cross-correlation.
xcov
Cross-covariance function estimate (equal to mean-removed cross-correlation).

References

[1] Bendat, J.S., and A.G. Piersol. Random Data: Analysis and Measurement Procedures. New York: John Wiley & Sons, 1971. Pg. 332.

[2] Oppenheim, A.V., and R.W. Schafer. Digital Signal Processing. Englewood Cliffs, NJ: Prentice Hall, 1975. Pgs. 63-67, 746-747, 839-842.



[ Previous | Help Desk | Next ]