Sei sulla pagina 1di 33

EECS 152B/CSE 135B

DSP Design & Laboratory


Assignment 3
Implement FIR and IIR Filters using TI c6713 DSK

Milan Patel (15888131)


Ali Saad (19141859)
Dacoda Strack (56925997)
Lab Section 3A
University of California, Irvine

Problem 1
The objective of this problem is to implement an FIR lowpass filter with the dsk board.
The filter should have a cuto frequency of 1760 Hz.
itm:questions-fir-lowpass
The following code starts o by declaring an array lowpass[] which has the filter coecients
found by the matlab code found in i.. It then begins polling and fills an input vector (of the
same length as the filter) with samples from the dsk board. After filling the input vector,
it then enters a loop which accomplishes the following:
1. It sums the convolution of the current input vector with the filter coecients
2. It outputs this sum to the board
3. It shifts the input vector once to the left and inserts a new sample in its rightmost
position
1
2

#include "dsk6713_aic23.h"
#include "dsk6713_led.h"

3
4
5

#define DSK6713_AIC23_INPUT_MIC 0x0015


#define DSK6713_AIC23_INPUT_LINEIN 0x0011

6
7

#define FILTER_LENGTH 49

8
9
10

Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

float lowpass[FILTER_LENGTH] = {-0.00081696436698336546, -0.00021751423510184715,


0.00066640936656965611,
0.00157034732804796000,
-0.00615075099554396580,
0.00500021450790641420,
0.01232212944474056100,
-0.03696902393001178000,
0.02746970212542162300,
0.20195759380955880000,
0.15377710387149290000,
-0.01779006866361691800,
-0.02095792775291762900,
0.01680622510945733700,
-0.00237847116307618370,
-0.00354590407634487650,
0.00214225259628533000,
-0.00021751423510184715,

0.00161979322029948370, 0.00214225259628533000,
-0.00047558977253344320, -0.00354590407634487650,
-0.00624411669947530590, -0.00237847116307618370,
0.01295156806914578300, 0.01680622510945733700,
-0.00158902124438583140, -0.02095792775291762900,
-0.03873070207107721900, -0.01779006866361691800,
0.08966015914615578500, 0.15377710387149290000,
0.21984511275197313000, 0.20195759380955880000,
0.08966015914615578500, 0.02746970212542162300,
-0.03873070207107721900, -0.03696902393001178000,
-0.00158902124438583140, 0.01232212944474056100,
0.01295156806914578300, 0.00500021450790641420,
-0.00624411669947530590, -0.00615075099554396580,
-0.00047558977253344320, 0.00157034732804796000,
0.00161979322029948370, 0.00066640936656965611,
-0.00081696436698336546};

29
30
31
32

unsigned int i = 0;
unsigned int j = 0;
unsigned int z = 0;

33
34

void main()

35

