-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankFilters.m
206 lines (181 loc) · 7.06 KB
/
rankFilters.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
% FILE: rankFilters.m
%
% FUNCTION: rankFilters
%
% CALL: [rankingFilters, tableRank, error] =....
% rankFilters(Fp, Fs, deltaP, deltaS, Ts, Wn, wn_param, M, type)
%
% Returns the best filters with passband frequency Fp and stopband frequency Fs,
% with sampling period Ts with order less than M and which respects the passband
% deltaP and stopband deltaS constraints for a all 9 windows
%
%
% INPUTS:
% Fp - passband frequency
% Fs - stopband frequency
% deltaP - passband constraint
% deltaS - stopband constraint
% Ts - sampling period
% Wn - window names, a vector with the names for the used
% windows
% wn_param - window extra parameters for the windows
% M - max order for the designed filter
% type - low or high pass filters
%
% VALUES FOR Wn must be one of those bellow:
% boxcar
% triang
% blackman
% chebwin
% hamming
% hanning
% kaiser
% lanczos
% tukeywin
%
%
% OUTPUTS:
% rankingFilters - filter orderd by order and the sum of
% the errors
% tableRank - filter orderd by the above criteria and
% returned as a table
% error - will be used in a future release
%
%
% USES:
% getWindow
% FIR
% checkError
% returnMaxError
% truncateCellArray
% filterDesign
%
% Author: Leonard-Gabriel Necula
% Created: December 24 2020
% Updated: January 18 2021
function [rankingFilters, tableRank, error] =....
rankFilters(Fp, Fs, deltaP, deltaS, Ts, Wn, wn_param, M, type)
error = cell(20, 1); % maximum number of errors
errorIndex = 1; % error index starts at 1
if nargin < 1
Fp = 1000; % Setting Fp to 1000 Hz
error{errorIndex} = ['Filter pass frequency was not set. ' ....
'Setting Fp = 1000 Hz'];
errorIndex = errorIndex + 1;
elseif isempty(Fp)
Fp = 1000; % Setting Fp to 1000 Hz
error{errorIndex} = ['Filter pass frequency was left empty. ' ....
'Setting Fp = 1000 Hz'];
errorIndex = errorIndex + 1;
end
if nargin < 2
Fs = 1.1 * Fp; % Setting Fp to 1000 Hz
error{errorIndex} = ['Filter stop band frequency was not set. ' ....
'Setting Fs = 1.1 * Fp '];
errorIndex = errorIndex + 1;
elseif isempty(Fs)
Fs = 1.1 * Fp; % Setting Fp to 1000 Hz
error{errorIndex} = ['Filter stop band frequency was left empty. ' ....
'Setting Fs = 1.1 * Fp'];
errorIndex = errorIndex + 1;
end
if nargin < 3
deltaP = 5;
error{errorIndex} = ['Filter passband error was not set. ' ....
'Setting deltaP = 5% '];
errorIndex = errorIndex + 1;
elseif isempty(deltaP)
deltaP = 5;
error{errorIndex} = ['Filter passband error was left empty. ' ....
'Setting deltaP = 5% '];
errorIndex = errorIndex + 1;
end
if nargin < 4
deltaS = 5;
error{errorIndex} = ['Filter stopband error was not set. ' ....
'Setting deltaS = 5% '];
errorIndex = errorIndex + 1;
elseif isempty(deltaS)
deltaS = 5;
error{errorIndex} = ['Filter stopband error was left empty. ' ....
'Setting deltaS = 5% '];
errorIndex = errorIndex + 1;
end
if nargin < 5
Ts = 2/(2.1 * (Fs + Fp));
error{errorIndex} = ['Filter sampling period was not set. ' ....
'Setting Ts = 2/(2.1 *(Fs + Fp)) '];
errorIndex = errorIndex + 1;
elseif isempty(Ts)
Ts = 2/(2.1 * (Fs + Fp));
error{errorIndex} = ['Filter sampling period was left empty. ' ....
'Setting Ts = 2/(2.1 *(Fs + Fp)) '];
errorIndex = errorIndex + 1;
end
if nargin < 6
Wn = getWindowName();
error{errorIndex} = ['Filter windows were not set. ' ....
'Setting Wn = all 9 windows '];
errorIndex = errorIndex + 1;
elseif isempty(Wn)
Wn = getWindowName();
error{errorIndex} = ['Filter windows were left empty. ' ....
'Setting Wn = all 9 windows '];
errorIndex = errorIndex + 1;
end
if nargin < 7
wn_param = setDefaultParamsForAllWindows(Wn);
error{errorIndex} = ['Filter windows extra parameters were not set. ' ....
'Setting wn_param = default values'];
errorIndex = errorIndex + 1;
elseif isempty(wn_param)
wn_param = setDefaultParamsForAllWindows(Wn);
error{errorIndex} = ['Filter windows extra parameters were left empty. ' ....
'Setting wn_param = default values '];
errorIndex = errorIndex + 1;
end
if nargin < 8
M = 512;
error{errorIndex} = ['Maximum order was not set. Set max order to 512'];
errorIndex = errorIndex + 1;
elseif isempty(M)
M = 512;
error{errorIndex} = ['Maximum order was left empty. Set max order to 512'];
errorIndex = errorIndex + 1;
end
if nargin < 9
type = 'low';
end
if isempty(type)
type = 'low';
end
% ---------------------------------------------------------------------- %
error = truncateCellArray(error, errorIndex - 1);
param_index = 1;
rankingFilters = [];
for i = 1:numel(Wn)
if checkType(Wn{i}, [4, 7, 8, 9])
[b, a, filterOrder, deltaP_real, deltaS_real, error, found] = ...
filterDesign(Fp, Fs, deltaP, deltaS, Ts, Wn{i}, wn_param(param_index), M, type);
param_index = param_index + 1;
else
[b, a, filterOrder, deltaP_real, deltaS_real, error, found] = ...
filterDesign(Fp, Fs, deltaP, deltaS, Ts, Wn{i}, wn_param(param_index), M, type);
end
[H, w] = freqz(b, a, 2048);
currentFilter.b = b;
currentFilter.a = a;
currentFilter.windowName = Wn{i};
currentFilter.filterOrder = filterOrder;
currentFilter.deltaP = deltaP_real;
currentFilter.deltaS = deltaS_real;
currentFilter.error = error;
currentFilter.validResult = found;
currentFilter.sumErrors = deltaP_real + deltaS_real;
rankingFilters = [rankingFilters currentFilter];
end
tableRank = struct2table(rankingFilters);
tableRank = sortrows(tableRank, {'filterOrder', 'sumErrors'});
tableRank = tableRank(:, 1:end-1);
rankingFilters = table2struct(tableRank);
end