Forums

please check code

Started by sham...@yahoo.co.uk December 11, 2009
hi everybody,
i am doing a project on real time implementation of acoustic noise cancellation using the tms320c6713 dsk. Infact, the first part of my project is to perform a simple system identification. Below is a c code, can anybody please see if it is correct please. it is based on a simple adaptive filter structure found in chassaings book, fig 7.3

//AdaptIDFIRW.c Adaptive FIR for system identification of an FIR (uses C67 tools)

#include "DSK6713_AIC23.h" //codec-DSK support file
Uint32 fs=DSK6713_AIC23_FREQ_8KHZ; //set sampling rate
#include "noise_gen.h" //support file for noise generation
#define beta 1E-13 //rate of convergence

float w[WLENGTH+1]; //buffer coeff for adaptive FIR
int dly_adapt[WLENGTH+1]; //buffer samples of adaptive FIR
int dly_fix[N+1]; //buffer samples of fixed FIR
short out_type = 1; //output type adaptive or fixed FIR
int fb; //feedback variable
shift_reg sreg; //shift register

int prand(void) //gen pseudo-random sequence {-1,1}
{
int prnseq;
if(sreg.bt.b0)
prnseq = -8000; //scaled negative noise level
else
prnseq = 8000; //scaled positive noise level
fb =(sreg.bt.b0)^(sreg.bt.b1); //XOR bits 0,1
fb^=(sreg.bt.b11)^(sreg.bt.b13); //with bits 11,13 -> fb
sreg.regval<<=1;
sreg.bt.b0 //close feedback path
return prnseq; //return noise sequence
}

interrupt void c_int11() //ISR
{
int i;
int fir_out = 0; //init output of fixed FIR
int adaptfir_out = 0; //init output of adapt FIR
float E; //error=difference of fixed/adapt out

dly_fix[0] = prand(); //noise as input samples to fixed FIR
dly_adapt[0]=dly_fix[0]; //as well as to adaptive FIR

for (i = N-1; i>= 0; i--)
{
fir_out +=(h[i]*dly_fix[i]); //fixed FIR filter output
dly_fix[i+1] = dly_fix[i]; //update samples of fixed FIR
}
for (i = 0; i < WLENGTH; i++)
adaptfir_out +=(w[i]*dly_adapt[i]); //adaptive FIR filter output

E = fir_out - adaptfir_out; //error signal

for (i = WLENGTH-1; i >= 0; i--)
{
w[i] = w[i]+(beta*E*dly_adapt[i]); //update weights of adaptive FIR
dly_adapt[i+1] = dly_adapt[i]; //update samples of adaptive FIR
}

if (out_type == 1) //slider position for adaptive FIR
output_sample((short)(adaptfir_out)); //output of adaptive FIR filter
else if (out_type == 2) //slider position for fixed FIR
output_sample((short)(fir_out)); //output of fixed FIR filter
return;
}

void main()
{
int T = 0, i = 0;
for (i = 0; i <= WLENGTH; i++)
{
w[i] = coeffs[i]; //init coeff for adaptive FIR
dly_adapt[i] = 0; //init buffer for adaptive FIR
}
for (T = 0; T < N; T++)
dly_fix[T] = 0; //init buffer for fixed FIR

sreg.regval=0xFFFF; //initial seed value
fb = 1; //initial feevack value
comm_intr(); //init DSK, codec, McBSP
while (1); //infinite loop
}

_____________________________________