r = xcorr(x,y)
r = xcorr(x)
r = xcorr(___,maxlag)
r = xcorr(___,scaleopt)
[r,lags] = xcorr(___)
returns the cross-correlation
of two discrete-time sequences. Cross-correlation measures the similarity between a vector
r = xcorr(
x and shifted (lagged) copies of a vector
a function of the lag. If
y have different
lengths, the function appends zeros to the end of the shorter vector so it has the same
length as the other.
r = xcorr(___, limits the
lag range from
maxlag for either of
the previous syntaxes.
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
y. The largest spike occurs at the lag value when the elements of
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
y with unity peak, and specify a maximum lag of
n = 0:15; x = 0.84.^n; y = circshift(x,5); [c,lags] = xcorr(x,y,10,'normalized'); stem(lags,c)
x— Input array
Input array, specified as a vector, matrix, or multidimensional array. If
x is a multidimensional array, then
operates column-wise across all dimensions and returns each autocorrelation and
cross-correlation as the columns of a matrix.
Complex Number Support: Yes
y— Input array
Input array, specified as a vector.
Complex Number Support: Yes
maxlag— Maximum lag
scaleopt— Normalization option
Normalization option, specified as one of the following.
'biased' — Biased estimate of the
'unbiased' — Unbiased estimate of the
Normalizes the sequence so that the autocorrelations at zero lag equal 1:
r— Cross-correlation or autocorrelation
Cross-correlation or autocorrelation, returned as a vector or matrix.
x is an M × N matrix, then
xcorr(x) returns a (2M – 1) × N2 matrix with the autocorrelations and cross-correlations of the columns
x. If you specify
r has size (2 ×
maxlag – 1) × N2.
For example, if
S has three columns, , then the result of
R = xcorr(S) is organized as
lags— Lag indices
Lag indices, returned as a vector.
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
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
xcorr computes raw correlations with no
The output vector,
c, has elements given by
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
 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.
 Stoica, Petre, and Randolph Moses. Spectral Analysis of Signals. Upper Saddle River, NJ: Prentice Hall, 2005.
Usage notes and limitations:
Leading ones in
size(x) must be constant for every input
x is variable-size and is a row vector,
it must be 1-by-
:. It cannot be
size(x,1) = 1 at run time.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
For example, create a
gpuArray object from a signal
x and compute the normalized
t = 0:0.001:10-0.001; x = cos(2*pi*10*t) + randn(size(t)); X = gpuArray(x); [r,lags] = xcorr(X,200,'normalized'); r = gather(r);