{
comm_poll();

36
37

float input[FILTER_LENGTH] = {0};


short sum = 0;
short b;

38
39
40
41

/* Fill input vector */


for(z = 0; z < FILTER_LENGTH; z++){
input[z] = input_left_sample();
}

42
43
44
45
46

while(1) {

47

/* Perform multiplication and sum */


for(z = 0; z < FILTER_LENGTH; z++){
sum += lowpass[z]*input[FILTER_LENGTH-1-z];
}

48
49
50
51
52

x
output_left_sample(sum);
sum = 0;

53
54
55

/* Shift input and enter input sample to x[0] */


for(i = 0;i<FILTER_LENGTH-1;i++){
input[i] = input[i+1];
}

56
57
58
59
60

b = input_left_sample();
input[FILTER_LENGTH-1] = (float)b;

61
62

63
64

Figure 1.1: Code used to test FIR lowpass filter


With the above code, the oscilloscope waveforms shown in Figure 1.2 are generated. As can
be seen in the figure, the amplitude of the waveform is much higher at 208 Hz (770 mV) than
at 1.85 kHz (310 mV), as would be expected from a lowpass filter.
These amplitudes seem to make sense based on our filter characteristics. Assuming that
the first waveform at 200 Hz has a unitary gain, the amplitude of the second waveform at
1.85 kHz should be less than 3 dB 770 mV = 545 mV since 1.85 kHz is higher than the
cut-o frequency 1590 kHz (see Table 5.i.1). As can be seen in the figure, this is indeed the
case.

(a) 208 Hz

(b) 1.85 kHz

Figure 1.2: Output waveforms for the FIR lowpass filter resulting from
the code given in Figure 1.1 for dierent pure sine frequencies

Problem 2
The objective for this problem is to design a highpass filter with cuto frequency 4560 Hz.
The code below is exactly the same as for Problem 1 save for the filter coecient values.
1
2

#include "dsk6713_aic23.h"
#include "dsk6713_led.h"

3
4
5

#define DSK6713_AIC23_INPUT_MIC 0x0015


#define DSK6713_AIC23_INPUT_LINEIN 0x0011

6
7

#define FILTER_LENGTH 45

8
9
10

Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

float highpass[FILTER_LENGTH] = {0.00042641152461490183, 0.00112562953175404700,


-0.00092337645796045369, -0.00148575066233061150,
0.00193128457176655390, -0.00430111838849391110,
0.00779811443426204570, 0.00063143422727044536,
0.00293017932281575520, 0.01834265031540686200,
-0.02457872732127334600, 0.02287737398573095500,
-0.04575027393544489600, -0.03556318675588369300,
0.03887105430451293100, -0.31455060652527644000,
-0.31455060652527644000, 0.03887105430451293100,
-0.03556318675588369300, -0.04575027393544489600,
0.02287737398573095500, -0.02457872732127334600,
0.01834265031540686200, 0.00293017932281575520,
0.00063143422727044536, 0.00779811443426204570,
-0.00430111838849391110, 0.00193128457176655390,
-0.00148575066233061150, -0.00092337645796045369,
0.00042641152461490183};

28
29
30
31

unsigned int i = 0;
unsigned int j = 0;
unsigned int z = 0;

32
33

void main()

34

35

comm_poll();

36
37
38
39

float input[FILTER_LENGTH] = {0};


short sum = 0;
short b;

40
41
42
43
44

/* Fill input vector */


for(z = 0; z < FILTER_LENGTH; z++){
input[z] = input_left_sample();
}

45
46
47

while(1) {
/* Perform multiplication and sum */

0.00208656645683152510,
-0.00191164049242499900,
-0.01257176282544475100,
-0.01009072458143068900,
0.03057290797537401400,
0.09462042004619715400,
0.46033558445336265000,
0.09462042004619715400,
0.03057290797537401400,
-0.01009072458143068900,
-0.01257176282544475100,
-0.00191164049242499900,
0.00208656645683152510,
0.00112562953175404700,

for(z = 0; z < FILTER_LENGTH; z++){

48

sum += lowpass[z]*input[FILTER_LENGTH-1-z];

49

50
51

output_left_sample(sum);
sum = 0;

52
53
54

/* Shift input and enter input sample to x[0] */


for(i = 0;i<FILTER_LENGTH-1;i++){
input[i] = input[i+1];
}

55
56
57
58
59

b = input_left_sample();
input[FILTER_LENGTH-1] = (float)b;

60
61

62
63

Figure 2.1: Code used to test FIR highpass filter

(a) 4.09 kHz

(b) 5.26 kHz

Figure 2.2: Output waveforms for the FIR highpass filter resulting from
the code given in Figure 2.1 for dierent pure sine frequencies
As shown in Figure 2.2, this filter shows the desired filter characteristics. Again, here, the
amplitudes seem to make sense. Assuming that the gain at 5.26 kHz is unity, it is expected
for the gain of the 4.09 kHz signal to be less than 3 dB since it is below the cuto frequency.
Indeed it is, as 3 dB gain would give a 453 mV amplitude, and this is much less than that.
In fact, seeing that the normalized frequency of the 4.09 kHz is 0.511 25 and thus very close
to the value which gives 40 dB gain (see Table 5.ii.1), its a wonder the amplitude isnt
even lower. The reason for this likely boils down to noise in the signal. As can be seen from
the figure, much of the signal can be attributed to noise, and from previous experience we
know that noise on the order of 50 mV is not unexpected.

Problem 3
The objective for this problem is to design a highpass filter with cuto frequencies of 1760 Hz
and 4560 Hz.
The code below is exactly the same as for Problem 1 save for the filter coecient values.
1
2

#include "dsk6713_aic23.h"
#include "dsk6713_led.h"

3
4
5

#define DSK6713_AIC23_INPUT_MIC 0x0015


#define DSK6713_AIC23_INPUT_LINEIN 0x0011

6
7

#define FILTER_LENGTH 44

8
9
10

Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011

11
12
13
14

unsigned int i = 0;
unsigned int j = 0;
unsigned int z = 0;

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

float bandpass[FILTER_LENGTH]= {0.000031804510880084353, -0.000019789648741247706,


-0.001124742421041582600,
0.000264852153473965080,
-0.007818726070629103100,
0.004500191811069565400,
0.062956242466695594000,
0.036841292313531136000,
-0.095827441561315896000,
-0.095827441561315896000,
0.036841292313531136000,
0.062956242466695594000,
0.004500191811069565400,
-0.007818726070629103100,
0.000264852153473965080,
-0.001124742421041582600,

0.002285005291701485100,
-0.000662268802485741410,
-0.020803397305029305000,
-0.013341181860725434000,
0.012430524442952817000,
-0.052009560747888622000,
0.312710665090100040000,
-0.247111212040529190000,
-0.019285086707876285000,
0.019690796960738364000,
-0.002496698628516754200,
0.004468372773721288400,
0.005281289897170116600,
-0.000019789648741247706,

31
32

void main()

33

34

comm_poll();

35
36
37
38

float input[FILTER_LENGTH] = {0};


short sum = 0;
short b;

39
40
41
42
43

/* Fill input vector */


for(z = 0; z < FILTER_LENGTH; z++){
input[z] = input_left_sample();
}

44
45
46

while(1) {
/* Perform multiplication and sum */

0.005281289897170116600,
0.004468372773721288400,
-0.002496698628516754200,
0.019690796960738364000,
-0.019285086707876285000,
-0.247111212040529190000,
0.312710665090100040000,
-0.052009560747888622000,
0.012430524442952817000,
-0.013341181860725434000,
-0.020803397305029305000,
-0.000662268802485741410,
0.002285005291701485100,
0.000031804510880084353};

for(z = 0; z < FILTER_LENGTH; z++){

47

sum += lowpass[z]*input[FILTER_LENGTH-1-z];

48

49
50

x
output_left_sample(sum);
sum = 0;

51
52
53

/* Shift input and enter input sample to x[0] */


for(i = 0;i<FILTER_LENGTH-1;i++){
input[i] = input[i+1];
}

54
55
56
57
58

b = input_left_sample();
input[FILTER_LENGTH-1] = (float)b;

59
60

61
62

Figure 3.1: Code used to test FIR bandpass filter

(a) 1.289 kHz

(b) 4.12 kHz

Figure 3.2: Output waveforms for the FIR bandpass filter resulting from
the code given in Figure 3.1 for dierent pure sine frequencies
With the above code, the oscilloscope waveforms shown in Figure 3.2 are generated. While
we confirmed that the filter attenuates frequencies above the higher cuto frequencies, we
neglected to take a picture of that oscilloscope waveform.

Problem 4
The objective of this problem is to implement the same filters specified in Problem 1, Problem
2, and Problem 3 with IIR filters instead of FIR ones.
While using the following code in lab, we elected to go about having just one set of filter
coecients uncommented at a time while changing N to fit the size of the filter we were
using. In the snippet below, all three sets of coecients are shown uncommented for claritys
sake.
In any case, after defining N (the length of the given filter) and the filter coecients proper
the code declares input[] and output[] vectors to hold sections of the input and output
signals of the filter of the same length as the filter.
After entering the while loop, it sums the convolution of the current input[] vector and
the b filter coecients in order to get the first part of the output signal. It then sums the
convolution of the current output[] vector and the a filter coecients and subtracts this
sum from the sum described above.
This floating-point dierence is typecasted to a short and output to the speakers.
The input and output vectors are then shifted to the left and new appropriate values are
placed in their rightmost positions.
1
2

#include "dsk6713_aic23.h"
#include "dsk6713_led.h"

3
4
5

#define DSK6713_AIC23_INPUT_MIC 0x0015


#define DSK6713_AIC23_INPUT_LINEIN 0x0011

6
7
8

Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011

9
10
11

// Change length of filter and uncomment appropriate filter


#define N 15

12
13
14
15

unsigned int i = 0;
unsigned int j = 0;
unsigned int z = 0;

16
17
18
19
20
21

/* Low pass Filter with length 8 */


float b[N] = { 0.015495475408834581,
0.063486394663502116,
-0.029432173299327756,
-0.044995815898198482,

-0.044995815898198468,
-0.029432173299327589,
0.063486394663502185,
0.015495475408834577};

22
23
24
25
26

float a[N] = { 1.000000000000000000, - 5.36523779557625600,


13.245327837339437000, -19.24381272936014400,
17.684441942602113000, -10.25448614975332100,
3.475008624624797100, - 0.53213396812700375};

27
28

/* High Pass Filter with length 8 */

29

float b[N] = {0.049627511046376083, -0.049188451610409481,


0.138121314059458600, -0.139309244079199950,
0.139309244079199780, -0.138121314059458520,
0.049188451610409245, -0.049627511046375986};

30
31
32
33
34

float a[N] = {1.000000000000000000, 2.306895654299675700,


4.422490037496873000, 5.049223959537021000,
4.574298241591526100, 2.802757514457913300,
1.203775077617120300, 0.289193186820021040};

35
36
37
38
39
40
41
42
43
44

/* Band Pass Filter with length 15 */


float b[N] = { 0.032327592148735554, -0.092434399595495334,
-0.182665720844533200, 0.235427846647907140,
0.089676279279297999, -0.000000000000000000,
0.202790003150476490, -0.235427846647908390,
-0.139335498243090470, 0.092434399595496028,

0.139335498243089440,
-0.202790003150475790,
-0.089676279279297971,
0.182665720844534420,
-0.032327592148735811};

45
46

float a[N] = {

47
48
49
50

1.000000000000000000,
-21.188830934225550000,
54.470584082326944000,
-35.774596361036920000,
5.419161525366078500,

- 4.027423078991632500, 10.808766498459843000,
34.523519894086988000, -46.620894233142970000,
-54.546702929057446000, 47.782069693527120000,
23.131917894380116000, -12.332159299321756000,
- 1.702254530803074800,
0.365819348914442730};

51
52

void main()

53

54

comm_poll();

55
56
57

short input[N] = {0};


float output[N] = {0};

58
59
60

float temp =0;


float temp1 = 0;

61
62
63

float out;
short outs;

64
65

short bs;

66
67

while(1)

68

69
70
71
72
73

// Perform multiplication and sum with flipped input and b


for(z = 0; z < N; z++)
{
temp += b[z]*input[N-1-z];
}

74
75
76
77
78
79

// Perform multiplication and sum with flipped output and a


for(z = 1; z < N; z++)
{
temp1 += output[N-z]*a[z];
}

80
81
82

// Subtract away feedback


out = temp-temp1;

83

// Output to speakers
outs = (short)out;
output_left_sample(outs);

84
85
86
87

temp = 0; temp1 = 0; //reset sum

88

variables for next cycle

89

// Shift input vector left (same as reversing input and shifting right)
for(i = 0;i<N-1;i++)
{
input[i] = input[i+1];
}

90
91
92
93
94
95

// Shift output vector left (same as reversing output and shifting right)
for(i = 0;i<N-1;i++)
{
output[i] = output[i+1];
}

96
97
98
99
100
101

// Set output into output vector and reset output sum


output[N-1] = out;
out = 0;

102
103
104
105

// Fetch input and enter into last value of input array (same as first value
// of flipped array)
bs = input_left_sample();
input[N-1] = bs;

106
107
108
109

110
111

Figure 4.1: Code used to test IIR filters

Figure 4.2: This is a figure

While we observed the expected filter behavior during the lab, we didnt realize that by attenuating the
signal too far we wouldnt be able to see the frequency of the waveform in the oscilloscope measurements
2
See Footnote 1
3
See Footnote 1

10

(b) Some higher frequency1

(a) Near 1.4802 kHz

Figure 4.3: Output waveforms for the IIR lowpass filter resulting from the
code given in Figure 4.1 for dierent pure sine frequencies

(a) Some lower frequency2

(b) 4.95 kHz

Figure 4.4: Output waveforms for the IIR highpass filter resulting from
the code given in Figure 4.1 for dierent pure sine frequencies

11

(b) Some frequency3

(a) 2.65 kHz

Figure 4.5: Output waveforms for the IIR bandpass filter resulting from
the code given in Figure 4.1 for dierent pure sine frequencies

12

Questions
100 mA 4 102

x=

(y) dy

1. Document the resulting parameters of the filters you design (i.e. filter
order, transition band frequencies, function used) & their associated matlab
code. Include the filter frequency plots using matlab for the coecients
used in all of the above problems. Plot the detailed passband and stopband
to demonstrate that the ripple requirements are met. Calculate the gain
values at the test frequencies used and verify that these correspond to that
on an oscilloscope.
For the gain tables below, the theoretical gain is found in matlab and compared to
the experimental gain. The experimental gain was assumed to be unitary when the
input signal frequency was in the passband, since we never actually probed the input
signal itself. For the signal not in the passband, the gain is calculated by
[
]
signal amplitude
gain = 20 log
dB
passband signal amplitude
As noted in Footnote 1, we didnt record the frequencies of any of the input signals not
in the IIR filters passbands. Because of this, there are no gain tabulations in those
filters sections.
All calculated experimental gains seem to be within 10 dB of their theoretical values,
though this margin is quite high.
i. FIR Lowpass Filter
Command used
Filter order
Transition bands

fir1()
48
/ (0.1992, 0.2637)

Table 5.i.1: Parameters used for the FIR lowpass filter

bl = fir1(48,[0.23]);
Figure 5.i.1: Matlab code used to generate FIR lowpass filter coecients

13

Frequency (kHz)
0.208
1.850

( rad/sample) Theo. Gain (dB) Exp. Gain (dB)


0.026
0.033
0.000
0.231
14.066
18.196

Table 5.i.2: Theoretical and experimental gain values for the FIR lowpass
filter

Magnitude (dB)

Lowpass Filter Response


100
0
-100
-200
-300
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.8

0.9

Phase (degrees)

Normalized Frequency (#: rad/sample)


0
-5
-10
-15
-20
-25
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Normalized Frequency (#: rad/sample)

Figure 5.i.2: Magnitude and phase responses for the FIR lowpass filter

Lowpass Filter Response


0.5

Magnitude (dB)

-0.5

-1

-1.5

-2

-2.5

-3

-3.5
0

0.02

0.04

0.06

0.08

0.1

0.12

0.14

0.16

0.18

0.2

Normalized Frequency (#: rad/sample)

Figure 5.i.3: Close-up of magnitude response in the passband of the FIR


lowpass filter

14

ii. FIR Highpass Filter


Command used
Filter order
Transition bands

fir1()
48
/ (0.5059, 0.5762)

Table 5.ii.1: Parameters used for the FIR highpass filter

bh = fir1(44,0.54,high)
Figure 5.ii.1: Matlab code used to generate FIR highpass filter coecients
Frequency (kHz)
4.090
5.260

( rad/sample) Theo. Gain (dB) Exp. Gain (dB)


0.511
32.615
39.043
0.657
0.028
0.000

Table 5.ii.2: Theoretical and experimental gain values for the FIR highpass filter

15

Magnitude (dB)

Highpass Filter Response


100
0
-100
-200
-300
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.8

0.9

Phase (degrees)

Normalized Frequency (#: rad/sample)


10
0
-10
-20
-30
-40
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Normalized Frequency (#: rad/sample)

Figure 5.ii.2: Magnitude and phase responses for the FIR highpass filter

Highpass Filter Response


0.5

Magnitude (dB)

-0.5

-1

-1.5

-2

-2.5

-3
0.55

0.6

0.65

0.7

0.75

0.8

0.85

0.9

0.95

Normalized Frequency (#: rad/sample)

Figure 5.ii.3: Close-up of magnitude response in the passband of the FIR


highpass filter

16

iii. FIR Bandpass Filter


Command used
Filter order
Transition bands

fir1()
48
/ (0.1641, 0.2363) (0.5566, 0.6509)

Table 5.iii.1: Parameters used for the FIR bandpass filter

bb = fir1(43,[0.20 0.59],band);
Figure 5.iii.1: Matlab code used to generate FIR bandpass filter coecients
Frequency (kHz)
1.289
4.120

( rad/sample) Theo. Gain (dB) Exp. Gain (dB)


0.161
43.016
33.172
0.515
0.026
0.000

Table 5.iii.2: Theoretical and experimental gain values for the FIR bandpass filter

17

Magnitude (dB)

Bandpass Filter Response


100
0
-100
-200
-300
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.8

0.9

Phase (degrees)

Normalized Frequency (#: rad/sample)


10
0
-10
-20
-30
-40
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Normalized Frequency (#: rad/sample)

Figure 5.iii.2: Magnitude and phase responses for the FIR bandpass filter

Bandpass Filter Response


0.5

Magnitude (dB)

-0.5

-1

-1.5

-2

-2.5

-3

-3.5
0.2

0.25

0.3

0.35

0.4

0.45

0.5

0.55

0.6

Normalized Frequency (#: rad/sample)

Figure 5.iii.3: Close-up of magnitude response in the passband of the FIR


bandpass filter

18

iv. IIR Lowpass Filter


Command used
Filter order
Transition bands

ellip()
7
/ (0.2207, 0.2266)

Table 5.iv.1: Parameters used for the IIR lowpass filter

[bl,al] = ellip(7,1,40,0.22);
Figure 5.iv.1: Matlab code used to generate IIR lowpass filter coecients

Magnitude (dB)

IIR Lowpass Filter Response


100
0
-100
-200
-300
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.8

0.9

Phase (degrees)

Normalized Frequency (#: rad/sample)


0
-2
-4
-6
-8
-10
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Normalized Frequency (#: rad/sample)

Figure 5.iv.2: Magnitude and phase responses for the IIR lowpass filter

19

IIR Lowpass Filter Response


1

Magnitude (dB)

-1

-2

-3

-4

-5

-6

-7
0

0.05

0.1

0.15

0.2

0.25

Normalized Frequency (#: rad/sample)

Figure 5.iv.3: Close-up of magnitude response in the passband of the IIR


lowpass filter

20

v. IIR Highpass Filter


Command used
Filter order
Transition bands

ellip()
7
/ (0.5645, 0.5703)

Table 5.v.1: Parameters used for the IIR highpass filter

[bh,ah]=ellip(7,1,40,0.57,high)
Figure 5.v.1: Matlab code used to generate IIR highpass filter coecients

Magnitude (dB)

IIR Highpass Filter Response


50
0
-50
-100
-150
-200
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.8

0.9

Phase (degrees)

Normalized Frequency (#: rad/sample)


10
8
6
4
2
0
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Normalized Frequency (#: rad/sample)

Figure 5.v.2: Magnitude and phase responses for the IIR highpass filter

21

IIR Highpass Filter Response


0

Magnitude (dB)

-0.5

-1

-1.5

-2

-2.5
0.55

0.6

0.65

0.7

0.75

0.8

0.85

0.9

0.95

Normalized Frequency (#: rad/sample)

Figure 5.v.3: Close-up of magnitude response in the passband of the IIR


highpass filter

22

vi. IIR Bandpass Filter


Command used
Filter order
Transition bands

ellip()
7
/ (0.2188, 0.2207) (0.5723, 0.5762)

Table 5.vi.1: Parameters used for the IIR bandpass filter

[bb,ab] = ellip(7,1,40,[0.22 0.57]);


Figure 5.vi.1: Matlab code used to generate IIR bandpass filter coecients

Magnitude (dB)

Bandpass Filter Response


50
0
-50
-100
-150
-200
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.8

0.9

Phase (degrees)

Normalized Frequency (#: rad/sample)


10
5
0
-5
-10
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Normalized Frequency (#: rad/sample)

Figure 5.vi.2: Magnitude and phase responses for the IIR bandpass filter

23

Bandpass Filter Response


0

Magnitude (dB)

-5

-10

-15

-20

-25
0.2

0.25

0.3

0.35

0.4

0.45

0.5

0.55

0.6

Normalized Frequency (#: rad/sample)

Figure 5.vi.3: Close-up of magnitude response in the passband of the IIR


bandpass filter

24

Conclusion
Through the course of this lab, we learned how filters can be implemented and convolutions
performed using c code, along with how to generate the filter coecients for FIR and IIR
low-, high- and bandpass filters using standard matlab functions.
While our experimental values seem to dier from our theoretical ones a bit, this likely has
to do with our failing to record the original input signal amplitude.

25

Matlab Code
1
2
3
4
5

%% Preliminary shit
ytexlabel = @(str) ylabel(str, interpreter, latex, FontUnits, points, FontSize, 16);
xtexlabel = @(str) xlabel(str, interpreter, latex, FontUnits, points, FontSize, 16);
textitle = @(str) title(str, interpreter, latex, FontUnits, points, FontSize, 20);
redline = @(x,y) plot(x, y, :r);

6
7

magnitude = @(h) 20*log(abs(h));

8
9
10

%% Low Pass Filter (cutoff w = 0.22)


bl = fir1(48,[0.23]);

11
12
13

[h,w] = freqz(bl, 1, 512);


phi = phasez(bl, 1, 512);

14
15
16

% Plot
figure(1);

17
18
19
20
21
22
23
24

% Magnitude
subplot(2, 1, 1);
plot(w/pi, magnitude(h))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Lowpass Filter Response);

25
26
27
28
29
30
31

hold on;
transitionBandStart = w( find(magnitude(h) < -3, 1))/pi;
transitionBandEnd = w( find(magnitude(h) < -40, 1))/pi;
redline([transitionBandStart transitionBandStart], [-300, 100]);
redline([transitionBandEnd transitionBandEnd],[-300, 100]);
hold off;

32
33
34
35
36
37
38

% Phase
subplot(2, 1, 2);
plot(w/pi, phi)
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Phase (degrees));

39
40
41

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-plot.pdf

42
43
44
45
46
47
48
49

% In close magnitude
figure(2)
plot(w(1:103)/pi, magnitude(h(1:103)))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Lowpass Filter Response);

50
51

set(gcf, PaperPositionMode, auto);

26

52

% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-close-up-plot.pd

53
54
55

%% High Pass Filter (cutoff w =0.57)


bh = fir1(44,0.54,high)

56
57
58

[h, w] = freqz(bh, 1, 512);


phi = phasez(bh, 1, 512);

59
60
61

% Plot
figure(1);

62
63
64
65
66
67
68
69

% Magnitude
subplot(2, 1, 1);
plot(w/pi, magnitude(h))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Highpass Filter Response);

70
71
72
73
74
75
76

hold on;
transitionBandStart = w( find( magnitude(h) > -40, 1) )/pi;
transitionBandEnd = w( find( magnitude(h) > -3, 1) )/pi;
redline([transitionBandStart transitionBandStart], [-300, 100]);
redline([transitionBandEnd transitionBandEnd],[-300, 100]);
hold off;

77
78
79
80
81
82
83

% Phase
subplot(2, 1, 2);
plot(w/pi, phi)
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Phase (degrees));

84
85
86

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-highpass-plot.pdf

87
88
89
90
91
92
93
94

% In close magnitude
figure(2)
plot(w( find(magnitude(h) > -3, 1) :end)/pi, magnitude(h( find(magnitude(h) > -3, 1) :end)))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Highpass Filter Response);

95
96
97

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-highpass-close-up-plot.p

98
99
100

%% Band Pass Filter (between wc =0.22 and 0.57 )


bb = fir1(43,[0.20 0.59],band);

101
102
103

[h,w] = freqz(bb, 1, 512);


phi = phasez(bb, 1, 512);

104
105

% Plot

27

106

figure(1);

107
108
109
110
111
112
113
114

% Magnitude
subplot(2, 1, 1);
plot(w/pi, magnitude(h))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Bandpass Filter Response);

115
116
117
118
119
120

hold on;
firstTransitionBandStartIndex = find(magnitude(h) > -40, 1);
firstTransitionBandEndIndex = find(magnitude(h) > -3, 1);
secondTransitionBandStartIndex = firstTransitionBandEndIndex + find( magnitude(h(firstTransitionBandEndI
secondTransitionBandEndIndex = firstTransitionBandEndIndex + find( magnitude(h(firstTransitionBandEndInd

121
122
123
124
125

firstTransitionBandStart = w(firstTransitionBandStartIndex)/pi;
firstTransitionBandEnd = w(firstTransitionBandEndIndex)/pi;
secondTransitionBandStart = w(secondTransitionBandStartIndex)/pi;
secondTransitionBandEnd = w(secondTransitionBandEndIndex)/pi;

126
127
128
129
130
131

redline([firstTransitionBandStart firstTransitionBandStart], [-300, 100]);


redline([firstTransitionBandEnd firstTransitionBandEnd],[-300, 100]);
redline([secondTransitionBandStart secondTransitionBandStart], [-300, 100]);
redline([secondTransitionBandEnd secondTransitionBandEnd], [-300, 100]);
hold off;

132
133
134
135
136
137
138

% Phase
subplot(2, 1, 2);
plot(w/pi, phi)
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Phase (degrees));

139
140
141

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-bandpass-plot.pdf

142
143
144
145
146
147
148
149

% In close magnitude
figure(2)
plot(w(firstTransitionBandEndIndex:secondTransitionBandStartIndex)/pi, magnitude(h(firstTransitionBandEn
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Bandpass Filter Response);

150
151
152

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-bandpass-close-up-plot.p

153
154
155
156
157
158

%% Print coefficients
fprintf(lowpass = )
fprintf([);
fprintf(%0.20f ,, low);
fprintf(\n);

159

28

160
161
162
163

fprintf(highpass = )
fprintf([);
fprintf(%0.20f, , high);
fprintf(\n);

164
165
166
167
168
169

fprintf(bandpass = )
fprintf([);
fprintf(%0.20f ,, bpass);
fprintf(\n);

Figure 7.1: Matlab code used to generate FIR filter coecients and plots
in its entirety.
1
2
3
4
5

%% Preliminary shit
ytexlabel = @(str) ylabel(str, interpreter, latex, FontUnits, points, FontSize, 16);
xtexlabel = @(str) xlabel(str, interpreter, latex, FontUnits, points, FontSize, 16);
textitle = @(str) title(str, interpreter, latex, FontUnits, points, FontSize, 20);
redline = @(x,y) plot(x, y, :r);

6
7

magnitude = @(h) 20*log(abs(h));

8
9
10

%% Low Pass Filter (cutoff w = 0.22)


bl = fir1(48,[0.23]);

11
12
13

[h,w] = freqz(bl, 1, 512);


phi = phasez(bl, 1, 512);

14
15
16

% Plot
figure(1);

17
18
19
20
21
22
23
24

% Magnitude
subplot(2, 1, 1);
plot(w/pi, magnitude(h))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Lowpass Filter Response);

25
26
27
28
29
30
31

hold on;
transitionBandStart = w( find(magnitude(h) < -3, 1))/pi;
transitionBandEnd = w( find(magnitude(h) < -40, 1))/pi;
redline([transitionBandStart transitionBandStart], [-300, 100]);
redline([transitionBandEnd transitionBandEnd],[-300, 100]);
hold off;

32
33
34
35
36
37
38

% Phase
subplot(2, 1, 2);
plot(w/pi, phi)
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Phase (degrees));

39

29

40
41

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-plot.pdf

42
43
44
45
46
47
48
49

% In close magnitude
figure(2)
plot(w(1:103)/pi, magnitude(h(1:103)))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Lowpass Filter Response);

50
51
52

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-close-up-plot.pd

53
54
55

%% High Pass Filter (cutoff w =0.57)


bh = fir1(44,0.54,high)

56
57
58

[h, w] = freqz(bh, 1, 512);


phi = phasez(bh, 1, 512);

59
60
61

% Plot
figure(1);

62
63
64
65
66
67
68
69

% Magnitude
subplot(2, 1, 1);
plot(w/pi, magnitude(h))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Highpass Filter Response);

70
71
72
73
74
75
76

hold on;
transitionBandStart = w( find( magnitude(h) > -40, 1) )/pi;
transitionBandEnd = w( find( magnitude(h) > -3, 1) )/pi;
redline([transitionBandStart transitionBandStart], [-300, 100]);
redline([transitionBandEnd transitionBandEnd],[-300, 100]);
hold off;

77
78
79
80
81
82
83

% Phase
subplot(2, 1, 2);
plot(w/pi, phi)
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Phase (degrees));

84
85
86

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-highpass-plot.pdf

87
88
89
90
91
92
93

% In close magnitude
figure(2)
plot(w( find(magnitude(h) > -3, 1) :end)/pi, magnitude(h( find(magnitude(h) > -3, 1) :end)))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));

30

94

textitle(Highpass Filter Response);

95
96
97

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-highpass-close-up-plot.p

98
99
100

%% Band Pass Filter (between wc =0.22 and 0.57 )


bb = fir1(43,[0.20 0.59],band);

101
102
103

[h,w] = freqz(bb, 1, 512);


phi = phasez(bb, 1, 512);

104
105
106

% Plot
figure(1);

107
108
109
110
111
112
113
114

% Magnitude
subplot(2, 1, 1);
plot(w/pi, magnitude(h))
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Magnitude (dB));
textitle(Bandpass Filter Response);

115
116
117
118
119
120

hold on;
firstTransitionBandStartIndex = find(magnitude(h) > -40, 1);
firstTransitionBandEndIndex = find(magnitude(h) > -3, 1);
secondTransitionBandStartIndex = firstTransitionBandEndIndex + find( magnitude(h(firstTransitionBandEndI
secondTransitionBandEndIndex = firstTransitionBandEndIndex + find( magnitude(h(firstTransitionBandEndInd

121
122
123
124
125

firstTransitionBandStart = w(firstTransitionBandStartIndex)/pi;
firstTransitionBandEnd = w(firstTransitionBandEndIndex)/pi;
secondTransitionBandStart = w(secondTransitionBandStartIndex)/pi;
secondTransitionBandEnd = w(secondTransitionBandEndIndex)/pi;

126
127
128
129
130
131

redline([firstTransitionBandStart firstTransitionBandStart], [-300, 100]);


redline([firstTransitionBandEnd firstTransitionBandEnd],[-300, 100]);
redline([secondTransitionBandStart secondTransitionBandStart], [-300, 100]);
redline([secondTransitionBandEnd secondTransitionBandEnd], [-300, 100]);
hold off;

132
133
134
135
136
137
138

% Phase
subplot(2, 1, 2);
plot(w/pi, phi)
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))
ytexlabel(Phase (degrees));

139
140
141

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-bandpass-plot.pdf

142
143
144
145
146
147

% In close magnitude
figure(2)
plot(w(firstTransitionBandEndIndex:secondTransitionBandStartIndex)/pi, magnitude(h(firstTransitionBandEn
grid on;
xtexlabel(Normalized Frequency ($\times\pi$ rad/sample))

31

148
149

ytexlabel(Magnitude (dB));
textitle(Bandpass Filter Response);

150
151
152

set(gcf, PaperPositionMode, auto);


% print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-bandpass-close-up-plot.p

153
154
155
156
157
158

%% Print coefficients
fprintf(lowpass = )
fprintf([);
fprintf(%0.20f ,, low);
fprintf(\n);

159
160
161
162
163

fprintf(highpass = )
fprintf([);
fprintf(%0.20f, , high);
fprintf(\n);

164
165
166
167
168
169

fprintf(bandpass = )
fprintf([);
fprintf(%0.20f ,, bpass);
fprintf(\n);

Figure 7.2: Matlab code used to generate IIR filter coecients and plots
in its entirety.

32

Potrebbero piacerti anche