/*ADAPTDMV.C - ADAPTIVE FILTER FOR NOISE CANCELLATION */ #include "math.h" #define beta 1.5E-8 /*rate of convergence */ #define N 30 /*# of coefficients */ #define NS 128 /*# of output sample points*/ #define Fs 8000 /*sampling frequency */ #define pi 3.1415926 #define DESIRED 1000*sin(2*pi*T*1000/Fs) /*desired signal */ #define ADDNOISE 1000*sin(2*pi*T*312/Fs) /*additive noise */ #define REFNOISE 1000*cos(2*pi*T*312/Fs) /*reference noise*/ main() { int I,T; float Y, E, DPLUSN; float W[N+1]; float Delay[N+1]; volatile int *IO_OUTPUT= (volatile int*) 0x809d00; volatile int *IO_INPUT = (volatile int*) 0x809d80; for (T = 0; T < N; T++) { W[T] = 0.0; Delay[T] = 0.0; } for (T=0; T < NS; T++) /*# of output samples */ { Delay[0] = REFNOISE; /*adaptive filter's input*/ DPLUSN = DESIRED + ADDNOISE; /*desired + noise, d+n */ Y = 0; for (I = 0; I < N; I++) Y += (W[I] * Delay[I]); /*adaptive filter output */ E = DPLUSN - Y; /*error signal */ for (I = N; I > 0; I--) { W[I] = W[I] + (beta*E*Delay[I]); /*update weights */ if (I != 0) Delay[I] = Delay[I-1]; /*update samples */ } *IO_OUTPUT++ = E; /*overall output E */ *IO_INPUT++ = DPLUSN; /* store d + n */ } }