eecs 152b lab report 3
TRANSCRIPT
-
8/18/2019 EECS 152B Lab Report 3
1/33
EECS 152B/CSE 135B
DSP Design & LaboratoryAssignment 3
Implement FIR and IIR Filters using TI c6713 DSK
Milan Patel (15888131)Ali Saad (19141859)
Dacoda Strack (56925997)
Lab Section 3AUniversity of California, Irvine
-
8/18/2019 EECS 152B Lab Report 3
2/33
Problem 1
The objective of this problem is to implement an FIR lowpass filter with the dsk board.The filter should have a cutoff frequency of 1760 Hz.
itm:questions-fir-lowpassThe following code starts off by declaring an array lowpass[] which has the filter coefficientsfound by the matlab code found in i.. It then begins polling and fills an input vector (of thesame 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 coefficients
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 rightmostposition
1 #include "dsk6713_aic23.h"
2 #include "dsk6713_led.h"
3
4 #define DSK6713_AIC23_INPUT_MIC 0x0015
5 #define DSK6713_AIC23_INPUT_LINEIN 0x0011
6
7 #define FILTER_LENGTH 49
8
9 Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
10 Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
11
12
lowpass[FILTER_LENGTH] = {-0.00081696436698336546, -0.00021751423510184715,
13 0.00066640936656965611, 0.00161979322029948370, 0.00214225259628533000,
14 0.00157034732804796000, -0.00047558977253344320, -0.00354590407634487650,
15 -0.00615075099554396580, -0.00624411669947530590, -0.00237847116307618370,
16 0.00500021450790641420, 0.01295156806914578300, 0.01680622510945733700,
17 0.01232212944474056100, -0.00158902124438583140, -0.02095792775291762900,
18 -0.03696902393001178000, -0.03873070207107721900, -0.01779006866361691800,
19 0.02746970212542162300, 0.08966015914615578500, 0.15377710387149290000,
20 0.20195759380955880000, 0.21984511275197313000, 0.20195759380955880000,
21 0.15377710387149290000, 0.08966015914615578500, 0.02746970212542162300,
22 -0.01779006866361691800, -0.03873070207107721900, -0.03696902393001178000,
23 -0.02095792775291762900, -0.00158902124438583140, 0.01232212944474056100,
24 0.01680622510945733700, 0.01295156806914578300, 0.00500021450790641420,
25
-0.00237847116307618370, -0.00624411669947530590, -0.00615075099554396580,26 -0.00354590407634487650, -0.00047558977253344320, 0.00157034732804796000,
27 0.00214225259628533000, 0.00161979322029948370, 0.00066640936656965611,
28 -0.00021751423510184715, -0.00081696436698336546};
29
30 i = 0;
31 j = 0;
32 z = 0;
33
34 main()
1
-
8/18/2019 EECS 152B Lab Report 3
3/33
35 {
36 comm_poll();
37
38 input[FILTER_LENGTH] = {0};
39 sum = 0;
40 b;
41
42 /* Fill input vector */
43 (z = 0; z < FILTER_LENGTH; z++){
44 input[z] = input_left_sample();
45 }
46
47
(1) {
48 /* Perform multiplication and sum */
49 (z = 0; z < FILTER_LENGTH; z++){
50 sum += lowpass[z]*input[FILTER_LENGTH-1-z];
51 }
52 x
53 output_left_sample(sum);
54 sum = 0;55
56 /* Shift input and enter input sample to x[0] */
57
(i = 0;i
-
8/18/2019 EECS 152B Lab Report 3
4/33
(a) 208 Hz (b) 1.
85 kHz
Figure 1.2: Output waveforms for the FIR lowpass filter resulting fromthe code given in Figure 1.1 for different pure sine frequencies
3
-
8/18/2019 EECS 152B Lab Report 3
5/33
Problem 2
The objective for this problem is to design a highpass filter with cutoff frequency 4560 Hz.
The code below is exactly the same as for Problem 1 save for the filter coefficient values.
1 #include "dsk6713_aic23.h"
2 #include "dsk6713_led.h"
3
4 #define DSK6713_AIC23_INPUT_MIC 0x0015
5 #define DSK6713_AIC23_INPUT_LINEIN 0x0011
6
7 #define FILTER_LENGTH 45
8
9 Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
10 Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
11
12 highpass[FILTER_LENGTH] = {0.00042641152461490183, 0.00112562953175404700,
13 -0.00092337645796045369, -0.00148575066233061150, 0.00208656645683152510,14 0.00193128457176655390, -0.00430111838849391110, -0.00191164049242499900,
15 0.00779811443426204570, 0.00063143422727044536, -0.01257176282544475100,
16 0.00293017932281575520, 0.01834265031540686200, -0.01009072458143068900,
17 -0.02457872732127334600, 0.02287737398573095500, 0.03057290797537401400,
18 -0.04575027393544489600, -0.03556318675588369300, 0.09462042004619715400,
19 0.03887105430451293100, -0.31455060652527644000, 0.46033558445336265000,
20 -0.31455060652527644000, 0.03887105430451293100, 0.09462042004619715400,
21 -0.03556318675588369300, -0.04575027393544489600, 0.03057290797537401400,
22 0.02287737398573095500, -0.02457872732127334600, -0.01009072458143068900,
23 0.01834265031540686200, 0.00293017932281575520, -0.01257176282544475100,
24 0.00063143422727044536, 0.00779811443426204570, -0.00191164049242499900,
25 -0.00430111838849391110, 0.00193128457176655390, 0.00208656645683152510,
26 -0.00148575066233061150, -0.00092337645796045369, 0.00112562953175404700,27 0.00042641152461490183};
28
29 i = 0;
30 j = 0;
31 z = 0;
32
33
main()
34 {
35 comm_poll();
36
37 input[FILTER_LENGTH] = {0};
38 sum = 0;
39 b;
40
41 /* Fill input vector */
42
(z = 0; z < FILTER_LENGTH; z++){
43 input[z] = input_left_sample();
44 }
45
46 (1) {
47 /* Perform multiplication and sum */
4
-
8/18/2019 EECS 152B Lab Report 3
6/33
48 (z = 0; z < FILTER_LENGTH; z++){
49 sum += lowpass[z]*input[FILTER_LENGTH-1-z];
50 }
51
52 output_left_sample(sum);
53 sum = 0;
54
55 /* Shift input and enter input sample to x[0] */
56 (i = 0;i
-
8/18/2019 EECS 152B Lab Report 3
7/33
Problem 3
The objective for this problem is to design a highpass filter with cutoff frequencies of 1760 Hzand 4560 Hz.
The code below is exactly the same as for Problem 1 save for the filter coefficient values.
1 #include "dsk6713_aic23.h"
2 #include "dsk6713_led.h"
3
4 #define DSK6713_AIC23_INPUT_MIC 0x0015
5 #define DSK6713_AIC23_INPUT_LINEIN 0x0011
6
7 #define FILTER_LENGTH 44
8
9 Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
10 Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
11
12 i = 0;13 j = 0;
14 z = 0;
15
16 bandpass[FILTER_LENGTH]= {0.000031804510880084353, -0.000019789648741247706,
17 -0.001124742421041582600, 0.002285005291701485100, 0.005281289897170116600,
18 0.000264852153473965080, -0.000662268802485741410, 0.004468372773721288400,
19 -0.007818726070629103100, -0.020803397305029305000, -0.002496698628516754200,
20 0.004500191811069565400, -0.013341181860725434000, 0.019690796960738364000,
21 0.062956242466695594000, 0.012430524442952817000, -0.019285086707876285000,
22 0.036841292313531136000, -0.052009560747888622000, -0.247111212040529190000,
23 -0.095827441561315896000, 0.312710665090100040000, 0.312710665090100040000,
24 -0.095827441561315896000, -0.247111212040529190000, -0.052009560747888622000,
25 0.036841292313531136000, -0.019285086707876285000, 0.012430524442952817000,
26 0.062956242466695594000, 0.019690796960738364000, -0.013341181860725434000,
27 0.004500191811069565400, -0.002496698628516754200, -0.020803397305029305000,
28 -0.007818726070629103100, 0.004468372773721288400, -0.000662268802485741410,
29 0.000264852153473965080, 0.005281289897170116600, 0.002285005291701485100,
30 -0.001124742421041582600, -0.000019789648741247706, 0.000031804510880084353};
31
32 main()
33 {
34 comm_poll();
35
36 input[FILTER_LENGTH] = {0};
37
sum = 0;38
b;
39
40 /* Fill input vector */
41 (z = 0; z < FILTER_LENGTH; z++){
42 input[z] = input_left_sample();
43 }
44
45 (1) {
46 /* Perform multiplication and sum */
6
-
8/18/2019 EECS 152B Lab Report 3
8/33
47 (z = 0; z < FILTER_LENGTH; z++){
48 sum += lowpass[z]*input[FILTER_LENGTH-1-z];
49 }
50 x
51 output_left_sample(sum);
52 sum = 0;
53
54 /* Shift input and enter input sample to x[0] */
55 (i = 0;i
-
8/18/2019 EECS 152B Lab Report 3
9/33
Problem 4
The objective of this problem is to implement the same filters specified in Problem 1, Problem2, 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 filtercoefficients uncommented at a time while changing N to fit the size of the filter we wereusing. In the snippet below, all three sets of coefficients are shown uncommented for clarity’ssake.
In any case, after defining N (the length of the given filter) and the filter coefficients properthe code declares input[] and output[] vectors to hold sections of the input and outputsignals of the filter of the same length as the filter.
After entering the while loop, it sums the convolution of the current input[] vector andthe b filter coefficients in order to get the first part of the output signal. It then sums theconvolution of the current output[] vector and the a filter coefficients and subtracts this
sum from the sum described above.
This floating-point difference 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 areplaced in their rightmost positions.
1 #include "dsk6713_aic23.h"
2 #include "dsk6713_led.h"
3
4 #define DSK6713_AIC23_INPUT_MIC 0x0015
5 #define DSK6713_AIC23_INPUT_LINEIN 0x0011
6
7 Uint32 fs = DSK6713_AIC23_FREQ_16KHZ; // 1
8 Uint16 inputsource = DSK6713_AIC23_INPUT_LINEIN; // 0x011
9
10 // Change length of filter and uncomment appropriate filter
11 #define N 15
12
13 i = 0;
14 j = 0;
15 z = 0;
16
17 /* Low pass Filter with length 8 */
18 b[N] = { 0.015495475408834581, -0.044995815898198468,
19
0.063486394663502116, -0.029432173299327589,20 -0.029432173299327756, 0.063486394663502185,
21 -0.044995815898198482, 0.015495475408834577};
22
23 a[N] = { 1.000000000000000000, - 5.36523779557625600,
24 13.245327837339437000, -19.24381272936014400,
25 17.684441942602113000, -10.25448614975332100,
26 3.475008624624797100, - 0.53213396812700375};
27
28 /* High Pass Filter with length 8 */
8
-
8/18/2019 EECS 152B Lab Report 3
10/33
29 b[N] = {0.049627511046376083, -0.049188451610409481,
30 0.138121314059458600, -0.139309244079199950,
31 0.139309244079199780, -0.138121314059458520,
32 0.049188451610409245, -0.049627511046375986};
33
34 a[N] = {1.000000000000000000, 2.306895654299675700,
35 4.422490037496873000, 5.049223959537021000,36 4.574298241591526100, 2.802757514457913300,
37 1.203775077617120300, 0.289193186820021040};
38
39 /* Band Pass Filter with length 15 */
40 b[N] = { 0.032327592148735554, -0.092434399595495334, 0.139335498243089440,
41 -0.182665720844533200, 0.235427846647907140, -0.202790003150475790,
42 0.089676279279297999, -0.000000000000000000, -0.089676279279297971,
43 0.202790003150476490, -0.235427846647908390, 0.182665720844534420,
44 -0.139335498243090470, 0.092434399595496028, -0.032327592148735811};
45
46 a[N] = { 1.000000000000000000, - 4.027423078991632500, 10.808766498459843000,
47 -21.188830934225550000, 34.523519894086988000, -46.620894233142970000,
48 54.470584082326944000, -54.546702929057446000, 47.782069693527120000,49 -35.774596361036920000, 23.131917894380116000, -12.332159299321756000,
50 5.419161525366078500, - 1.702254530803074800, 0.365819348914442730};
51
52 main()
53 {
54 comm_poll();
55
56 input[N] = {0};
57 output[N] = {0};
58
59 temp =0;
60
temp1 = 0;
61
62 out;
63 outs;
64
65 bs;
66
67 (1)
68 {
69 // Perform multiplication and sum with flipped input and b
70
(z = 0; z < N; z++)
71 {
72 temp += b[z]*input[N-1-z];
73
}74
75 // Perform multiplication and sum with flipped output and a
76 (z = 1; z < N; z++)
77 {
78 temp1 += output[N-z]*a[z];
79 }
80
81 // Subtract away feedback
82 out = temp-temp1;
9
-
8/18/2019 EECS 152B Lab Report 3
11/33
83
84 // Output to speakers
85 outs = (
)out;
86 output_left_sample(outs);
87
88 temp = 0; temp1 = 0; //reset sum variables for next cycle
89
90 // Shift input vector left (same as reversing input and shifting right)
91 (i = 0;i
-
8/18/2019 EECS 152B Lab Report 3
12/33
(a) Near 1.4802kHz
(b) Some higher frequency1
Figure 4.3: Output waveforms for the IIR lowpass filter resulting from thecode given in Figure 4.1 for different pure sine frequencies
(a) Some lower frequency2 (b) 4.95 kHz
Figure 4.4: Output waveforms for the IIR highpass filter resulting fromthe code given in Figure 4.1 for different pure sine frequencies
11
-
8/18/2019 EECS 152B Lab Report 3
13/33
(a) 2.
65 kHz (b) Some frequency3
Figure 4.5: Output waveforms for the IIR bandpass filter resulting fromthe code given in Figure 4.1 for different pure sine frequencies
12
-
8/18/2019 EECS 152B Lab Report 3
14/33
Questions
100mA 4 × 10−2 µΩ
x =
∫ ∞
−∞
ω(y) dy
1. Document the resulting parameters of the filters you design (i.e. filterorder, transition band frequencies, function used) & their associated code. Include the filter frequency plots using for the coefficientsused in all of the above problems. Plot the detailed passband and stopbandto demonstrate that the ripple requirements are met. Calculate the gainvalues at the test frequencies used and verify that these correspond to thaton an oscilloscope.
For the gain tables below, the theoretical gain is found in matlab and compared tothe 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 inputsignal itself. For the signal not in the passband, the gain is calculated by
gain = 20 log
signal amplitude
passband signal amplitude
dB
As noted in Footnote 1, we didn’t record the frequencies of any of the input signals notin the IIR filters’ passbands. Because of this, there are no gain tabulations in thosefilters’ 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 fir1()Filter order 48Transition bands ω/π ∈ (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 coefficients
13
-
8/18/2019 EECS 152B Lab Report 3
15/33
Frequency (kHz) ω (×π rad/sample) Theo. Gain (dB) Exp. Gain (dB)0.208 0.026 0.033 0.0001.850 0.231 −14.066 −18.196
Table 5.i.2: Theoretical and experimental gain values for the FIR lowpassfilter
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
M a g n i t u d e ( d B )
-300
-200
-100
0
100
Lowpass Filter Response
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
P h a s e ( d e g r e e s )
-25
-20
-15
-10
-5
0
Figure 5.i.2: Magnitude and phase responses for the FIR lowpass filter
Normalized Frequency (#: rad/sample)0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2
M a g n i t u d e ( d B )
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
0.5
Lowpass Filter Response
Figure 5.i.3: Close-up of magnitude response in the passband of the FIRlowpass filter
14
-
8/18/2019 EECS 152B Lab Report 3
16/33
ii. FIR Highpass Filter
Command used fir1()Filter order 48Transition bands ω/π ∈ (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 coeffi-cients
Frequency (kHz) ω (×π rad/sample) Theo. Gain (dB) Exp. Gain (dB)4.090 0.511 −32.615 −39.043
5.260 0.657 0.028 0.000
Table 5.ii.2: Theoretical and experimental gain values for the FIR high-pass filter
15
-
8/18/2019 EECS 152B Lab Report 3
17/33
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
M a g n i t u d e
( d B )
-300
-200
-100
0
100
Highpass Filter Response
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
P h a s e ( d e g r e e s )
-40
-30
-20
-10
0
10
Figure 5.ii.2: Magnitude and phase responses for the FIR highpass filter
Normalized Frequency (#: rad/sample)0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1
M a g n i t u d e ( d B )
-3
-2.5
-2
-1.5
-1
-0.5
0
0.5
Highpass Filter Response
Figure 5.ii.3: Close-up of magnitude response in the passband of the FIRhighpass filter
16
-
8/18/2019 EECS 152B Lab Report 3
18/33
iii. FIR Bandpass Filter
Command used fir1()Filter order 48Transition bands ω/π ∈ (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 coeffi-cients
Frequency (kHz) ω (×π rad/sample) Theo. Gain (dB) Exp. Gain (dB)1.289 0.161 −43.016 −33.172
4.120 0.515 −0.026 0.000
Table 5.iii.2: Theoretical and experimental gain values for the FIR band-pass filter
17
-
8/18/2019 EECS 152B Lab Report 3
19/33
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
M a g n i t u d e
( d B )
-300
-200
-100
0
100
Bandpass Filter Response
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
P h a s e ( d e g r e e s )
-40
-30
-20
-10
0
10
Figure 5.iii.2: Magnitude and phase responses for the FIR bandpass filter
Normalized Frequency (#: rad/sample)0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6
M a g n i t u d e ( d B )
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
0.5
Bandpass Filter Response
Figure 5.iii.3: Close-up of magnitude response in the passband of the FIRbandpass filter
18
-
8/18/2019 EECS 152B Lab Report 3
20/33
iv. IIR Lowpass Filter
Command used ellip()Filter order 7Transition bands ω/π ∈ (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 coeffi-cients
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
M a g n i t u d e
( d B )
-300
-200
-100
0
100
IIR Lowpass Filter Response
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
P h a s e ( d e g r e e s )
-10
-8
-6
-4
-2
0
Figure 5.iv.2: Magnitude and phase responses for the IIR lowpass filter
19
-
8/18/2019 EECS 152B Lab Report 3
21/33
-
8/18/2019 EECS 152B Lab Report 3
22/33
v. IIR Highpass Filter
Command used ellip()Filter order 7Transition bands ω/π ∈ (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 coeffi-cients
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
M a g n i t u d e
( d B )
-200
-150
-100
-50
0
50
IIR Highpass Filter Response
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
P h a s e ( d e g r e e s )
0
2
4
6
8
10
Figure 5.v.2: Magnitude and phase responses for the IIR highpass filter
21
-
8/18/2019 EECS 152B Lab Report 3
23/33
Normalized Frequency (#: rad/sample)0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1
M a g n i t u d e ( d B )
-2.5
-2
-1.5
-1
-0.5
0
IIR Highpass Filter Response
Figure 5.v.3: Close-up of magnitude response in the passband of the IIRhighpass filter
22
-
8/18/2019 EECS 152B Lab Report 3
24/33
vi. IIR Bandpass Filter
Command used ellip()Filter order 7Transition bands ω/π ∈ (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 coeffi-cients
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
M a g n i t u d e
( d B )
-200
-150
-100
-50
0
50
Bandpass Filter Response
Normalized Frequency (#: rad/sample)0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
P h a s e ( d e g r e e s )
-10
-5
0
5
10
Figure 5.vi.2: Magnitude and phase responses for the IIR bandpass filter
23
-
8/18/2019 EECS 152B Lab Report 3
25/33
Normalized Frequency (#: rad/sample)0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6
M a g n i t u d e ( d B )
-25
-20
-15
-10
-5
0
Bandpass Filter Response
Figure 5.vi.3: Close-up of magnitude response in the passband of the IIRbandpass filter
24
-
8/18/2019 EECS 152B Lab Report 3
26/33
Conclusion
Through the course of this lab, we learned how filters can be implemented and convolutionsperformed using c code, along with how to generate the filter coefficients for FIR and IIRlow-, high- and bandpass filters using standard matlab functions.
While our experimental values seem to differ from our theoretical ones a bit, this likely hasto do with our failing to record the original input signal amplitude.
25
-
8/18/2019 EECS 152B Lab Report 3
27/33
Code
1 %% Preliminary shit
2 ytexlabel = @(str) ylabel(str, ’interpreter’, ’latex’, ’FontUnits’, ’points’, ’FontSize’, 16);
3 xtexlabel = @(str) xlabel(str, ’interpreter’, ’latex’, ’FontUnits’, ’points’, ’FontSize’, 16);
4
textitle = @(str) title(str, ’interpreter’, ’latex’, ’FontUnits’, ’points’, ’FontSize’, 20);5 redline = @(x,y) plot(x, y, ’:r’);
6
7 magnitude = @(h) 20*log(abs(h));
8
9 %% Low Pass Filter (cutoff w = 0.22)
10 bl = fir1(48,[0.23]);
11
12 [h,w] = freqz(bl, 1, 512);
13 phi = phasez(bl, 1, 512);
14
15 % Plot
16 figure(1);
17
18 % Magnitude
19 subplot(2, 1, 1);
20 plot(w/pi, magnitude(h))
21 grid on;
22 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
23 ytexlabel(’Magnitude (dB)’);
24 textitle(’Lowpass Filter Response’);
25
26 hold on;
27 transitionBandStart = w( find(magnitude(h) < -3, 1))/pi;
28 transitionBandEnd = w( find(magnitude(h) < -40, 1))/pi;
29 redline([transitionBandStart transitionBandStart], [-300, 100]);
30 redline([transitionBandEnd transitionBandEnd],[-300, 100]);31 hold off;
32
33 % Phase
34 subplot(2, 1, 2);
35 plot(w/pi, phi)
36 grid on;
37 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
38 ytexlabel(’Phase (degrees)’);
39
40 set(gcf, ’PaperPositionMode’, ’auto’);
41 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-plot.pdf
42
43 % In close magnitude44 figure(2)
45 plot(w(1:103)/pi, magnitude(h(1:103)))
46 grid on;
47 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
48 ytexlabel(’Magnitude (dB)’);
49 textitle(’Lowpass Filter Response’);
50
51 set(gcf, ’PaperPositionMode’, ’auto’);
26
-
8/18/2019 EECS 152B Lab Report 3
28/33
52 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-close-up
53
54 %% High Pass Filter (cutoff w =0.57)
55 bh = fir1(44,0.54,’high’)
56
57 [h, w] = freqz(bh, 1, 512);
58 phi = phasez(bh, 1, 512);59
60 % Plot
61 figure(1);
62
63 % Magnitude
64 subplot(2, 1, 1);
65 plot(w/pi, magnitude(h))
66 grid on;
67 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
68 ytexlabel(’Magnitude (dB)’);
69 textitle(’Highpass Filter Response’);
70
71 hold on;72 transitionBandStart = w( find( magnitude(h) > -40, 1) )/pi;
73 transitionBandEnd = w( find( magnitude(h) > -3, 1) )/pi;
74 redline([transitionBandStart transitionBandStart], [-300, 100]);
75 redline([transitionBandEnd transitionBandEnd],[-300, 100]);
76 hold off;
77
78 % Phase
79 subplot(2, 1, 2);
80 plot(w/pi, phi)
81 grid on;
82 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
83 ytexlabel(’Phase (degrees)’);
84
85 set(gcf, ’PaperPositionMode’, ’auto’);
86 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-highpass-plot.pd
87
88 % In close magnitude
89 figure(2)
90 plot(w( find(magnitude(h) > -3, 1) : )/pi, magnitude(h( find(magnitude(h) > -3, 1) : )))
91 grid on;
92 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
93 ytexlabel(’Magnitude (dB)’);
94 textitle(’Highpass Filter Response’);
95
96
set(gcf, ’PaperPositionMode’, ’auto’);97 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-highpass-close-u
98
99 %% Band Pass Filter (between wc =0.22 and 0.57 )
100 bb = fir1(43,[0.20 0.59],’band’);
101
102 [h,w] = freqz(bb, 1, 512);
103 phi = phasez(bb, 1, 512);
104
105 % Plot
27
-
8/18/2019 EECS 152B Lab Report 3
29/33
106 figure(1);
107
108 % Magnitude
109 subplot(2, 1, 1);
110 plot(w/pi, magnitude(h))
111 grid on;
112 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)113 ytexlabel(’Magnitude (dB)’);
114 textitle(’Bandpass Filter Response’);
115
116 hold on;
117 firstTransitionBandStartIndex = find(magnitude(h) > -40, 1);
118 firstTransitionBandEndIndex = find(magnitude(h) > -3, 1);
119 secondTransitionBandStartIndex = firstTransitionBandEndIndex + find( magnitude(h(firstTransition
120 secondTransitionBandEndIndex = firstTransitionBandEndIndex + find( magnitude(h(firstTransitionBa
121
122 firstTransitionBandStart = w(firstTransitionBandStartIndex)/pi;
123 firstTransitionBandEnd = w(firstTransitionBandEndIndex)/pi;
124 secondTransitionBandStart = w(secondTransitionBandStartIndex)/pi;
125 secondTransitionBandEnd = w(secondTransitionBandEndIndex)/pi;126
127 redline([firstTransitionBandStart firstTransitionBandStart], [-300, 100]);
128 redline([firstTransitionBandEnd firstTransitionBandEnd],[-300, 100]);
129 redline([secondTransitionBandStart secondTransitionBandStart], [-300, 100]);
130 redline([secondTransitionBandEnd secondTransitionBandEnd], [-300, 100]);
131 hold off;
132
133 % Phase
134 subplot(2, 1, 2);
135 plot(w/pi, phi)
136 grid on;
137 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
138 ytexlabel(’Phase (degrees)’);
139
140 set(gcf, ’PaperPositionMode’, ’auto’);
141 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-bandpass-plot.pd
142
143 % In close magnitude
144 figure(2)
145 plot(w(firstTransitionBandEndIndex:secondTransitionBandStartIndex)/pi, magnitude(h(firstTransiti
146 grid on;
147 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
148 ytexlabel(’Magnitude (dB)’);
149 textitle(’Bandpass Filter Response’);
150
151 set(gcf, ’PaperPositionMode’, ’auto’);
152 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-bandpass-close-u
153
154 %% Print coefficients
155 fprintf(’lowpass = ’)
156 fprintf(’[’);
157 fprintf(’%0.20f ,’, low);
158 fprintf(’\n’);
159
28
-
8/18/2019 EECS 152B Lab Report 3
30/33
160 fprintf(’highpass = ’)
161 fprintf(’[’);
162 fprintf(’%0.20f, ’, high);
163 fprintf(’\n’);
164
165
166 fprintf(’bandpass = ’)167 fprintf(’[’);
168 fprintf(’%0.20f ,’, bpass);
169 fprintf(’\n’);
Figure 7.1: Matlab code used to generate FIR filter coefficients and plotsin its entirety.
1 %% Preliminary shit
2 ytexlabel = @(str) ylabel(str, ’interpreter’, ’latex’, ’FontUnits’, ’points’, ’FontSize’, 16);
3 xtexlabel = @(str) xlabel(str, ’interpreter’, ’latex’, ’FontUnits’, ’points’, ’FontSize’, 16);
4 textitle = @(str) title(str, ’interpreter’, ’latex’, ’FontUnits’, ’points’, ’FontSize’, 20);
5 redline = @(x,y) plot(x, y, ’:r’);6
7 magnitude = @(h) 20*log(abs(h));
8
9 %% Low Pass Filter (cutoff w = 0.22)
10 bl = fir1(48,[0.23]);
11
12 [h,w] = freqz(bl, 1, 512);
13 phi = phasez(bl, 1, 512);
14
15 % Plot
16 figure(1);
17
18 % Magnitude19 subplot(2, 1, 1);
20 plot(w/pi, magnitude(h))
21 grid on;
22 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
23 ytexlabel(’Magnitude (dB)’);
24 textitle(’Lowpass Filter Response’);
25
26 hold on;
27 transitionBandStart = w( find(magnitude(h) < -3, 1))/pi;
28 transitionBandEnd = w( find(magnitude(h) < -40, 1))/pi;
29 redline([transitionBandStart transitionBandStart], [-300, 100]);
30 redline([transitionBandEnd transitionBandEnd],[-300, 100]);
31 hold off;32
33 % Phase
34 subplot(2, 1, 2);
35 plot(w/pi, phi)
36 grid on;
37 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
38 ytexlabel(’Phase (degrees)’);
39
29
-
8/18/2019 EECS 152B Lab Report 3
31/33
40 set(gcf, ’PaperPositionMode’, ’auto’);
41 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-plot.pdf
42
43 % In close magnitude
44 figure(2)
45 plot(w(1:103)/pi, magnitude(h(1:103)))
46 grid on;47 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
48 ytexlabel(’Magnitude (dB)’);
49 textitle(’Lowpass Filter Response’);
50
51 set(gcf, ’PaperPositionMode’, ’auto’);
52 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-lowpass-close-up
53
54 %% High Pass Filter (cutoff w =0.57)
55 bh = fir1(44,0.54,’high’)
56
57 [h, w] = freqz(bh, 1, 512);
58 phi = phasez(bh, 1, 512);
59
60 % Plot
61 figure(1);
62
63 % Magnitude
64 subplot(2, 1, 1);
65 plot(w/pi, magnitude(h))
66 grid on;
67 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
68 ytexlabel(’Magnitude (dB)’);
69 textitle(’Highpass Filter Response’);
70
71 hold on;
72 transitionBandStart = w( find( magnitude(h) > -40, 1) )/pi;
73 transitionBandEnd = w( find( magnitude(h) > -3, 1) )/pi;
74 redline([transitionBandStart transitionBandStart], [-300, 100]);
75 redline([transitionBandEnd transitionBandEnd],[-300, 100]);
76 hold off;
77
78 % Phase
79 subplot(2, 1, 2);
80 plot(w/pi, phi)
81 grid on;
82 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
83 ytexlabel(’Phase (degrees)’);
84
85 set(gcf, ’PaperPositionMode’, ’auto’);
86 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-highpass-plot.pd
87
88 % In close magnitude
89 figure(2)
90 plot(w( find(magnitude(h) > -3, 1) : )/pi, magnitude(h( find(magnitude(h) > -3, 1) : )))
91 grid on;
92 xtexlabel(’Normalized Frequency ($\times\pi$ rad/sample)’)
93 ytexlabel(’Magnitude (dB)’);
30
-
8/18/2019 EECS 152B Lab Report 3
32/33
-
8/18/2019 EECS 152B Lab Report 3
33/33
148 ytexlabel(’Magnitude (dB)’);
149 textitle(’Bandpass Filter Response’);
150
151 set(gcf, ’PaperPositionMode’, ’auto’);
152 % print -dpdf /home/dacoda/Documents/school/dsp-design-152b/lab/lab-3/plots/fir-bandpass-close-u
153
154 %% Print coefficients155 fprintf(’lowpass = ’)
156 fprintf(’[’);
157 fprintf(’%0.20f ,’, low);
158 fprintf(’\n’);
159
160 fprintf(’highpass = ’)
161 fprintf(’[’);
162 fprintf(’%0.20f, ’, high);
163 fprintf(’\n’);
164
165
166 fprintf(’bandpass = ’)
167 fprintf(’[’);168 fprintf(’%0.20f ,’, bpass);
169 fprintf(’\n’);
Figure 7.2: Matlab code used to generate IIR filter coefficients and plotsin its entirety.