@@ -25,6 +25,14 @@ class casc2orderIIR {
25
25
static_assert (M % 2 == 0 , " M must be even!" );
26
26
}
27
27
28
+ void copy_coeff_from (casc2orderIIR<M> otherFilter)
29
+ {
30
+ gain = otherFilter.gain ;
31
+ bCoeff = otherFilter.bCoeff ;
32
+ aCoeff = otherFilter.aCoeff ;
33
+ fType = otherFilter.fType ;
34
+ }
35
+
28
36
template <typename Iter>
29
37
void Process (Iter begin, Iter end)
30
38
{
@@ -54,7 +62,7 @@ class casc2orderIIR {
54
62
uint j;
55
63
56
64
for (j = 0 ; j < M; j++) {
57
- y.at (j + 1 ).at (p) = y.at (j).at (p) * b. at (j). at ( 0 ) ;
65
+ y.at (j + 1 ).at (p) = y.at (j).at (p);
58
66
59
67
y.at (j + 1 ).at (p) += y.at (j).at (d1) * b.at (j).at (j1 ) - y.at (j + 1 ).at (d1) * a.at (j).at (j1 );
60
68
y.at (j + 1 ).at (p) += y.at (j).at (d2) * b.at (j).at (j2) - y.at (j + 1 ).at (d2) * a.at (j).at (j2);
@@ -111,12 +119,14 @@ class casc2orderIIR {
111
119
double alpha1{ (0.5 - beta1) * t / 2.0 };
112
120
double alpha2{ (0.5 - beta2) * t / 2.0 };
113
121
114
- bCoeff.at (2 * k).at (0 ) = 2 * alpha1;
115
- bCoeff.at (2 * k + 1 ).at (0 ) = 2 * alpha2;
122
+ gain *= 4 * alpha1 * alpha2;
123
+
124
+ bCoeff.at (2 * k).at (0 ) = 1.0 ;
125
+ bCoeff.at (2 * k + 1 ).at (0 ) = 1.0 ;
116
126
bCoeff.at (2 * k).at (1 ) = 0 ;
117
127
bCoeff.at (2 * k + 1 ).at (1 ) = 0 ;
118
- bCoeff.at (2 * k).at (2 ) = -bCoeff. at ( 2 * k). at ( 0 ) ;
119
- bCoeff.at (2 * k + 1 ).at (2 ) = -bCoeff. at ( 2 * k + 1 ). at ( 0 ) ;
128
+ bCoeff.at (2 * k).at (2 ) = -1.0 ;
129
+ bCoeff.at (2 * k + 1 ).at (2 ) = -1.0 ;
120
130
121
131
aCoeff.at (2 * k).at (0 ) = 1 ;
122
132
aCoeff.at (2 * k + 1 ).at (0 ) = 1 ;
@@ -141,11 +151,13 @@ class casc2orderIIR {
141
151
142
152
double beta1{ (1 - t) / dnm / 2 };
143
153
double gamma1{ (0.5 + beta1) * std::cos (e0 ) };
144
- double alpha1{ (0.5 + beta1 + gamma1) / 4 }; // -
154
+ double alpha1{ (0.5 + beta1 + gamma1) / 4 };
155
+
156
+ gain *= 2 * alpha1;
145
157
146
- bCoeff.at (k).at (0 ) = 2 * alpha1 ;
147
- bCoeff.at (k).at (1 ) = -2 * bCoeff. at (k). at ( 0 ); // -
148
- bCoeff.at (k).at (2 ) = bCoeff. at (k). at ( 0 ) ;
158
+ bCoeff.at (k).at (0 ) = 1.0 ;
159
+ bCoeff.at (k).at (1 ) = -2.0 ;
160
+ bCoeff.at (k).at (2 ) = 1.0 ;
149
161
150
162
aCoeff.at (k).at (0 ) = 1 ;
151
163
aCoeff.at (k).at (1 ) = -2 * gamma1;
@@ -169,9 +181,11 @@ class casc2orderIIR {
169
181
double gamma1{ (0.5 + beta1) * std::cos (e0 ) };
170
182
double alpha1{ (0.5 + beta1 - gamma1) / 4 };
171
183
172
- bCoeff.at (k).at (0 ) = 2 * alpha1;
173
- bCoeff.at (k).at (1 ) = 2 * bCoeff.at (k).at (0 );
174
- bCoeff.at (k).at (2 ) = bCoeff.at (k).at (0 );
184
+ gain *= 2 * alpha1;
185
+
186
+ bCoeff.at (k).at (0 ) = 1.0 ;
187
+ bCoeff.at (k).at (1 ) = 2.0 ;
188
+ bCoeff.at (k).at (2 ) = 1.0 ;
175
189
176
190
aCoeff.at (k).at (0 ) = 1 ;
177
191
aCoeff.at (k).at (1 ) = -2 * gamma1;
@@ -182,14 +196,17 @@ class casc2orderIIR {
182
196
// preload the filter memory for steady state input equal to value parameter
183
197
void PreloadFilter (double value)
184
198
{
199
+ double preload_value = value * gain;
185
200
std::array<std::array<double , 3 >, M + 1 > memVals{ 0 };
186
201
for (int i = 0 ; i < 3 ; i++) {
187
- memVals.at (0 ).at (i) = value ;
202
+ memVals.at (0 ).at (i) = preload_value ;
188
203
}
189
204
if (fType == FilterType::LowPass) {
190
205
for (uint j = 1 ; j < M + 1 ; j++) {
206
+ preload_value /= 1 + aCoeff.at (j - 1 ).at (1 ) + aCoeff.at (j - 1 ).at (2 );
207
+ preload_value *= bCoeff.at (j - 1 ).at (0 ) + bCoeff.at (j - 1 ).at (1 ) + bCoeff.at (j - 1 ).at (2 );
191
208
for (uint i = 0 ; i < 3 ; i++) {
192
- memVals.at (j).at (i) = value ;
209
+ memVals.at (j).at (i) = preload_value ;
193
210
}
194
211
}
195
212
}
@@ -254,6 +271,12 @@ class casc_2o_IIR_lp : casc_2o_IIR<M> {
254
271
static_assert (M % 2 == 0 , " M must be even!" );
255
272
}
256
273
274
+ void copy_coeff_from (casc_2o_IIR_lp<M> otherFilter)
275
+ {
276
+ this ->gain = otherFilter.gain ;
277
+ this ->aCoeff = otherFilter.aCoeff ;
278
+ }
279
+
257
280
template <typename Iter>
258
281
void process (Iter begin, Iter end)
259
282
{
@@ -306,6 +329,12 @@ class casc_2o_IIR_hp : casc_2o_IIR<M> {
306
329
static_assert (M % 2 == 0 , " M must be even!" );
307
330
}
308
331
332
+ void copy_coeff_from (casc_2o_IIR_hp<M> otherFilter)
333
+ {
334
+ this ->gain = otherFilter.gain ;
335
+ this ->aCoeff = otherFilter.aCoeff ;
336
+ }
337
+
309
338
template <typename Iter>
310
339
void process (Iter begin, Iter end)
311
340
{
@@ -358,6 +387,12 @@ class casc_2o_IIR_bp : casc_2o_IIR<M> {
358
387
static_assert (M % 2 == 0 , " M must be even!" );
359
388
}
360
389
390
+ void copy_coeff_from (casc_2o_IIR_bp<M> otherFilter)
391
+ {
392
+ this ->gain = otherFilter.gain ;
393
+ this ->aCoeff = otherFilter.aCoeff ;
394
+ }
395
+
361
396
template <typename Iter>
362
397
void process (Iter begin, Iter end)
363
398
{
@@ -431,4 +466,4 @@ class casc_2o_IIR_bp : casc_2o_IIR<M> {
431
466
}
432
467
}
433
468
};
434
- }
469
+ }
0 commit comments