C code for noise cancellation using TMS 320vc5416 dspC code




#include "mcbsp_initcfg.h" 
#include "dsk5416.h" 
#include "dsk5416_pcm3002.h" 
#include 
#include 
#include 
#include "TMS320.H" 
#include "Dsplib.h" 

#define PI 3.1415926535897932384626433832795 
#define LEN 10000 

float *y,*e; 
long filesize(FILE *stream); 
void adaptive_filter(); 

DSK5416_PCM3002_Config setup= 
{ 
0x1ff, //Reg 0 left channel DAC attenuation 
0x1ff, //Reg 1 right channel DAC attenuation 
0x000, //Reg 2 control 
0x000, //Reg 3 Format selection 
}; //open codec with default setting 


void adaptive_filter() 
{ 
FILE *f_signal,*f_noise,*f_filter; 
long i = 0; 
float corrupt[LEN]; 
float noise[LEN]; 
long length=0; 
float values; 
char line[LEN]; 

/*variables used in adaptive filter section*/ 
double mu = 0.01; // Initialize the step size; 
Int16 wrdataL,wrdataR; 
int m,sum; 
float w[21]; 

DSK5416_PCM3002_CodecHandle hCodec; //initialize the board support library 
hCodec = DSK5416_PCM3002_openCodec(0,&setup); //set codec frequency 
DSK5416_PCM3002_setFreq(hCodec,8000); //endless loop audio codec 


if ((f_signal = fopen("corrupted_signal.dat","r")) == NULL) /*opening the corrupted signal file*/ 
{ 
fprintf(stderr,"cannot open corrupted signal file.\n"); 
exit(1); 
} 

if((f_noise=fopen("random_noise.dat","r")) == NULL) /*opening the reference noise file*/ 
{ 
fprintf(stderr, "Cannot open reference noise file.\n"); 
exit(1); 
} 

if((f_filter=fopen("filter_adaptive.dat","w")) == NULL) /*opening the reference noise file*/ 
{ 
fprintf(stderr, "Cannot open adaptive filter file.\n"); 
exit(1); 
} 


while(fgets(line, LEN, f_signal) != NULL) 
{ 
/* get a line, up to LEN chars from f_signal. done if NULL */ 
sscanf (line, "%f", &values); 
/* convert the string to a float */ 
corrupt[length]= values; 
printf("%f",corrupt[length]); 
length++; 
} 

while(fgets(line, LEN, f_noise) != NULL) 
{ 
/* get a line, up to LEN chars from f_noise. done if NULL */ 
sscanf (line, "%f", &values); 
/* convert the string to a float */ 
noise[i]= values; 
i++; 
} 



printf("Filesize of corrupted_signal.dat is %ld bytes\n",i); 
y = (float*)malloc(sizeof(int)*length); 
e = (float*)malloc(sizeof(int)*length); 

/*filter noisy signal using adaptive filter*/ 
/*----INITIALIZING THE VALUES----*/ 

for (i=0;i<21;i++) 
{ 
w[i] =0.0; //Initialize the adaptive filter coefficients 
} 
for(i=0;i { 
y[i] = 0.0; // Initialize the adaptive filter output array 
e[i] = 0.0; // Initialize the output array 
} 


/* Adaptive filtering using the LMS algorithm */ 
for (m = 22; m { 
sum = 0; 
for (i=1;i<22;i++) 
{ 
sum = sum + w[i]* noise[m - i]; 
} 
y[m] = sum; 
e[m] = corrupt[m]-y[m]; 
for(i=1 ; i<22 ; i++) 
{ 
w[i] = w[i] + 2*mu*e[m]*noise[m - i]; 
} 

wrdataL=(int)(0x7fff*e[m]); 
wrdataR=(int)(0x7fff*e[m]); 
while(!DSK5416_PCM3002_write16(hCodec,wrdataL)); //first write is from left channel 
while(!DSK5416_PCM3002_write16(hCodec,wrdataR)); //second write is from right channel 
} 



//now Right-write is lineout(good,left-write is speakerout, only left-read avail) 


DSK5416_PCM3002_closeCodec(hCodec); 
TSK_exit(); 
} 




long filesize(FILE *stream) 
{ 
long curpos, length; 
curpos = ftell(stream); 
fseek(stream, 0L, SEEK_END); 
length = ftell(stream); 
fseek(stream, curpos, SEEK_SET); 
return length; 
} 



void main() 
{ 
DSK5416_init(); 
adaptive_filter(); 
} 

No comments:

Post a Comment