%% Autoregressive Process Coefficients
% Estimate the coefficients of an autoregressive process given by
%  x(n) = 0.1\,x(n-1)-0.8\,x(n-2)-0.27\,x(n-3) + w(n).

a = [1 0.1 -0.8 -0.27];

%
% Generate a realization of the process by filtering white noise of variance 0.4.

v = 0.4;
w = sqrt(v)*randn(15000,1);
x = filter(1,a,w);

%
% Estimate the correlation function. Discard the correlation values at negative lags. Use the Levinson-Durbin recursion to estimate the model coefficients. Verify that the prediction error corresponds to the variance of the input.

[r,lg] = xcorr(x,'biased');
r(lg<0) = [];

[ar,e] = levinson(r,numel(a)-1)

% Estimate the reflection coefficients for a 16th-order model. Verify that the only reflection coefficients that lie outside the 95% confidence bounds are the ones that correspond to the correct model order. See AR Order Selection with Partial Autocorrelation Sequence for more details.

[~,~,k] = levinson(r,16);
stem(k,'filled')

conf = sqrt(2)*erfinv(0.95)/sqrt(15000);
hold on
[X,Y] = ndgrid(xlim,conf*[-1 1]);
plot(X,Y,'--r')
hold off

% Copyright 2012 The MathWorks, Inc.