% Adaptive Linear prediction using LMS and NLMS 
% Example 9.2.3, M. Hayes, Statistical Digital Signal Processing and
% Modeling, John Wiley, 1996

% the signal is generated as follows:
% u(n)=a_1u(n-1)+a_2u(n-2) + v(n), v(n) is unit variance white noise


% Why not try LMS and then NLMS with mu ranging from 0 to 2
% You will observe that NLMS is more stabe and even works for
% mu =1.9. Dont forget to run for a long time, say t=10 000

clear all
% initialise the signal
u=0;
un_1=1;
un_2=1;


a_1=.9;
a_2=0; % AR coefficients


% initialise filter, step 
h=[0 0]'-1;
mu=.0001;

trRest=1;  % initialise estimate of trace of R


t=200000; % number of LMS steps

filtHist=zeros(2,t);
eHist=zeros(1,t);
for i=1:t
    % generate signal
    un_2=un_1;
    un_1=u;
    u=a_1*un_1+a_2*un_2+randn;
    
    e=u-h'*[un_1 un_2]';
    
    % uncomment line for NLMS
    trRest=[un_1 un_2]*[un_1 un_2]' + 0.001;
    
    h=h+(mu/trRest)*e*[un_1 un_2]'; 
    
    filtHist(:,i)=h;
    eHist(i)=e;
end

figure
plot(filtHist')
hold on
plot(ones(t,1)* [a_1, a_2], 'k--')
title('h(n) and true values (--)')



