Académique Documents
Professionnel Documents
Culture Documents
ORourke 2:00pm
1
CONTENTS EECS 152B/CES135B Implement FIR and IIR Filters
Contents
Problem 1 3
Lowpass FIR Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Problem 2 7
Highpass FIR Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Problem 3 11
Bandpass FIR Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Problem 4 16
Lowpass IIR Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Problem 5 20
Highpass IIR Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Problem 6 24
Bandpass IIR Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Page 2 of 27
EECS 152B/CES135B Implement FIR and IIR Filters
Problem 1
Lowpass FIR Filter
Implement an FIR filter for low pass filtering.
Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
short inShort;
10 f l o a t input[75];
f l o a t y = 0;
short ys;
int i;
int j;
15
void main()
{
comm_poll();
20 while(1)
{
inShort = input_left_channel();
input[0] = ( f l o a t )inShort;
25
input[0] /= 10000;
y = 0;
y *= 1000;
40 ys = (short)y;
output_left_sample(ys);
}
}
Listing 3: lp75.h
f l o a t lowpass[75]={3.663493e-04, -2.658213e-04, -1.053694e-03, -1.559787e-03,
-1.350120e-03, -2.722435e-04, 1.353100e-03, 2.771166e-03, 3.105759e-03,
1.834415e-03, -8.093653e-04, -3.759031e-03, -5.489628e-03, -4.744107e-03,
-1.290370e-03, 3.701649e-03, 7.955725e-03, 9.048265e-03, 5.661719e-03,
5 -1.469770e-03, -9.485162e-03, -1.441258e-02, -1.297701e-02, -4.437347e-03,
8.469433e-03, 2.012731e-02, 2.419536e-02, 1.658128e-02, -2.015164e-03,
-2.524064e-02, -4.258587e-02, -4.296134e-02, -1.912323e-02, 2.878990e-02,
9.215754e-02, 1.558964e-01, 2.030639e-01, 2.204460e-01, 2.030639e-01,
1.558964e-01, 9.215754e-02, 2.878990e-02, -1.912323e-02, -4.296134e-02,
10 -4.258587e-02, -2.524064e-02, -2.015164e-03, 1.658128e-02, 2.419536e-02,
2.012731e-02, 8.469433e-03, -4.437347e-03, -1.297701e-02, -1.441258e-02,
-9.485162e-03, -1.469770e-03, 5.661719e-03, 9.048265e-03, 7.955725e-03,
3.701649e-03, -1.290370e-03, -4.744107e-03, -5.489628e-03, -3.759031e-03,
-8.093653e-04, 1.834415e-03, 3.105759e-03, 2.771166e-03, 1.353100e-03,
15 -2.722435e-04, -1.350120e-03, -1.559787e-03, -1.053694e-03, -2.658213e-04,
3.663493e-04};
The filter order we used for this lowpass FIR filter was 74 using a Hanning window. The transition band
frequency is 0.06 radians per sample. We designed a hanning window using a the kaiser function and we
used the function FIR1 to design our filter. The gain for this filter is simply the sum of its coefficients, 1.
This gain value does indeed match the gain in the oscilloscope.
Page 6 of 27
EECS 152B/CES135B Implement FIR and IIR Filters Problem 1
Problem 2
Highpass FIR Filter
Implement an FIR filter for high pass filtering.
Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
10 f l o a t input[75];
f l o a t y = 0;
short ys;
int i;
int j;
15 short inShort;
void main()
{
comm_poll();
20
while(1)
{
inShort = input_left_channel();
25 input[0] = ( f l o a t )inShort;
input[0] /= 10000;
y = 0;
y *= 1000;
40 ys = (short)y;
output_left_sample(ys);
}
}
Listing 6: hp75.h
f l o a t highpass[75]={-2.394542e-04, 1.064131e-03, -2.032407e-04, -1.449515e-03,
1.038450e-03, 1.483255e-03, -2.168201e-03, -8.981727e-04, 3.317254e-03,
-4.760582e-04, -4.053910e-03, 2.607603e-03, 3.872124e-03, -5.185693e-03,
-2.326875e-03, 7.604197e-03, -8.032495e-04, -9.025818e-03, 5.365352e-03,
5 8.527333e-03, -1.073507e-02, -5.302928e-03, 1.580368e-02, -1.117585e-03,
-1.905439e-02, 1.068972e-02, 1.869141e-02, -2.276555e-02, -1.271423e-02,
3.612908e-02, -1.344941e-03, -4.916148e-02, 2.802509e-02, 6.011001e-02,
-8.289118e-02, -6.740673e-02, 3.101262e-01, 5.697388e-01, 3.101262e-01,
-6.740673e-02, -8.289118e-02, 6.011001e-02, 2.802509e-02, -4.916148e-02,
10 -1.344941e-03, 3.612908e-02, -1.271423e-02, -2.276555e-02, 1.869141e-02,
1.068972e-02, -1.905439e-02, -1.117585e-03, 1.580368e-02, -5.302928e-03,
-1.073507e-02, 8.527333e-03, 5.365352e-03, -9.025818e-03, -8.032495e-04,
7.604197e-03, -2.326875e-03, -5.185693e-03, 3.872124e-03, 2.607603e-03,
-4.053910e-03, -4.760582e-04, 3.317254e-03, -8.981727e-04, -2.168201e-03,
15 1.483255e-03, 1.038450e-03, -1.449515e-03, -2.032407e-04, 1.064131e-03,
-2.394542e-04};
For this highpass FIR filter, we used the same filter order as the lowpass FIR filter, 74. As expected, the
transition band is also the same as the lowpass filter, 0.6 radians per sample. We used the filter we designed
in the lowpass filter using FIR1 and applied the firlp2hp function to transform it to a highpass FIR filter.
The gain of this FIR filter is 0.12 by adding up all the coefficients.
Page 10 of 27
EECS 152B/CES135B Implement FIR and IIR Filters Problem 2
Problem 3
Bandpass FIR Filter
Implement a FIR filter for band pass filtering.
5 Wn = f_lp/(Fs/2);
win = kaiser(75,3.86);
b1 = fir1(74, Wn, win);
A = horzcat(b2,b1)
15
5 Wn = f_lp/(Fs/2);
win = kaiser(75,3.86);
b = fir1(74, Wn, win);
a = b + g;
15
Listing 9: bp75.h
f l o a t bandpass[75]={0 2.128262e-03, -2.899031e-03, 2.966511e-03, -1.796345e-03,
-9.521165e-04, 5.215206e-03, -1.037139e-02, 1.520839e-02, -1.805164e-02,
1.705467e-02, -1.060586e-02, -2.235170e-03, 2.137944e-02, -4.553111e-02,
7.225815e-02, -9.832295e-02, 1.202200e-01, -1.348135e-01, 1.139478e+00,
5 -1.348135e-01, 1.202200e-01, -9.832295e-02, 7.225815e-02, -4.553111e-02,
2.137944e-02, -2.235170e-03, -1.060586e-02, 1.705467e-02, -1.805164e-02,
1.520839e-02, -1.037139e-02, 5.215206e-03, -9.521165e-04, -1.796345e-03,
2.966511e-03, -2.899031e-03, 2.128262e-03,};
We used a filter order of 74 to design this bandpass FIR filter. Both transition bands had frequencies of 0.6
radians per sample. We used both the lowpass and highpass filter we previously design and concatenated
them both which resulted in this bandpass FIR filter. The gain we resulted in was 1.12 for this filter.
Page 15 of 27
EECS 152B/CES135B Implement FIR and IIR Filters Problem 3
Problem 4
Lowpass IIR Filter
Implement an IIR filter for low pass filtering.
We could not get the filter to work because of the NAN seen above
Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
10 f l o a t d_samples[stages][2];
void main()
{
comm_poll();
15
while(1)
{
int i
f l o a t input, n, output;
20
output = ((a[i][0]*n))+((a[i][1]*d_samples[i][0]))+
30 ((a[i][2]*d_samples[i][1]));
output_sample(output);
}
}
10 [b,a] = ellip(6,5,40,Wnlp)
freqz(b,a,Fs)
[z,p,k] = ellip(6,5,40,Wnlp)
[sos,g] = zp2sos(z,p,k);
15
The filter order we used for this lowpass IIR filter was 6. The transition band frequency is 0.10 radians per
sample for this filter. We used the ellip function to design this filter. Then we used the dir2cas function to
transform it to second order cascaded form. The gain for this filter is 0.0165. We were not able to completely
run the C code because we continued to get a NaN error. We eventually realized the indexing for the array
a was off by one which caused it to explode.
Page 19 of 27
EECS 152B/CES135B Implement FIR and IIR Filters Problem 4
Problem 5
Highpass IIR Filter
Implement an IIR filter for high pass filtering.
We could not get the filter to work because of the NAN seen above
Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
10 f l o a t d_samples[stages][2];
void main()
{
comm_poll();
15
while(1)
{
int i
f l o a t input, n, output;
20
output = ((a[i][0]*n))+((a[i][1]*d_samples[i][0]))+
30 ((a[i][2]*d_samples[i][1]));
output_sample(output);
}
}
The filter order we used for this highpass IIR filter was 6. The transition band frequency is 0.12 radians
per sample for this filter. We used the ellip function to design this filter as we did for the lowpass IIR filter.
Then we used the dir2cas function to transform it to second order cascaded form. The gain for this filter
is 0.044. We were not able to completely run the C code because we continued to get a NaN error. We
eventually realized the indexing for the array a was off by one which caused it to explode.
Page 23 of 27
EECS 152B/CES135B Implement FIR and IIR Filters Problem 5
Problem 6
Bandpass IIR Filter
Implement a IIR filter for band pass filtering.
We could not get the filter to work because of the NAN seen above
clc;
clear all;
Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
10 f l o a t d_samples[stages][2];
void main()
{
comm_poll();
15
while(1)
{
int i
f l o a t input, n, output;
20
output = ((a[i][0]*n))+((a[i][1]*d_samples[i][0]))+
30 ((a[i][2]*d_samples[i][1]));
output_sample(output);
}
}
The filter order we used for this highpass IIR filter was 6. The transition band frequency is 0.12 radians
per sample for this filter. We used the ellip function to design this filter as we did for the lowpass IIR filter.
Then we used the dir2case function to transform it to second order cascaded form. The gain for this filter
is 0.0299. We were not able to completely run the C code because we continued to get a NaN error. We
eventually realized the indexing for the array a was off by one which caused it to explode.
Page 27 of 27