% System identification with the LMS algorithm
%		I			-	number of repetitions
%		K			-	iterations
%		sigmax      -	standard deviation of the input signal
%		Wo      	-	plant/system to be identified
%		sigman      -	standard deviation of the noise
% 		mu			-	step size
%		M			-	misadjustment
%		ind         -	sample index
Author: Rodrigo C. de Lamare, 1999

clear all	
I = 1000;    
K = 500; 
sigmax = 1; % SNR = sigmax.^2/sigman.^2;
Wo = [1 0.8 0.5 0.4 0.3 0.1]';
sigman = 0.01; 
mu = 0.05;  
% length of the plant/system
L=length(Wo);	
% order of the plant/system
N=L-1;			
% MSE vector 
MSE=zeros(K,1);		
% Minimum MSE vector
MSEmin=zeros(K,1);
for i=1:I,		
   X=zeros(L,1);    	
   W=zeros(L,1);	
   % input
   x=randn(K,1)*sigmax;		
   % noise
   n=randn(K,1)*sigman;		 
   for k=1:K,		
      X=[x(k)		
         X(1:N)];
      % desired signal 
      d=Wo'*X;	
      % output estimate 
      y=W'*X;	
      % error signal
      e=d+n(k)-y;	      
      % new/updated filter
      W=W+mu*conj(e)*X;	
      % accummulation of MSE
      MSE(k)=MSE(k)+e^2;	
      % accummulation of MSE
      MSEmin(k)=MSEmin(k)+(n(k))^2;	
   end
end
% sample index
ind=0:(K-1);
MSE = MSE/I;
MSEmin = MSEmin/I;
% Misadjustment computation 
M=MSE./MSEmin-1; 
plot(10*log10(MSE));
xlabel('iterations');
ylabel('MSE (dB)');