xcorr

Cross-correlation of two sequences, or autocorrelation of one.

Category: Data Import and Analysis > Descriptive Statistics

Syntax

[c, lags] = xcorr(x)

Autocorrelation of x at lags from -(N-1) to N-1.

[c, lags] = xcorr(x, y)

Cross-correlation of x and y.

Description

Cross-correlation slides y over x and sums the products of the overlapping samples at each shift. A peak at lag m means x resembles y shifted by m samples, which is how delays between two signals are estimated. The autocorrelation of a signal always peaks at lag 0, where it equals the signal energy.

If the vectors have different lengths, the shorter one is padded with zeros.

Input arguments

x, y
Numeric vectors.

Output arguments

c
Correlation values, one per lag.
lags
Lag indices, so c(k) belongs to lags(k).

Example

Autocorrelation and lags

x = [1 2 3];
[c, lags] = xcorr(x)
xcorr(x, [1 1 1])
Output
c = 1×5

     3     8    14     8     3
lags = 1×5

    -2    -1     0     1     2
ans = 1×5

     1     3     6     5     3

Notes

xcorr belongs to the Signal Processing Toolbox in MATLAB. Only the default (unscaled) form is documented here.

See also

xcov conv corrcoef cov