## 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 <- pracma::circshift(x, 5)
rl <- xcorr(x, y)
plot(rl$lag, rl$R, type="h")
## 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
rl <- xcorr(x)
plot(rl$lag, rl$R, type="h")
## 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 <- pracma::circshift(x, 5)
rl <- xcorr(x, y, 10, 'coeff')
plot(rl$lag, rl$R, type="h")
Run the code above in your browser using DataLab