1function[acv] = autocov(X)
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% autocov computes the autocovariance between two column vectors X and Y
4% with same length N
using the Fast Fourier Transform algorithm from 0 to
6% The resulting autocovariance column vector acv
is given by the formula:
7% acv(p,1) = 1/(N-p) * \sum_{i=1}^{N} (X_{i} - X_bar) * (Y_{i+p} - Y_bar)
8% where X_bar and Y_bar are the mean estimates:
9% X_bar = 1/N * \sum_{i=1}^{N} X_{i}; Y_bar = 1/N * \sum_{i=1}^{N} Y_{i}
10% It satisfies the following identities:
11% 1. variance consistency:
if acv = autocov(X,X), then acv(1,1) = var(X)
12% 2. covariance consistence:
if acv = autocov(X,Y), then acv(1,1) = cov(X,Y)
13%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16% Written by Jacques Burrus on March 29th 2012.
18%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22% Verify the input consistency
23if nargin < 1, error(
'Missing input vector.'); end
27if M < 2, error(
'X is too short.'); end
28if M~=
P || N~=Q, error(
'Input vectors do not have the same size.'); end
29if N ~= 1, error(
'X must be a column vector.'); end
31% Compute the autocovariance
32X_pad = [X - mean(X); zeros(M,1)];%%paddle
33Y_pad = [Y - mean(Y); zeros(M,1)];
38acv = ifft( conj( X_hat ) .* Y_hat );%the imaginary part
is due to
float precision errors.
39acv = real( acv(1:M-1) ) ./ (M - (1:1:M-1))
';