%*******************************************************
%  Matlab macro that illustrates ensemble averaging to 
%  determine the average behavior of the tap-weight 
%  coefficients and squared-error (learning curves)
% 
%  AUTHOR: Balu Santhanam
%  DATE  : 11/30/99
%  Prediction example: p = 2 order predictor 
%  Observation process x[n]: 
%  Desired signal : d[n] AR(2) with predictor coefficients 
%  a = [2*0.99,-0.99^2]; d[n] = x[n]
%  Observation vector : \tilde{u}[n] = [x[n-1],x[n-2]]
%  Macro generates E(w[n]) and MSE[n]
%  Usage: ntap = 2 , nave = 100
%  sigma_v = 0.05, 500 samples
%******************************************************
ntap = input('# of tap weight coefficients needed--->');
MSE = zeros(1,500);
filco1 = zeros(500,ntap);
filco = zeros(500,ntap);
warning off
nave = input('# of trials for ensemble averaging--->');
for i = 1:1:nave  
    fprintf('\n') 
    fprintf('%s\t%2d\n','Trial or experiment #',i)   
    fprintf('\n')                         
    noisy = awn(zeros(1,500),[0,0.05]);        
    d = filter(1,[1,-2*0.99,0.99*0.99],noisy);                  
    obs = shift1(d.',1,0); % d[n] = x[n]                      
    [filco1,y,MSE1] = nlms_co(obs,d,0.75,0.01,ntap);
    clear y d noisy
    MSE = MSE + MSE1/nave;
    for j = 1:1:ntap
        fprintf('\n') 
        fprintf('%s\t%2d\n','Averaging over tap #',j)
        fprintf('\n')
        filco(:,j) = filco(:,j) + filco1(:,j)/nave; 
    end
end
%*********************************************************
