// 1tap LMS algorithm simulation

clear;                            // clear all variable

MU=0.001;                         // step size parameter
h=3.0;                            // unknown system coefficent

xvec=loadwave('male.wav');        // male voice (fs=8kHz)
//xvec=loadwave('female.wav');    // female voice (fs=8kHz)
SIMLEN=length(xvec);              // set simulation data length
evec=zeros(1:SIMLEN);             // data buffer : e
wvec=zeros(1:SIMLEN);             // data buffer : w

w=0.0;                            // initial value of w
for i=1:SIMLEN                    // simulation loop
  x=xvec(i);                      // read input signal
  d=h*x;                          // calculate desired signal
  y=w*x;                          // adaptive filter 
  e=d-y;                          // calculate error signal
  
  w=w+2.0*MU*e*x;                 // LMS algorithm
  //w=w+2.0*MU*e*x/(x*x+1e-9);    // NLMS (Normalized LMS) algorithm
  
  wvec(i)=w;                      // save adaptive filter coefficient
  evec(i)=e;                      // save error signal
end

// graph plot
scf(0); clf; plot(wvec); 
xtitle('adaptive filter coefficient', 'i', 'wi');
scf(1); clf; plot(evec);
xtitle('error signal', 'i', 'e[i]');
scf(2); clf; plot(evec^2);
xtitle('power of error signal', 'i', 'e[i]^2');
scf(3); clf; plot(10*log10(evec+1e-14));
xtitle('power of error signal (log plot)', 'i', '10*log10(e[i]^2)');
