% AR_PREDICT.M
%
% P. Flandrin, Nov. 25, 2004
%
% visualise la prŽdiction d'un mod�le AR d'ordre
% 0, 1, 2, 3 ou 4 
%
% calls  : - AR_coeff.m

N = 500; % nombre de points du processus
p = 2; % ordre du mod�le (<= 4)
n = 1; % numŽro de la 1�re figure de placement des p™les

% ----- mod�le

A = AR_coeff(p,n);

b = randn(1,N);
x = filter(1,A,b);

figure(n+2)

clf

xp = x-filter(A,1,x) ;

h = 40 ;

xpp = x ;

for m = N/2:N/2+h
    
    if p == 1
    
        xpp(m) = -A(2)*xpp(m-1) ;
        
    elseif p == 2
    
        xpp(m) = -(A(2)*xpp(m-1) + A(3)*xpp(m-2)) ;
        
    elseif p == 3
                
        xpp(m) = -(A(2)*xpp(m-1) + A(3)*xpp(m-2) + A(4)*xpp(m-3)) ;
        
    else
                
        xpp(m) = -(A(2)*xpp(m-1) + A(3)*xpp(m-2) + A(4)*xpp(m-3) + A(5)*xpp(m-4)) ;
        
    end

end

plot(N/2:N/2+h,x(N/2:N/2+h),'-+') ;

hold on

pause

for k = N/2:N/2+h
	
	plot(N/2:k,xp(N/2:k),'-or')
    plot(N/2:k,xpp(N/2:k),'-xg')
	pause(.1)
	
end

hold off
