function rlsExample

% Adaptive Linear prediction using RLS
% 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



clear all

n=200000; % length of input signal

% initialise the signal
u=zeros(1,n);
un_0=0;
un_1=1;
un_2=1;


a_1=.9;
a_2=0; % AR coefficients
for i=1:n
    % generate input signal
    un_2=un_1;
    un_1=un_0;
    un_0=a_1*un_1 + a_2*un_2+randn;
    u(i)=un_0;
end

lambda=0.9999; % rls forgetting factor

filtHist=rls(u(1:end-1),u(2:end),2,lambda);  % note reference signal is input signal one step ahead

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




function [H] = rls(u,d,M,lambda) 

k=length(u); % number of iterations
H=zeros(M,k); % each col will store the filter coeffiecients


h=0*[1:M]'-1; % initialise filter
I=eye(M); % capital i  is the identity matrix
uvec=0*[1:M]'; % initialise vector of filter inputs
S=I/1000; % initialise inv(R) - see lecture notes

for n=1:k
    uvec=[u(n); uvec(1:M-1) ];
    a=d(n)-uvec'*h;
    g=S*uvec/( lambda + uvec'*S*uvec );
    S=(I  - g*uvec')*S / lambda;
    h= h + g*a;
    H(:,n)=h;
end
S









