LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
autocov.m
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
5% N-2.
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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14
15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16% Written by Jacques Burrus on March 29th 2012.
17% Url: www.bfi.cl
18%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19
20Y=X;
21
22% Verify the input consistency
23if nargin < 1, error('Missing input vector.'); end
24
25[M N] = size(X);
26[P Q]= size(Y);
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
30
31% Compute the autocovariance
32X_pad = [X - mean(X); zeros(M,1)];%%paddle
33Y_pad = [Y - mean(Y); zeros(M,1)];
34
35X_hat = fft(X_pad);
36Y_hat = fft(Y_pad);
37
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))';