Signal Processing Toolbox | Help Desk |
xcorr
Cross-correlation function estimate.
c = xcorr(x,y) c = xcorr(x) c = xcorr(x,y,'option') c = xcorr(x,y,maxlags) c = xcorr(x,y,maxlags,'option') [c,lags] = xcorr(x,y) [c,lags] = xcorr(x,y,maxlags) [c,lags] = xcorr(x,y,maxlags,'option')
xcorr
estimates the cross-correlation sequence of random processes. Autocorrelation is handled as a special case.
The true cross-correlation sequence isxcorr
must estimate the sequence because, in practice, access is available to only a finite segment of the infinite-length random process.
c = xcorr(x,y)
returns the cross-correlation sequence in a length 2N-1 vector, where x
and y
are length N vectors.
c = xcorr(x)
is the autocorrelation sequence for the vector x
. Where x
is an N-by-P matrix, c = xcorr(X) returns a matrix with 2N-1 rows whose P2 columns contain the cross-correlation sequences for all combinations of the columns of X
.
By default, xcorr
computes raw correlations with no normalization. For a length N vector:c
has elements given by c
(m) = cxy(m-N), m=1,...,2N-1.
The correlation function requires normalization to estimate the function properly.
c = xcorr(x,y,'option
')
specifies a scaling option, where '
option
'
is
biased
, for biased estimates of the cross-correlation function:unbiased
, for unbiased estimates of the cross-correlation function:coeff
, to normalize the sequence so the autocorrelations at zero lag are identically 1.0
none
, to use the raw, unscaled cross-correlations (default)
[c,lags] = xcorr(x,y)
where x
and y
are length N
vectors, returns the cross-correlation sequence in a length 2N-1
vector and the output lags in the vector [-N+1:N-1]
. That is, the maximum lag is N-1
.
[c,lags] = xcorr(x,y,maxlags)
where x
and y
are length N
vectors, returns the cross-correlation sequence in a length 2*maxlags+1
vector c
. lags
is a vector of the lag indices where c
was estimated, that is, [-maxlags:maxlags]
.
[c,lags] = xcorr(x,maxlags)
returns the autocorrelation sequence over the lag range [-maxlags:maxlags]
.
[c,lags] = xcorr(X,maxlags)
where x
is an M-by-P matrix, is a matrix with 2*maxlags+1
rows whose P2 columns contain the cross-correlation sequences for all combinations of the columns of X
.
[c,lags] = xcorr(x,maxlags,'option
')
and
[c,lags] = xcorr(x,y,maxlags,'option
')
specifies both a maximum number of lags and a scaling option.
In all cases, xcorr
gives an output such that the zeroth lag of the correlation vector is in the middle of the sequence, at element or row maxlags+1
or at N
.
The second output lags
is useful when plotting. For example, the estimated autocorrelation of zero-mean Gaussian white noise cww(m) can be displayed for -10 ww = randn(1000,1); [c_ww,lags] = xcorr(ww,10,'coeff'); stem(lags,c_ww)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):
x = [1,2i,3]; y = [4,5,6]; [c1,lags] = xcorr(x,y); c1 = mat2str(c1,2), lags c1 = [12+iFor the case where input argument*
4.2e-16 15-i*
8 22-i*
10 5-i*
12 6-i*
1.4e-15] lags = -2 -1 0 1 2 c2 = conj(fliplr(xcorr(y,x))); c2 = mat2str(c2,2) c2 = [12+i*
4.2e-16 15-i*
8 22-i*
10 5-i*
12 6-i*
1.4e-15]
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
randn('seed',0) X = randn(2,2); [M,P] = size(X); c = xcorr(X); c0 = zeros(P); c0(:) = c(M,:) % Extract zero-lag row c0 = 1.7500 0.3079 0.3079 0.1293You can calculate the matrix of correlation coefficients that the MATLAB function
corrcoef
generates by substituting
c = xcov(X,'coef')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.
For more information on estimating covariance and correlation functions, see [1] and [2].
There must be at least one vector input argument; otherwise, xcorr
gives the following error message:
1st arg must be a vector or matrix.The string
'
option
'
must be the last argument; otherwise, xcorr
gives the following error message:
Argument list not in correct order.If the second argument was entered as a scalar, it is taken to be
maxlag
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):
3rd arg is maxlag, 2nd arg cannot be scalar. When b is a vector, a must be a vector. Maxlag must be a scalar.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:
OPTION must be 'none' for different length vectors A and B.
Cross-covariance function estimate (equal to mean-removed cross-correlation). |