% LMS for prediction
% This simulation example looks at the Misadjustment
% Example 9.2.1, pg 509, Statistical DSP & Modeling, Hayes, John Wiley, 1996

sigma_v=1;
%hopt=[1.2728 -0.81]';
hopt=[.9 0]';

% the signal to be modelled is the AR process 
% x(n)= 1.2728*x(n-1) - 0.81*x(n-2) + v(n)
% where v(n) is i.i.d. Gaussian mean 0 and std deviation sigma_v


T=1e3;   % no of LMS iterations
Touter=50;  % outer loop averaging
mu=1;  % step size try .01, .1, 1
x=[ 0 0]'; % the initial values for simulating the AR process
EE=[];   % storing the errors
X=[];
for k=1:Touter  % outer loop for averaging the Mean Squared Error
    H=[];   
    h=[ 0 0]';
    h=hopt;
    E=[]; 
    trRest=1;  % initialise estimate of trace of R

    
    for i=1:T
        trRest=x'*x + 0.001; %uncomment for NLMS
        xNew=hopt'*x+randn*sigma_v; % generating x(i)
        e=xNew-h'*x;  % the error signal
        h=h+(mu/trRest)*e*x;   % LMS weight update
        x=[xNew; x(1)]; % for next iteration x(i+1) generation
        H=[H h];
        E=[E e];
        X=[X; x(1)^2 x(2)*x(1)];
    end
    EE(k,:)=E;
end


% Uncomment to plot the weight vector
figure
plot(ones(T,1)*hopt');
hold on;
plot(H','-- r');
title(['Weight vector  mu =' num2str(mu) '. h_opt shown as straight lines.'])

% Uncomment to plot the MSE
figure
plot(mean(EE(:,1:1:end).^2,1),'.')
title(['MSE for mu= ' num2str(mu)])