Adaptive FIR fornoise cancellation

stingymilitaryElectronics - Devices

Nov 27, 2013 (3 years and 7 months ago)

71 views

#define beta
0.0001220703125

#define N 30

#define LEFT 0

#define RIGHT 1

float w[N]

float delay[N]

float Fs = 8000.0;

short output;

short out_type =1;

volatile union{unsigned int uint; short channel[2];}CODECData



//
Adaptive FIR for

noise cancellation


v
oid DSP
FIR
Library::AdaptFIRNoiseCancellation(){



short i;


float yn=0, E=0, dplush=0, desired=0, noise=0;



CODECData.uint = input_sample();


desired = (float) CODECData.channel[LEFT];

noise = (float) CODECData.channel[RIGHT];


dplush = desired + noise;

delay[0] = noise;


for ( i =0; i< N; i++ ){


yn += ( w[i] * delay[i] );

}


E = (desired + noise )
-

yn;


f
or
( i=N
-
1; i>=0; i
--
){


w[i] = w[i] + beta*E*delay[i];


delay[i] = delay[i
-
1];

}


if ( out_type == 1 ){


output = ((short)E);

}

else if (out_type == 2

){


output = ((short)dplush);

}

Output_left_sample(output);


Return;

}


Caller Function





for ( T =0; T< N; T++ ){

w[T] = 0;

delay[T] = 0;

}

FIRLib.AdaptFIRNoiseCancellation()

w
hile(1);





#define stages 5


int a[stages][3] = {

{27940,
-
10910, 27940},

{32768,
-
11841, 32768},

{32768,
-
13744, 32768},

{32768,
-
11338, 32768},

{32768,
-
14239, 32768
}};


int b[stages][2] = {

{
-
11417, 25710},

{
-
9204, 31581},

{
-
15860, 31605},

{
-
10221, 32581},

{
-
15258, 32584}};


s
hort dly[stages][2] = {0};


//
IIR Filter using
cascaded Direct Form II

void DSPIIRLibrary::
CascDirectFII
(){


int i,input;


int un,yn;



input =
CODECData
.
input_sample();


for ( i=0; i<stages; i++ ){



un = input
-
((b[i][0]*dly[i][0])>>15)
-
((b[i][1]*dly[i][1])>>15;




yn = ((a[i][0]*un)>>15)+((a[i][1]*
dly[i][0])>>15)+((a[i][2]*dly[i][1])>>15);




dly[i][1] = dly[i][0];



dly[i][0] = un;

input = yn;

}


o
utput_sample(yn);

r
eturn;



}




Caller Function





IIR
Lib.CascDirectFII()

w
hile(1);





int
w1
[3] =
{0.375, 0.75, 0.25
};

int w2[2] = {
-
0.3125
,

-
0.875
}
;

int w3[6] = {0.125, 0.185, 0.125, 0.0625, 0.03125, 0.125};


int
dly[
3]
,yn[3]
;


//
Non
-
Linear
IIR Filter
(Volterra)

void DSPIIRLibrary::
RecursNonLinear
IIR
(){


int i,input;


int un,yn
,y1,y2,y3
;



dly[0] =
CODECData
.
input_sample();



for ( i=0; i<
3
; i++ ){



y1 += w
1
[i]*dl
y
[i];

}


for ( i=0; i<
2
; i++ ){



y2 += w
2
[i]*yn[i+1];

}


for ( i=0; i<
3
; i++ ){


for
(
j
=0; i<
2
; i++ ){

y3 += w
3
[
2*
i
+
j
]*yn[
j
+1]
*dly[i]
;



}


}


Yn[0]=y1+y2+y3;



for ( i=0; i<
2
; i++ ){

dly[i+1] = dly[i];

yn[i+1] = yn[i]

}


output_sampl
e(yn);

return;



}



Caller Function





IIR
Lib.
RecursNonLinear
IIR
()

w
hile(1);






void
adapt1(
float
in1,

float

in2,

float

gamma
, float

&
out1,

float

&
out2){



out2 = in1 * (1.0
-

gamma)
-

in2 * gamma;


out1 = in1 * gamma + in2 * (1.0 + gamma);

}


void
ad
apt2
(
float
in1,

float

in2,

float

gamma
, float &out1, float &out2){

float

t1;


t1 = in2 * (gamma
-

1.0)
-

in1 * gamma;


out1 = in1 + t1
-

in2;


out2 = t1;

}


//
Seventh order wave digital filter

(
wdf7
)

void DSP
Special
Library::
WaveDigitalFilter7th
(){




floa
t

but1, but2;


float
w
[6][3];



float

n1, n2, n3, n4, n5, n6, n6_1;


but1 =
CODECData
.
input_sample();


but2 =
CODECData
.
input_sample();



adapt1(but1,
w[0][2]
, 0.070579, n1,
w[0][0]
);


adapt1(n1,
w[1][2]
, 0.459054, n2,
w[1][0]
);


adapt2(n2,
w[2][2]
, 0.1925
556, n3,
w[2][0]
);


adapt1(but2,
w[3][2]
, 0.2474406, n4,
w[3][0]
);


adapt2(n4,
w[4][2]
, 0.3485903, n5,
w[4][0]
);


adapt2(n5,
w[5][2]
, 0.0626826, n6,
w[5][0]
);




outp <= 0.5 * (n6_1 + n3);



for (j = 0; j<
6
; j++){


for (j = 0; j<1; j++){


w[i][
j+1] = w[i][j];



}


}


n6_1 = n6;


output_sample(outp);

return;


}



Caller Function





Special
Lib.
WaveDigitalFilter7th

()

w
hile(1);