#include <stdio.h>
#include <stdlib.h>
float XR[10] ,AR[10],LMS();
float C2,C3,U,EFR,R1,R2,R3,XTR;
int IP,I,J;
int main (int argc, const char * argv[]) {
/*
This short routine demonstrates the LMS algorithm. It will estimate the coef. C2 & C3
of the TEST FILTER using the input (R1) and ouput data (XTR).
The estimated coefficients are printed out as AR[1]<-> C2
and AR[2]<-> C3. The error term is printed as EFR*/
C2=-6.9;
C3=1.55;
U=0.3;
IP=2;
EFR=1;
for (I=1; I<50; I++) {
R3=R2;
R2=R1;
R1=2*(((float)rand()/RAND_MAX)-0.5); // input signal
XTR=C2*R2+C3*R3+R1; //TEST FILTER
for ( J= IP+1; J>0 ;J--){
XR[J]=XR[J-1];
}
XR[1]=R1;
LMS();
printf( "%f ,%f, %f, \n", AR[1],AR[2],EFR);
}
}
float LMS(){ // LMS FUNCTION
int K;
/*
' This is the LMS sequential adaptive algorithm
'
' Input Parameters:
'
' IP - Integer scalar of filter length
' U - Real scalar representing the adaptive step size
' X - Array of contents of input data shift register
'
' Output Parameters:
'
' A - An array of AR parameters (adaptive weights)
'
' Notes:
'
' External array X must be dimensioned .GE. IP+1 and array A must
' be dimensioned .GE. IP in the calling program.
' EQUATION NOS. REFER TO MARPLE'S BOOK. EQS 9.3 & 9.8 ARE IN
' CHAPTER 9
'
' */
EFR=XTR-XR[1]; //DIFFERENCE BETWEEN TEST FILTER OUTPUT 'XTR'
// AND INPUT R1
for (K=1 ;K<=IP; K++){
EFR=EFR+AR[K]*XR[K+1]; // EQ. 9.3
}
for ( K=1 ;K<=IP; K++){
AR[K]=AR[K]-2*U*EFR*XR[K+1]; //EQ 9.8
}
}