returns the sample XCF of
xcf = crosscorr(___)
y2 using any of the input arguments in the previous syntaxes.
crosscorr( plots on the axes specified by
of the current axes (
ax can precede any of the input
argument combinations in the previous syntaxes.
Generate 100 random deviates from a Gaussian distribution with mean 0 and variance 1.
rng(1); % For reproducibility x = randn(100,1);
Create a 4-period delayed version of
y = lagmatrix(x,4);
Compute and plot the XCF.
y(isnan(y)) = 0; % crosscorr does not accept NaNs [XCF,lags,bounds] = crosscorr(x,y); bounds
bounds = 2×1 0.2000 -0.2000
bounds displays the upper and lower confidence bounds, which are the horizontal lines in the XCF plot. As you should expect,
XCF peaks at lag 4.
Specify the AR(1) model for the first series:
where is Gaussian with mean 0 and variance 1.
MdlY1 = arima('AR',0.3,'Constant',2,'Variance',1);
Simulate data from
rng(1); T = 1000; y1 = simulate(MdlY1,T);
Simulate data for the second series by inducing correlation at lag 36.
y2 = [randn(36,1);y1(1:end-36)+randn(T-36,1)*0.1];
Plot the XCF using the default settings.
The plot does not indicate significant cross-correlation between the two series.
Plot the XCF for 60 lags on either side of lag 0.
The plot shows significant correlation at lag 36, as expected.
y1— First observed univariate time series
First observed univariate time series for which MATLAB estimates or plots the XCF, specified as a numeric vector. The last element of
y1 contains the latest observation.
y2— Second observed univariate time series
Second observed univariate time series for which MATLAB estimates or plots the XCF, specified as a numeric vector. The last element of
y2 contains the latest observation.
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
crosscorr(y1,y2,'NumLags',10,'NumSTD',2)plots the sample cross correlation between
y2for the lags
10and displays confidence bounds consisting of
NumLags— Number of lags
Number of lags in the sample XCF, specified as the comma-separated pair consisting of
'NumLags' and a positive integer.
crosscorr uses lags 0,±1,±2,…,±
NumLags to estimate the XCF.
crosscorr(y,'NumLags',10) plots the sample XCF of
y2 for lags
– 10 through
NumSTD— Number of standard errors in confidence bounds
Number of standard errors in the confidence bounds, specified as the comma-separated pair consisting of
'NumSTD' and a nonnegative scalar. The confidence bounds are 0 ±
NumSTD*, where is the estimated standard error of the sample cross-correlation between
y2 assuming the series are uncorrelated.
The default yields approximate 95% confidence bounds.
crosscorr(y,'NumSTD',1.5) plots the XCF of
y2 with confidence bounds
1.5 standard errors away from 0.
xcf— Sample XCF
Sample XCF between the univariate time series
y2, returned as a numeric vector of length
The elements of
xcf correspond to the elements of
lags. The center element is the lag 0 cross-correlation.
lags— Lag numbers used for XCF estimation
Lag numbers used for XCF estimation, returned as the numeric vector
(-NumLags):NumLags having the same orientation as
bounds— Approximate upper and lower confidence bounds
Approximate upper and lower cross-correlation confidence bounds assuming
y2 are uncorrelated, returned as a two-element numeric vector.
h— Handles to plotted graphics objects
Handles to plotted graphics objects, returned as a graphics array.
contains unique plot identifiers, which you can use to query or
modify properties of the plot.
The cross-correlation function measures the similarity between a time series and lagged versions of another time series as a function of the lag.
XCF starts with the estimation of the sample cross-covariance function. Consider the time series y1t and y2t and lags k = 0, ±1, ±2,…. For data pairs (y11,y21), (y12,y22),...,(y1T,y2T), an estimate of the lag k cross-covariance is
where and are the sample means of the series.
The sample standard deviations of the series are:
An estimate of the cross-correlation is
y2 have different lengths, then MATLAB appends enough zeros to the end of the shorter vector to make both vectors the same size.
crosscorr uses a Fourier transform to compute the XCF in the frequency domain, then converts back to the time domain using an inverse Fourier transform.
crosscorr plots the XCF when you do not request any output or when you request the fourth output.
 Box, G. E. P., G. M. Jenkins, and G. C. Reinsel. Time Series Analysis: Forecasting and Control. 3rd ed. Englewood Cliffs, NJ: Prentice Hall, 1994.