This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

xcorr

Cross-correlation

Syntax

r = xcorr(x,y)
r = xcorr(x)
r = xcorr(___,maxlag)
r = xcorr(___,scaleopt)
[r,lags] = xcorr(___)

Description

example

r = xcorr(x,y) returns the cross-correlation of two discrete-time sequences. Cross-correlation measures the similarity between a vector x and shifted (lagged) copies of a vector y as a function of the lag. If x and y have different lengths, the function appends zeros to the end of the shorter vector so it has the same length as the other.

example

r = xcorr(x) returns the autocorrelation sequence of x. If x is a matrix, then r is a matrix whose columns contain the autocorrelation and cross-correlation sequences for all combinations of the columns of x.

example

r = xcorr(___,maxlag) limits the lag range from -maxlag to maxlag for either of the previous syntaxes.

example

r = xcorr(___,scaleopt) also specifies a normalization option for the cross-correlation or autocorrelation. Any option other than 'none' (the default) requires x and y to have the same length.

example

[r,lags] = xcorr(___) also returns the lags at which the correlations are computed.

Examples

collapse all

Create a vector x and a vector y that is equal to x shifted by 5 elements to the right. Compute and plot the estimated cross-correlation of x and y. The largest spike occurs at the lag value when the elements of x and y match exactly (-5).

n = 0:15;
x = 0.84.^n;
y = circshift(x,5);
[c,lags] = xcorr(x,y);
stem(lags,c)

Compute and plot the estimated autocorrelation of a vector x. The largest spike occurs at zero lag, when x matches itself exactly.

n = 0:15;
x = 0.84.^n;
[c,lags] = xcorr(x);
stem(lags,c)

Compute and plot the normalized cross-correlation of vectors x and y with unity peak, and specify a maximum lag of 10.

n = 0:15;
x = 0.84.^n;
y = circshift(x,5);
[c,lags] = xcorr(x,y,10,'normalized');
stem(lags,c)

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array. If x is a multidimensional array, then xcorr operates column-wise across all dimensions and returns each autocorrelation and cross-correlation as the columns of a matrix.

Data Types: single | double
Complex Number Support: Yes

Input array, specified as a vector.

Data Types: single | double
Complex Number Support: Yes

Maximum lag, specified as an integer scalar. If you specify maxlag, the returned cross-correlation sequence ranges from -maxlag to maxlag. If you do not specify maxlag, the lag range equals 2N – 1, where N is the greater of the lengths of x and y.

Data Types: single | double

Normalization option, specified as one of the following.

  • 'none' — Raw, unscaled cross-correlation. 'none' is the only valid option when x and y have different lengths.

  • 'biased' — Biased estimate of the cross-correlation:

    R^xy,biased(m)=1NR^xy(m).

  • 'unbiased' — Unbiased estimate of the cross-correlation:

    R^xy,unbiased(m)=1N|m|R^xy(m).

  • 'normalized' or 'coeff' — Normalizes the sequence so that the autocorrelations at zero lag equal 1:

    R^xy,coeff(m)=1R^xx(0)R^yy(0)R^xy(m).

Output Arguments

collapse all

Cross-correlation or autocorrelation, returned as a vector or matrix.

If x is an M × N matrix, then xcorr(x) returns a (2M – 1) × N2 matrix with the autocorrelations and cross-correlations of the columns of x. If you specify maxlag, then r has size (2 × maxlag – 1) × N2.

For example, if S has three columns, S=(x1x2x3), then the result of R = xcorr(S) is organized as

R=(Rx1x1Rx1x2Rx1x3Rx2x1Rx2x2Rx2x3Rx3x1Rx3x2Rx3x3).

Lag indices, returned as a vector.

More About

collapse all

Cross-Correlation and Autocorrelation

The result of xcorr can be interpreted as an estimate of the correlation between two random sequences or as the deterministic correlation between two deterministic signals.

The true cross-correlation sequence of two jointly stationary random processes, xn and yn, is given by

Rxy(m)=E{xn+myn*}=E{xnynm*},

where  < n < , the asterisk denotes complex conjugation, and E is the expected value operator. xcorr can only estimate the sequence because, in practice, only a finite segment of one realization of the infinite-length random process is available.

By default, xcorr computes raw correlations with no normalization:

R^xy(m)={n=0Nm1xn+myn,m0,R^yx*(m),m<0.

The output vector, c, has elements given by

c(m)=R^xy(mN),m=1,2,,2N1.

In general, the correlation function requires normalization to produce an accurate estimate. You can control the normalization of the correlation by using the input argument scaleopt.

References

[1] Buck, John R., Michael M. Daniel, and Andrew C. Singer. Computer Explorations in Signals and Systems Using MATLAB®. 2nd Edition. Upper Saddle River, NJ: Prentice Hall, 2002.

[2] Stoica, Petre, and Randolph Moses. Spectral Analysis of Signals. Upper Saddle River, NJ: Prentice Hall, 2005.

Extended Capabilities

See Also

| | |