-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase_5.m
164 lines (123 loc) · 3.9 KB
/
Phase_5.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
%% Running FIR on all the windows
% Initializations
clear all; clc; close all;
M = 20;
Ts = 1;
% r = {80, 100} cheb
% beta = {0, 10} kaiser
% L = {0, 3} lanczos
% alfa = {0, 100%} tukey
r = 89;
L = 1.5;
beta = 8;
alfa = 0.65;
wn_param = [r, beta, L, alfa];
[w, titles] = getWindowName();
%% Filter response M = 20
wc = 0.05 * pi;
M = 20;
plotWindowFIRResponse(w, titles, M, wc, Ts, wn_param);
% Having a small wc, closer to 0 means we're pushing the oscilations in the
% stopband of the filter
%% Filter response M = 20
wc = 0.5 * pi;
M = 20;
plotWindowFIRResponse(w, titles, M, wc, Ts, wn_param);
%% Filter response M = 20
wc = 0.90 * pi;
M = 20;
plotWindowFIRResponse(w, titles, M, wc, Ts, wn_param);
% Having a higher wc, closer to pi means the oscilations from the stopband
% go to the passband
%% Ranking Filters
clc; clear all; close all;
type = 'low'
if strcmp(type, 'low')
wp = 0.5 * pi;
ws = 0.7 * pi;
else
wp = 0.7 * pi;
ws = 0.5 * pi;
end
Ts = 1/44100;
% Passband and stopband frequency
Fp = wp/(2*pi*Ts);
Fs = ws/(2*pi*Ts);
% The variables below are in %'s 1 = 1%
deltaP = 4;
deltaS = 4;
% 15% => boxcar of order 17
% Window parameters
r = 29;
L = 1.1;
beta = 2;
alfa = 0.85;
wn_param = [r, beta, L, alfa];
% Set filter max order
M = 20;
[Wn, titles] = getWindowName();
[rankingStruct, tableRanking, err] = ....
rankFilters(Fp, Fs, deltaP, deltaS, Ts, Wn, wn_param, M, type);
disp(tableRanking);
%% GAIN
figure;
for i = 1 : length(rankingStruct)
subplot(3,3,i);
Wn = rankingStruct(i).windowName;
Wn = [upper(Wn(1)) Wn(2:end)];
[H, omega] = freqz(rankingStruct(i).b, 1, 2048);
plotGain(H, omega, [Wn ' frequency response '....
'Order = ' num2str(rankingStruct(i).filterOrder)]);
if strcmp(type, 'low')
line([0, wp], [1+deltaP/100,1+deltaP/100],....
'Color', 'c', 'LineWidth', 1.5);
text(wp/2, 1+deltaP/50, '1 + \Delta_p');
line([0, wp], [1-deltaP/100,1-deltaP/100],....
'Color', 'c', 'LineWidth', 1.5);
text(wp/2, 1-deltaP/50, '1 - \Delta_p');
line([ws, pi], [deltaS/100,deltaS/100],....
'Color', 'm', 'LineWidth', 1.5);
text((pi + ws)/2, deltaS/50, '\Delta_s');
else
line([wp, pi], [1+deltaP/100,1+deltaP/100],....
'Color', 'c', 'LineWidth', 1.5);
text((pi+wp)/2, 1+deltaP/50, '1 + \Delta_p');
line([wp, pi], [1-deltaP/100,1-deltaP/100],....
'Color', 'c', 'LineWidth', 1.5);
text((pi+wp)/2, 1-deltaP/50, '1 - \Delta_p');
line([0, ws], [deltaS/100,deltaS/100],....
'Color', 'm', 'LineWidth', 1.5);
text(ws/2, deltaS/50, '\Delta_s');
end
if ~rankingStruct(i).validResult
title([Wn ' DOES NOT SATISFY CONSTRAINTS']);
end
end
%% DB GAIN
figure;
for i = 1 : length(rankingStruct)
subplot(3,3,i);
Wn = rankingStruct(i).windowName;
Wn = [upper(Wn(1)) Wn(2:end)];
[H, omega] = freqz(rankingStruct(i).b, 1, 2048);
plotGainDB(H, omega, [Wn ' frequency response in dB']);
legend(['Order = ' num2str(rankingStruct(i).filterOrder)]);
if ~rankingStruct(i).validResult
title([Wn ' DOES NOT SATISFY CONSTRAINTS']);
end
end
%% Phase
% The phase is LINEAR
% TO CHECK CALL: islinphase(rankingStruct(1).b, 123)
figure;
for i = 1 : length(rankingStruct)
subplot(3,3,i);
Wn = rankingStruct(i).windowName;
Wn = [upper(Wn(1)) Wn(2:end)];
[H, omega] = freqz(rankingStruct(i).b, 1, 2048);
plotPhase(H, omega, [Wn ' frequency response phase']);
legend(['Order = ' num2str(rankingStruct(i).filterOrder)]);
if ~rankingStruct(i).validResult
title([Wn ' DOES NOT SATISFY CONSTRAINTS']);
end
end