%% Estimate Series Using Forward Predictor
% Estimate a data series using a third-order forward predictor. Compare the estimate to the original signal.
% First, create the signal data as the output of an autoregressive (AR) process driven by normalized white Gaussian noise. Use the last 4096 samples of the AR process output to avoid startup transients.

noise = randn(50000,1);
x = filter(1,[1 1/2 1/3 1/4],noise);
x = x(end-4096+1:end);

%
% Compute the predictor coefficients and the estimated signal.

a = lpc(x,3);
est_x = filter([0 -a(2:end)],1,x);

%
% Compare the predicted signal to the original signal by plotting the last 100 samples of each.

plot(1:100,x(end-100+1:end),1:100,est_x(end-100+1:end),'--')
grid
xlabel('Sample Number')
ylabel('Amplitude')
legend('Original signal','LPC estimate')

%
% Compute the prediction error and the autocorrelation sequence of the prediction error. Plot the autocorrelation. The prediction error is approximately white Gaussian noise, as expected for a third-order AR input process.

e = x-est_x;
[acs,lags] = xcorr(e,'coeff');

plot(lags,acs)
grid
xlabel('Lags')
ylabel('Normalized Autocorrelation')
ylim([-0.1 1.1])

% Copyright 2015 The MathWorks, Inc.