Skip to content

Commit 8e0e999

Browse files
authored
Merge pull request #23 from mike919192/optimizeRTVersion
Optimize runtime configurable version
2 parents 934457d + 14b5541 commit 8e0e999

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

include/sdsp/casc2orderIIR.h

+50-15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ class casc2orderIIR {
2525
static_assert(M % 2 == 0, "M must be even!");
2626
}
2727

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+
2836
template <typename Iter>
2937
void Process(Iter begin, Iter end)
3038
{
@@ -54,7 +62,7 @@ class casc2orderIIR {
5462
uint j;
5563

5664
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);
5866

5967
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);
6068
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 {
111119
double alpha1{ (0.5 - beta1) * t / 2.0 };
112120
double alpha2{ (0.5 - beta2) * t / 2.0 };
113121

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;
116126
bCoeff.at(2 * k).at(1) = 0;
117127
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;
120130

121131
aCoeff.at(2 * k).at(0) = 1;
122132
aCoeff.at(2 * k + 1).at(0) = 1;
@@ -141,11 +151,13 @@ class casc2orderIIR {
141151

142152
double beta1{ (1 - t) / dnm / 2 };
143153
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;
145157

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;
149161

150162
aCoeff.at(k).at(0) = 1;
151163
aCoeff.at(k).at(1) = -2 * gamma1;
@@ -169,9 +181,11 @@ class casc2orderIIR {
169181
double gamma1{ (0.5 + beta1) * std::cos(e0) };
170182
double alpha1{ (0.5 + beta1 - gamma1) / 4 };
171183

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;
175189

176190
aCoeff.at(k).at(0) = 1;
177191
aCoeff.at(k).at(1) = -2 * gamma1;
@@ -182,14 +196,17 @@ class casc2orderIIR {
182196
// preload the filter memory for steady state input equal to value parameter
183197
void PreloadFilter(double value)
184198
{
199+
double preload_value = value * gain;
185200
std::array<std::array<double, 3>, M + 1> memVals{ 0 };
186201
for (int i = 0; i < 3; i++) {
187-
memVals.at(0).at(i) = value;
202+
memVals.at(0).at(i) = preload_value;
188203
}
189204
if (fType == FilterType::LowPass) {
190205
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);
191208
for (uint i = 0; i < 3; i++) {
192-
memVals.at(j).at(i) = value;
209+
memVals.at(j).at(i) = preload_value;
193210
}
194211
}
195212
}
@@ -254,6 +271,12 @@ class casc_2o_IIR_lp : casc_2o_IIR<M> {
254271
static_assert(M % 2 == 0, "M must be even!");
255272
}
256273

274+
void copy_coeff_from(casc_2o_IIR_lp<M> otherFilter)
275+
{
276+
this->gain = otherFilter.gain;
277+
this->aCoeff = otherFilter.aCoeff;
278+
}
279+
257280
template <typename Iter>
258281
void process(Iter begin, Iter end)
259282
{
@@ -306,6 +329,12 @@ class casc_2o_IIR_hp : casc_2o_IIR<M> {
306329
static_assert(M % 2 == 0, "M must be even!");
307330
}
308331

332+
void copy_coeff_from(casc_2o_IIR_hp<M> otherFilter)
333+
{
334+
this->gain = otherFilter.gain;
335+
this->aCoeff = otherFilter.aCoeff;
336+
}
337+
309338
template <typename Iter>
310339
void process(Iter begin, Iter end)
311340
{
@@ -358,6 +387,12 @@ class casc_2o_IIR_bp : casc_2o_IIR<M> {
358387
static_assert(M % 2 == 0, "M must be even!");
359388
}
360389

390+
void copy_coeff_from(casc_2o_IIR_bp<M> otherFilter)
391+
{
392+
this->gain = otherFilter.gain;
393+
this->aCoeff = otherFilter.aCoeff;
394+
}
395+
361396
template <typename Iter>
362397
void process(Iter begin, Iter end)
363398
{
@@ -431,4 +466,4 @@ class casc_2o_IIR_bp : casc_2o_IIR<M> {
431466
}
432467
}
433468
};
434-
}
469+
}

0 commit comments

Comments
 (0)