-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathqf_filter.cpp
413 lines (310 loc) · 8.54 KB
/
qf_filter.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/***************************************************************************
qf_filter.cpp
-----------------
begin : Mon Jan 02 2006
copyright : (C) 2006 by Vincent Habchi, F5RCS
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "qf_common.h"
#include "qf_poly.h"
#include "qf_comp.h"
#include "qf_capacity.h"
#include "qf_filter.h"
#undef _QF_FILTER_DEBUG
// Constructors
// Just initialize a few things
qf_filter::qf_filter (qf_spec* Psp):
Pspec (Psp),
E (0.0, 0.0, 1.0),
F (0.0, 0.0, 1.0),
P (0.0, 0.0, 1.0),
BN (0.0, 0.0, 1.0),
BD (0.0, 0.0, 1.0),
BNc (0.0, 0.0, 1.0),
BDc (0.0, 0.0, 1.0),
lcmp ()
{}
// Copy ????
qf_filter::qf_filter (const qf_filter &FL) :
Pspec (FL. Pspec),
E (FL.E),
F (FL.F),
P (FL.P),
BN (FL.BN),
BD (FL.BD),
BNc (FL.BNc),
BDc (FL.BDc),
lcmp (FL.lcmp)
{}
// Out of polynoms P and F, calculate the susceptance B1 = BN/BD
// Of the filter
void qf_filter::scptX1 (qf_poly & F,
qf_poly & P,
qf_poly & BN,
qf_poly & BD) {
// Computes Hurwitz polynomial E
// E(x)E(-x) = F(x)F(-x) + P(x)P(-x)
qf_poly FH (F. hsq ());
qf_poly PH (P. hsq ());
#ifdef _QF_FILTER_DEBUG
FH.disp ("FH");
PH.disp ("PH");
#endif
E = FH + PH;
E. hurw ();
#ifdef _QF_FILTER_DEBUG
E. disp ("E");
#endif
qf_poly EE (E. even ());
qf_poly EO (E. odd ());
qf_poly FE (F. even ());
qf_poly FO (F. odd ());
// BN must be of the same order as E
BN = EO + FO;
if (BN. deg () != E. deg ()) {
BN = EE + FE;
BD = EO - FO;
return;
}
BD = EE - FE;
}
// Out of polynoms E and F, calculate the susceptance B1 = BN/BD
// Of the filter
void qf_filter::scptX2 (qf_poly & E,
qf_poly & F,
qf_poly & BN,
qf_poly & BD) {
qf_poly EE (E. even ());
qf_poly EO (E. odd ());
qf_poly FE (F. even ());
qf_poly FO (F. odd ());
// BN must be of the same order as E
BN = EO + FO;
if (BN. deg () != E. deg ()) {
BN = EE + FE;
BD = EO - FO;
return;
}
BD = EE - FE;
}
// Out of polynoms P and E, calculate the susceptance B1 = BN/BD
// Of the filter
void qf_filter::scptX3 (qf_poly & E,
qf_poly & P,
qf_poly & BN,
qf_poly & BD) {
// Computes Hurwitz polynomial F
// F(x)F(-x) = E(x)E(-x) - P(x)P(-x)
qf_poly EH (E. hsq ());
qf_poly PH (P. hsq ());
#ifdef _QF_FILTER_DEBUG
EH. disp ("EH");
PH. disp ("PH");
#endif
F = EH - PH;
F. hurw ();
#ifdef _QF_FILTER_DEBUG
F. disp ("F");
#endif
qf_poly EE (E. even ());
qf_poly EO (E. odd ());
qf_poly FE (F. even ());
qf_poly FO (F. odd ());
// BN must be of the same order as E
BN = EO + FO;
if (BN. deg () != E. deg ()) {
BN = EE + FE;
BD = EO - FO;
return;
}
BD = EE - FE;
}
// Extraction routines
// Extract finite attenuation pole (first version)
// Result is a shunt cap to ground, and a serial L||C
// Or, dual, a serial ind and a shunt L+C
bool qf_filter::pole_finite (qf_double_t p,
qf_double_t Ws,
qf_trp* trp) {
// Partial removal of infinite pole (first // cap)
// c = [B(s)/s] (s^2 = - W^2)
unsigned BDd = BD.deg ();
unsigned BNd = BN.deg ();
bool is_neg = false;
assert ((BDd + BNd) % 2 != 0);
qf_double_t pl = -p * p; // s^2 = - W^2
qf_double_t v;
if (BDd % 2 == 0) {
v = ((BN << 1).evalX2 (pl)) / (BD.evalX2 (pl)); // BN/s (s^2) / BD (s^2)
}
else
v = BN.evalX2 (pl) / ((BD >> 1).evalX2 (pl)); // BN / sBD
is_neg = (v < 0);
trp -> v = v * Ws;
#ifdef _QF_FILTER_DEBUG
std::cout << "Cp or Ls = " << v * Ws << '\n';
#endif
qf_poly BDS (BD >> 1); // BD * s
BDS *= v; // c * BD * s
BN -= BDS; // BN = BN - c * BD * s.
#ifdef _QF_FILTER_DEBUG
BN.disp("BN");
#endif
// BN is divisible by s^2+W^2
Cplx pc (0, p); // Pole z = jW
BN.div (pc);
// Full removal of finite pole
if (BDd % 2 == 0)
v = (BN >> 1).evalX2 (pl) / BD.evalX2 (pl); // c1 = s * BN' / BD
else
v = BN.evalX2 (pl) / (BD << 1).evalX2 (pl);
is_neg = (v < 0) || is_neg; // Must remember if v is already true
trp -> w1 = v * Ws;
trp -> w2 = - Ws / (v * pl);
#ifdef _QF_FILTER_DEBUG
std::cout << "C(s) = " << trp -> w1 << '\n';
std::cout << "L(s) = " << trp -> w2 << '\n';
#endif
// 1/B = 1/B - (s/c1) / (s^2 + W^2)
qf_poly BNS (BN >> 1);
BNS *= (1 / v);
BD -= BNS;
if (BD. deg () == 0) // Removal of a final pole
return is_neg;
pc = Cplx (0, p);
BD.div (pc);
#ifdef _QF_FILTER_DEBUG
BN.disp ("BN");
BD.disp ("BD");
#endif
return is_neg; // True if we have generated a negative comp
}
// Extract a single pole at infinity (shunt C)
bool qf_filter::pole_inf_c (qf_double_t& v, qf_double_t Ws) {
// C = B(s) / s, s = INF
if (BD. pk () == 0)
v = BN. pk ();
else {
assert (BN.deg () == (BD.deg () + 1));
v = BN.pk () / BD.pk ();
}
#ifdef _QF_FILTER_DEBUG
std::cout << "C(p) = " << v << endl;
#endif
unsigned d = BN. deg ();
qf_poly BDSc ((BD >> 1) * v);
BN -= BDSc; // B(s) = B(s) - C * s
if (BN. deg () == d) BN. zerod (); // Cancels roundoff errors
#ifdef _QF_FILTER_DEBUG
BN.disp ("BN'");
BD.disp ("BD");
#endif
v *= Ws;
return (v < 0);
}
// Extract a single pole at infinity (serial L)
bool qf_filter::pole_inf_l (qf_double_t& v, qf_double_t Ws) {
// L = X(s) / s, s = INF
if (BN. pk () == 0)
v = BD. pk ();
else {
assert (BD.deg () == (BN.deg () + 1));
v = BD.pk () / BN.pk ();
}
#ifdef _QF_FILTER_DEBUG
std::cout << "L(s) = " << v << endl;
#endif
unsigned d = BD. deg ();
qf_poly BNSc ((BN >> 1) * v);
BD -= BNSc; // B(s) = B(s) - C * s
if (BD. deg () == d) BD. zerod ();
#ifdef _QF_FILTER_DEBUG
BN.disp ("BN'");
BD.disp ("BD");
#endif
v *= Ws;
return (v < 0);
}
// Extract a single pole at zero (serial C)
bool qf_filter::pole_zero_c (qf_double_t& v, qf_double_t Ws) {
// C = B(s) / s, s = 0
assert (BD. val () == (BN. val () - 1));
v = BN. lnt () / BD. lnt ();
#ifdef _QF_FILTER_DEBUG
std::cout << "C(s) = " << v << endl;
#endif
unsigned d = BD. val ();
BD = ((BD >> 1) * v);
BD -= BN; // BD' = v * s * BD - BN
BN = (BN >> 1) * v; // BN' = v * s * BN
if (BD. val () == d + 1) BN. zerov (); // Cancels roundoff errors
#ifdef _QF_FILTER_DEBUG
BN.disp ("BN'");
BD.disp ("BD'");
#endif
v *= Ws;
return (v < 0);
}
// Extract a single pole at zero (shunt L)
bool qf_filter::pole_zero_l (qf_double_t& v, qf_double_t Ws) {
// L = X(s) / s, s = 0
assert (BN. val () == (BD. val () - 1));
v = BD. lnt () / BN. lnt ();
#ifdef _QF_FILTER_DEBUG
std::cout << "L(s) = " << v << endl;
#endif
unsigned d = BN. val ();
BN = ((BN >> 1) * v);
BN -= BD; // BD' = v * s * BD - BN
BD = (BD >> 1) * v; // BN' = v * s * BN
if (BN. val () == d + 1) BD. zerov (); // Cancels roundoff errors
#ifdef _QF_FILTER_DEBUG
BN.disp ("BN'");
BD.disp ("BD'");
#endif
v *= Ws;
return (v < 0);
}
// General synthesis routine for ladder filters
// These all poles filters are made of serial ind and shunt cap.
// n is the order of the filter
// s is a scale factor, useful in case of Bessel filters
bool qf_filter::synth_ladder (qf_double_t s) {
qf_double_t val;
unsigned n = Pspec -> ord;
if (! Pspec -> dual) {
while (n) {
pole_inf_c (val, 1);
lcmp. insert (new qf_cap (val / s, true, true));
n --;
if (n == 0) break;
pole_inf_l (val, 1);
lcmp. insert (new qf_ind (val / s, false, false));
n --;
}
}
else {
while (n) {
pole_inf_c (val, 1);
lcmp. insert (new qf_ind (val / s, false, false));
n --;
if (n == 0) break;
pole_inf_l (val, 1);
lcmp. insert (new qf_cap (val / s, true, true));
n --;
}
}
lcmp. insert (new qf_end ());
return true;
}