C code for adaptive filter using 1 tap filter

/*----first you have to generate the file which has the values of the corrupted signal and the reference noise
------the above mentioned files can be generated in matlab(or in C) as given in my another blog of matlab code
------also i have taken a pure sine wave file to compare it with the values by the adaptive filter for comparison
------Also remove some comments to run the code on in dsp kit tms 320 v5416 and have some practical experience
*/



//#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,*f_pure;
long i = 0;
float corrupt[LEN];
float noise[LEN];
float sine[LEN];
long length=0;
float values;
char line[LEN];
/*variables used in adaptive filter section*/
float mu = 0.01; // Initialize the step size;
//Int16 wrdataL,wrdataR;
float w;
/*
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_pure = fopen("pure_signal.dat","r")) == NULL) /*opening the corrupted signal file*/
{
fprintf(stderr,"cannot open sine signal file.\n");
exit(1);
}

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\n",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++;
}

i=0;
while(fgets(line, LEN, f_pure) != NULL)
{
/* get a line, up to LEN chars from f_pure. done if NULL */
sscanf (line, "%f", &values);
/* convert the string to a float */
sine[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----*/

w = 0.3; //Initialize the adaptive filter coefficients

for(i=0;i {
y[i] = w*noise[i]; // Initialize the adaptive filter output array
e[i] = corrupt[i] - y[i]; // Initialize the output array
w = w + (mu * e[i]*noise[i]);
fprintf(f_filter,"%f\t%f\n",e[i],sine[i]);

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

free(y);
free(e);
fclose(f_signal);
fclose(f_noise);
fclose(f_pure);


/*
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