DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
FilterDesign.h
1// DSPark -- Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi -- MIT License
3
4#pragma once
5
54#include "Biquad.h"
55
56#include <algorithm>
57#include <array>
58#include <cmath>
59#include <complex>
60#include <numbers>
61
62namespace dspark {
63
70{
71 double b2 = 0.0, b1 = 0.0, b0 = 1.0;
72 double a2 = 0.0, a1 = 0.0, a0 = 1.0;
73
75 [[nodiscard]] double magnitudeSquared(double omega) const noexcept
76 {
77 const double w2 = omega * omega;
78 const double nr = b0 - b2 * w2, ni = b1 * omega;
79 const double dr = a0 - a2 * w2, di = a1 * omega;
80 return (nr * nr + ni * ni) / (dr * dr + di * di);
81 }
82
84 [[nodiscard]] double magnitudeDb(double hz) const noexcept
85 {
86 return 10.0 * std::log10(std::max(magnitudeSquared(2.0 * std::numbers::pi * hz), 1e-30));
87 }
88
90 [[nodiscard]] bool isSecondOrder() const noexcept { return a2 != 0.0; }
91
93 [[nodiscard]] double poleFrequency() const noexcept
94 {
95 const double omega = isSecondOrder() ? std::sqrt(std::abs(a0 / a2)) : std::abs(a0 / a1);
96 return omega / (2.0 * std::numbers::pi);
97 }
98
99 // -- Prototypes (the cookbook's analog forms) -------------------------------
100
102 [[nodiscard]] static AnalogSection lowPass(double hz, double q = 0.7071067811865476) noexcept
103 {
104 const double w = omegaOf(hz);
105 return make(0.0, 0.0, w * w, 1.0, w / safeQ(q), w * w);
106 }
107
109 [[nodiscard]] static AnalogSection highPass(double hz, double q = 0.7071067811865476) noexcept
110 {
111 const double w = omegaOf(hz);
112 return make(1.0, 0.0, 0.0, 1.0, w / safeQ(q), w * w);
113 }
114
116 [[nodiscard]] static AnalogSection bandPass(double hz, double q = 0.7071067811865476) noexcept
117 {
118 const double w = omegaOf(hz);
119 return make(0.0, w / safeQ(q), 0.0, 1.0, w / safeQ(q), w * w);
120 }
121
123 [[nodiscard]] static AnalogSection notch(double hz, double q = 0.7071067811865476) noexcept
124 {
125 const double w = omegaOf(hz);
126 return make(1.0, 0.0, w * w, 1.0, w / safeQ(q), w * w);
127 }
128
130 [[nodiscard]] static AnalogSection allPass(double hz, double q = 0.7071067811865476) noexcept
131 {
132 const double w = omegaOf(hz);
133 return make(1.0, -w / safeQ(q), w * w, 1.0, w / safeQ(q), w * w);
134 }
135
137 [[nodiscard]] static AnalogSection peak(double hz, double q, double gainDb) noexcept
138 {
139 const double w = omegaOf(hz), A = std::pow(10.0, gainDb / 40.0);
140 return make(1.0, w * A / safeQ(q), w * w, 1.0, w / (A * safeQ(q)), w * w);
141 }
142
144 [[nodiscard]] static AnalogSection lowShelf(double hz, double gainDb, double slope = 1.0) noexcept
145 {
146 const double w = omegaOf(hz), A = std::pow(10.0, gainDb / 40.0), d = shelfDamping(A, slope);
147 return make(A, A * std::sqrt(A) * d * w, A * A * w * w, A, std::sqrt(A) * d * w, w * w);
148 }
149
151 [[nodiscard]] static AnalogSection highShelf(double hz, double gainDb, double slope = 1.0) noexcept
152 {
153 const double w = omegaOf(hz), A = std::pow(10.0, gainDb / 40.0), d = shelfDamping(A, slope);
154 return make(A * A, A * std::sqrt(A) * d * w, A * w * w, 1.0, std::sqrt(A) * d * w, A * w * w);
155 }
156
164 [[nodiscard]] static AnalogSection lowShelfQ(double hz, double gainDb, double q) noexcept
165 {
166 const double w = omegaOf(hz), A = std::pow(10.0, gainDb / 40.0), d = 1.0 / std::clamp(q, 0.05, 100.0);
167 return make(A, A * std::sqrt(A) * d * w, A * A * w * w, A, std::sqrt(A) * d * w, w * w);
168 }
169
171 [[nodiscard]] static AnalogSection highShelfQ(double hz, double gainDb, double q) noexcept
172 {
173 const double w = omegaOf(hz), A = std::pow(10.0, gainDb / 40.0), d = 1.0 / std::clamp(q, 0.05, 100.0);
174 return make(A * A, A * std::sqrt(A) * d * w, A * w * w, 1.0, std::sqrt(A) * d * w, A * w * w);
175 }
176
182 [[nodiscard]] static AnalogSection tiltSecondOrder(double hz, double gainDb, double q = 0.7071067811865476) noexcept
183 {
184 // A low shelf of -gainDb with half of gainDb back over the whole band.
185 const double w = omegaOf(hz), A = std::pow(10.0, -gainDb / 40.0), d = 1.0 / std::clamp(q, 0.05, 100.0);
186 return make(1.0, std::sqrt(A) * d * w, A * w * w, A, std::sqrt(A) * d * w, w * w);
187 }
188
190 [[nodiscard]] static AnalogSection firstOrderLowPass(double hz) noexcept
191 {
192 const double w = omegaOf(hz);
193 return make(0.0, 0.0, w, 0.0, 1.0, w);
194 }
195
197 [[nodiscard]] static AnalogSection firstOrderHighPass(double hz) noexcept
198 {
199 const double w = omegaOf(hz);
200 return make(0.0, 1.0, 0.0, 0.0, 1.0, w);
201 }
202
204 [[nodiscard]] static AnalogSection firstOrderLowShelf(double hz, double gainDb) noexcept
205 {
206 const double w = omegaOf(hz), root = std::pow(10.0, gainDb / 40.0);
207 return make(0.0, 1.0, w * root, 0.0, 1.0, w / root);
208 }
209
211 [[nodiscard]] static AnalogSection firstOrderHighShelf(double hz, double gainDb) noexcept
212 {
213 const double w = omegaOf(hz), root = std::pow(10.0, gainDb / 40.0);
214 return make(0.0, root, w, 0.0, 1.0 / root, w);
215 }
216
218 [[nodiscard]] static AnalogSection tilt(double hz, double gainDb) noexcept
219 {
220 const double w = omegaOf(hz), root = std::pow(10.0, gainDb / 40.0);
221 return make(0.0, root, w, 0.0, 1.0, w * root);
222 }
223
225 [[nodiscard]] static AnalogSection make(double b2, double b1, double b0,
226 double a2, double a1, double a0) noexcept
227 {
229 s.b2 = b2; s.b1 = b1; s.b0 = b0;
230 s.a2 = a2; s.a1 = a1; s.a0 = a0;
231 return s;
232 }
233
234private:
235 [[nodiscard]] static double omegaOf(double hz) noexcept { return 2.0 * std::numbers::pi * std::max(hz, 1e-3); }
236 [[nodiscard]] static double safeQ(double q) noexcept { return std::max(q, 1e-3); }
237 [[nodiscard]] static double shelfDamping(double A, double slope) noexcept
238 {
239 const double s = std::clamp(slope, 1e-3, 1.0);
240 return std::sqrt(std::max(0.0, (A + 1.0 / A) * (1.0 / s - 1.0) + 2.0));
241 }
242};
243
249{
250 static constexpr int kMaxSections = 16;
251
252 std::array<AnalogSection, kMaxSections> sections {};
253 int count = 0;
254
256 void add(const AnalogSection& section) noexcept
257 {
258 if (count < kMaxSections) sections[static_cast<size_t>(count++)] = section;
259 }
260
262 [[nodiscard]] double magnitudeSquared(double omega) const noexcept
263 {
264 double m = 1.0;
265 for (int i = 0; i < count; ++i) m *= sections[static_cast<size_t>(i)].magnitudeSquared(omega);
266 return m;
267 }
268
270 [[nodiscard]] double magnitudeDb(double hz) const noexcept
271 {
272 return 10.0 * std::log10(std::max(magnitudeSquared(2.0 * std::numbers::pi * hz), 1e-30));
273 }
274};
275
285{
286public:
287 static constexpr int kFitSteps = 2;
288
299 class Band
300 {
301 public:
302 static constexpr int kPoints = 16;
303 static constexpr double kTopShare = 20000.0 / 44100.0 + 1e-4;
304
305 Band() noexcept { prepare(48000.0); }
306 explicit Band(double sampleRate) noexcept { prepare(sampleRate); }
307
309 void prepare(double sampleRate) noexcept
310 {
311 fs_ = (std::isfinite(sampleRate) && sampleRate > 0.0) ? sampleRate : 48000.0;
312 highHz_ = std::min(kTopShare * fs_, std::max(20000.0, 0.42 * fs_));
313 lowHz_ = std::min(20.0, 0.01 * highHz_);
314 const double ratio = std::pow(highHz_ / lowHz_, 1.0 / (kPoints - 1));
315 double hz = lowHz_;
316 for (int i = 0; i < kPoints; ++i, hz *= ratio)
317 {
318 const double omega = 2.0 * std::numbers::pi * std::min(hz, highHz_);
319 omega2_[i] = omega * omega;
320 y_[i] = yOf(omega / fs_);
321 weight_[i] = weightOf(omega);
322 }
323 }
324
325 [[nodiscard]] double getSampleRate() const noexcept { return fs_; }
326 [[nodiscard]] double getLowHz() const noexcept { return lowHz_; }
327 [[nodiscard]] double getHighHz() const noexcept { return highHz_; }
328
329 private:
330 friend class FilterDesign;
331 double fs_ = 48000.0, lowHz_ = 20.0, highHz_ = 20000.0;
332 double omega2_[kPoints] {}; // squared angular frequency of each point (rad/s)
333 double y_[kPoints] {}; // 1 - cos of its digital frequency
334 double weight_[kPoints] {}; // how much its error counts
335 };
336
337 // -- One section ------------------------------------------------------------
338
347 [[nodiscard]] static BiquadCoeffs bilinear(const AnalogSection& section, double sampleRate, double exactHz) noexcept
348 {
349 const double c = (exactHz > 0.0 && exactHz < 0.49 * sampleRate)
350 ? 2.0 * kPi * exactHz / std::tan(kPi * exactHz / sampleRate)
351 : 2.0 * sampleRate;
352 const double c2 = c * c;
353 const double a0 = section.a2 * c2 + section.a1 * c + section.a0;
354 BiquadCoeffs out;
355 if (!(std::abs(a0) > 0.0)) return out;
356 const double inverse = 1.0 / a0;
357 out.b0 = (section.b2 * c2 + section.b1 * c + section.b0) * inverse;
358 out.b1 = 2.0 * (section.b0 - section.b2 * c2) * inverse;
359 out.b2 = (section.b2 * c2 - section.b1 * c + section.b0) * inverse;
360 out.a1 = 2.0 * (section.a0 - section.a2 * c2) * inverse;
361 out.a2 = (section.a2 * c2 - section.a1 * c + section.a0) * inverse;
362 return out;
363 }
364
387 static bool bilinearPolynomial(const double* analogNum, const double* analogDen, int order, double sampleRate,
388 double exactHz, double* digitalNum, double* digitalDen) noexcept
389 {
390 if (order < 1 || order > 16 || !(sampleRate > 0.0)) return false;
391 const double c = (exactHz > 0.0 && exactHz < 0.49 * sampleRate)
392 ? 2.0 * kPi * exactHz / std::tan(kPi * exactHz / sampleRate)
393 : 2.0 * sampleRate;
394 double num[17] = {}, den[17] = {};
395 double power = 1.0; // c^j
396 for (int j = 0; j <= order; ++j)
397 {
398 // The row (1 - z^-1)^j (1 + z^-1)^(order - j).
399 double row[17] = { 1.0 };
400 for (int r = 0; r < order; ++r)
401 {
402 const double sign = r < j ? -1.0 : 1.0;
403 for (int i = r + 1; i >= 1; --i) row[i] += sign * row[i - 1];
404 }
405 for (int i = 0; i <= order; ++i)
406 {
407 num[i] += analogNum[j] * power * row[i];
408 den[i] += analogDen[j] * power * row[i];
409 }
410 power *= c;
411 }
412 if (!(std::abs(den[0]) > 0.0)) return false;
413 const double inverse = 1.0 / den[0];
414 for (int i = 0; i <= order; ++i)
415 {
416 digitalNum[i] = num[i] * inverse;
417 digitalDen[i] = den[i] * inverse;
418 }
419 return true;
420 }
421
434 [[nodiscard]] static BiquadCoeffs digital(const AnalogSection& section, const Band& band) noexcept
435 {
436 const double fs = band.fs_;
437 if (!finite(section)) return BiquadCoeffs {};
438 if (!usable(section)) return bilinear(section, fs, section.poleFrequency());
439
440 const Shape shape = shapeOf(section, fs);
441 const Denominator start = invariantDenominator(section, fs);
442 if (shape.allPass)
443 {
444 BiquadCoeffs out;
445 if (shape.first) { out.b0 = start.a1; out.b1 = 1.0; out.b2 = 0.0; }
446 else { out.b0 = start.a2; out.b1 = start.a1; out.b2 = 1.0; }
447 out.a1 = start.a1; out.a2 = start.a2;
448 return out;
449 }
450
451 // Everything is fitted in t = y / y0, with y = 1 - cos w and y0 its
452 // value at the pole frequency: the terms of a section are then of the
453 // order of one whether its corner is at 20 Hz or next to Nyquist.
454 const double poleOmega = shape.first ? section.a0 / section.a1 : std::sqrt(section.a0 / section.a2);
455 const double y0 = yOf(std::clamp(poleOmega / fs, 1e-9, kPi));
456 Samples x;
457 gather(section, shape, band, y0, x);
458
459 const int nN = shape.numeratorTerms(), nD = shape.denominatorTerms();
460 double p[5] = {}; // numerator terms, then denominator terms
461 p[nN] = start.d1 * y0 / start.d0;
462 if (nD > 1) p[nN + 1] = start.d2 * y0 * y0 / start.d0;
463
464 // The numerator over the poles carried from the prototype.
465 double normal[5][6];
466 double cost = 0.0, gap = 0.0;
467 if (nN > 0)
468 {
469 pass(shape, x, p, false, normal, cost, gap);
470 if (!advance(shape, false, 1e-12, normal, p)) return bilinear(section, fs, section.poleFrequency());
471 clampNumerator(shape, x, p);
472 }
473
474 // Most sections are on the curve already.
475 double best[5] = {};
476 std::copy(p, p + 5, best);
477 double bestCost = 0.0;
478 pass(shape, x, p, true, nullptr, bestCost, gap);
479
480 // The rest: numerator and denominator together. Each pass measures
481 // the section it has and prepares the step to the next.
482 for (int i = 0; i < kFitSteps && gap > kClose; ++i)
483 {
484 pass(shape, x, p, true, normal, cost, gap);
485 if (i > 0)
486 {
487 if (!(cost < bestCost)) break;
488 const bool settled = bestCost - cost < kSettled * bestCost;
489 std::copy(p, p + 5, best);
490 bestCost = cost;
491 if (settled || !(gap > kClose)) break;
492 }
493 if (!advance(shape, true, 1e-6, normal, p)) break;
494 if (!positive(p[nN], nD > 1 ? p[nN + 1] : 0.0, x.tMax)) break;
495 clampNumerator(shape, x, p);
496 if (i + 1 == kFitSteps)
497 {
498 pass(shape, x, p, true, nullptr, cost, gap);
499 if (cost < bestCost) std::copy(p, p + 5, best);
500 }
501 }
502
503 BiquadCoeffs out;
504 if (!assemble(shape, x, best, y0, out)) return bilinear(section, fs, section.poleFrequency());
505 return out;
506 }
507
509 [[nodiscard]] static BiquadCoeffs digital(const AnalogSection& section, double sampleRate) noexcept
510 {
511 return digital(section, Band(sampleRate));
512 }
513
523 static int digital(const AnalogCascade& cascade, const Band& band, BiquadCoeffs* out, int capacity) noexcept
524 {
525 const int n = std::min(cascade.count, capacity);
526 for (int i = 0; i < n; ++i)
527 out[i] = digital(cascade.sections[static_cast<size_t>(i)], band);
528 return n;
529 }
530
532 static int digital(const AnalogCascade& cascade, double sampleRate, BiquadCoeffs* out, int capacity) noexcept
533 {
534 return digital(cascade, Band(sampleRate), out, capacity);
535 }
536
538 [[nodiscard]] static double magnitudeDb(const BiquadCoeffs* sections, int n, double hz, double sampleRate) noexcept
539 {
540 double db = 0.0;
541 for (int i = 0; i < n; ++i)
542 db += 20.0 * std::log10(std::max(sections[i].getMagnitude(hz, sampleRate), 1e-15));
543 return db;
544 }
545
546 // -- Cascades ---------------------------------------------------------------
547 //
548 // Each returns an analog prototype; digital() turns it into biquads.
549
550 static constexpr double kButterworthQ = 0.7071067811865476;
551 static constexpr double kMaxCutSlope = 96.0;
552 static constexpr int kMaxOrder = AnalogCascade::kMaxSections;
553
573 [[nodiscard]] static AnalogCascade highCut(double hz, double slopeDbPerOctave, double q = kButterworthQ) noexcept
574 {
575 AnalogCascade out = cutPrototype(slopeDbPerOctave, q);
576 const double omega = omegaOf(hz);
577 for (int i = 0; i < out.count; ++i) at(out, i) = scaled(at(out, i), omega);
578 return out;
579 }
580
582 [[nodiscard]] static AnalogCascade lowCut(double hz, double slopeDbPerOctave, double q = kButterworthQ) noexcept
583 {
584 AnalogCascade out = cutPrototype(slopeDbPerOctave, q);
585 const double omega = omegaOf(hz);
586 for (int i = 0; i < out.count; ++i) at(out, i) = mirrored(at(out, i), omega);
587 return out;
588 }
589
605 [[nodiscard]] static AnalogCascade lowShelf(double hz, double gainDb, double order, double q = kButterworthQ) noexcept
606 {
607 AnalogCascade out = shelfPrototype(gainDb, order, q);
608 const double omega = omegaOf(hz);
609 for (int i = 0; i < out.count; ++i) at(out, i) = scaled(at(out, i), omega);
610 return out;
611 }
612
614 [[nodiscard]] static AnalogCascade highShelf(double hz, double gainDb, double order, double q = kButterworthQ) noexcept
615 {
616 AnalogCascade out = shelfPrototype(gainDb, order, q);
617 const double omega = omegaOf(hz);
618 for (int i = 0; i < out.count; ++i) at(out, i) = mirrored(at(out, i), omega);
619 return out;
620 }
621
631 [[nodiscard]] static AnalogCascade bell(double hz, double q, double gainDb, double order) noexcept
632 {
633 AnalogCascade out;
634 const AnalogCascade proto = shelfPrototype(gainDb, order);
635 const double w0 = omegaOf(hz), width = w0 / std::clamp(q, 1e-3, 1e3);
636 for (int i = 0; i < proto.count; ++i)
637 {
638 const AnalogSection& s = proto.sections[static_cast<size_t>(i)];
639 if (!s.isSecondOrder())
640 {
641 out.add(AnalogSection::make(1.0, s.b0 * width, w0 * w0, 1.0, s.a0 * width, w0 * w0));
642 continue;
643 }
644 const BandPair num = bandPair(s.b1, s.b0, width, w0), den = bandPair(s.a1, s.a0, width, w0);
645 out.add(AnalogSection::make(1.0, num.upper1, num.upper0, 1.0, den.upper1, den.upper0));
646 out.add(AnalogSection::make(1.0, num.lower1, num.lower0, 1.0, den.lower1, den.lower0));
647 }
648 return out;
649 }
650
658 [[nodiscard]] static AnalogCascade bandPass(double hz, double q, double order) noexcept
659 {
660 return bandFilter(hz, q, order, false);
661 }
662
668 [[nodiscard]] static AnalogCascade bandStop(double hz, double q, double order) noexcept
669 {
670 return bandFilter(hz, q, order, true);
671 }
672
681 [[nodiscard]] static AnalogCascade allPass(double hz, double q, double order) noexcept
682 {
683 AnalogCascade out;
684 const double nu = wholeOrPart(order);
685 const double w = omegaOf(hz), w2 = w * w;
686 if (nu != std::floor(nu))
687 {
688 // Between whole orders: whole pairs at the angles of the order and
689 // one section with what is left, which turns the phase at the
690 // frequency by 90 degrees per order of its share. Under one order
691 // it is a real pole that comes in from far above; over one, a
692 // pair whose second pole comes in the same way while its damping
693 // goes to that of the pair of the next whole order.
694 const int pairs = static_cast<int>(std::floor(0.5 * nu));
695 const double rest = nu - 2.0 * pairs; // in (0, 2), not 1
696 const double sharpest = kButterworthQ / std::clamp(q, 0.1, 100.0);
697 for (int i = 0; i < pairs; ++i)
698 {
699 const double a1 = 2.0 * std::sin(kPi * (2 * i + 1) / (2.0 * nu)) * (i == 0 ? sharpest : 1.0) * w;
700 out.add(AnalogSection::make(1.0, -a1, w2, 1.0, a1, w2));
701 }
702 const double turn = std::tan(0.25 * kPi * rest);
703 if (rest < 1.0)
704 {
705 out.add(AnalogSection::make(0.0, -turn, w, 0.0, turn, w));
706 }
707 else
708 {
709 const double part = rest - 1.0;
710 const double narrow = 2.0 * std::sin(kPi * (2 * pairs + 1) / (2.0 * (2 * pairs + 2)));
711 double damping = std::pow(narrow, part);
712 if (pairs == 0) damping *= std::pow(sharpest, part);
713 const double second = std::clamp(1.0 - damping / turn, 0.0, 1.0);
714 out.add(AnalogSection::make(second, -damping * w, w2, second, damping * w, w2));
715 }
716 return out;
717 }
718 const int n = static_cast<int>(nu);
719 for (int i = 0; i < n / 2; ++i)
720 {
721 double damping = 2.0 * std::sin(kPi * (2 * i + 1) / (2.0 * n));
722 if (i == 0) damping *= kButterworthQ / std::clamp(q, 0.1, 100.0);
723 const double a1 = damping * w;
724 out.add(AnalogSection::make(1.0, -a1, w2, 1.0, a1, w2));
725 }
726 if (n % 2 == 1) out.add(AnalogSection::make(0.0, -1.0, w, 0.0, 1.0, w));
727 return out;
728 }
729
740 [[nodiscard]] static AnalogCascade tilt(double pivotHz, double slopeDbPerOctave) noexcept
741 {
742 AnalogCascade out;
743 if (!std::isfinite(slopeDbPerOctave)) return out;
744 const double order = std::clamp(slopeDbPerOctave / 6.020599913279624, -1.0, 1.0);
745 if (std::abs(order) < 1e-9) return out;
746 const double toZero = std::pow(kLadderStep, 0.5 * (1.0 - order));
747 const double toPole = std::pow(kLadderStep, 0.5 * (1.0 + order));
748 double cell = 2.0 * kPi * 632.4555320336759 / (kLadderStep * kLadderStep * kLadderStep * kLadderStep);
749 for (int i = 0; i < 4; ++i)
750 {
751 const double z1 = cell * toZero, p1 = cell * toPole, z2 = z1 * kLadderStep, p2 = p1 * kLadderStep;
752 out.add(AnalogSection::make(1.0 / (z1 * z2), 1.0 / z1 + 1.0 / z2, 1.0,
753 1.0 / (p1 * p2), 1.0 / p1 + 1.0 / p2, 1.0));
754 cell *= kLadderStep * kLadderStep;
755 }
756 const double level = out.magnitudeSquared(omegaOf(pivotHz));
757 if (level > 0.0 && std::isfinite(level))
758 {
759 const double gain = 1.0 / std::sqrt(level);
760 AnalogSection& first = at(out, 0);
761 first.b2 *= gain; first.b1 *= gain; first.b0 *= gain;
762 }
763 return out;
764 }
765
776 [[nodiscard]] static double tiltSlopeWithin(double pivotHz, double slopeDbPerOctave, double ceilingDb) noexcept
777 {
778 if (!std::isfinite(slopeDbPerOctave) || !std::isfinite(pivotHz) || !(ceilingDb > 0.0)) return slopeDbPerOctave;
779 const double pivot = std::clamp(pivotHz, 20.0, 20000.0);
780 const double octaves = slopeDbPerOctave > 0.0 ? std::log2(20000.0 / pivot) : std::log2(pivot / 20.0);
781 const double rise = std::abs(slopeDbPerOctave) * octaves;
782 return rise > ceilingDb ? slopeDbPerOctave * (ceilingDb / rise) : slopeDbPerOctave;
783 }
784
797 [[nodiscard]] static AnalogCascade ellipticHighCut(double hz, int order = 12, double rippleDb = 0.05,
798 double stopDb = 100.0) noexcept
799 {
800 AnalogCascade out = ellipticPrototype(order, rippleDb, stopDb);
801 const double omega = omegaOf(hz);
802 for (int i = 0; i < out.count; ++i) at(out, i) = scaled(at(out, i), omega);
803 return out;
804 }
805
807 [[nodiscard]] static AnalogCascade ellipticLowCut(double hz, int order = 12, double rippleDb = 0.05,
808 double stopDb = 100.0) noexcept
809 {
810 AnalogCascade out = ellipticPrototype(order, rippleDb, stopDb);
811 const double omega = omegaOf(hz);
812 for (int i = 0; i < out.count; ++i) at(out, i) = mirrored(at(out, i), omega);
813 return out;
814 }
815
821 [[nodiscard]] static double ellipticStopRatio(int order = 12, double rippleDb = 0.05, double stopDb = 100.0) noexcept
822 {
823 double ripple = 0.0, stop = 0.0;
824 return 1.0 / ellipticModulus(order, rippleDb, stopDb, ripple, stop);
825 }
826
827private:
828 static constexpr double kPi = std::numbers::pi;
829 static constexpr int kClusterPoints = 7;
830 static constexpr int kMaxPoints = Band::kPoints + 2 * kClusterPoints;
831 static constexpr double kFloor = 1e-8;
832 static constexpr double kClose = 5.3e-6;
833 static constexpr double kUltrasonicHz = 3000.0;
834 static constexpr double kSettled = 0.05;
835
837 struct Shape
838 {
839 int kind = 0;
840 bool first = false;
841 bool allPass = false;
842 double cosNotch = 0.0;
843 double yNotch = 0.0;
844
846 [[nodiscard]] int numeratorTerms() const noexcept
847 {
848 if (first) return 1;
849 return kind == 3 ? 0 : (kind == 2 ? 1 : 2);
850 }
851 [[nodiscard]] int denominatorTerms() const noexcept { return first ? 1 : 2; }
852 };
853
855 struct Denominator
856 {
857 double d0 = 1.0, d1 = 0.0, d2 = 0.0;
858 double a1 = 0.0, a2 = 0.0;
859 };
860
862 struct Samples
863 {
864 int n = 0;
865 double t[kMaxPoints] {};
866 double m[kMaxPoints] {};
867 double weight[kMaxPoints] {};
868 double tMax = 2.0;
869 double tz = 0.0;
870 double dc = 0.0;
871 double floor = 0.0;
872 };
873
879 [[nodiscard]] static double weightOf(double omega) noexcept
880 {
881 const double over = std::max(0.0, omega * (0.5 / kPi) - 20000.0) * (1.0 / kUltrasonicHz);
882 return 1.0 / (1.0 + over * over);
883 }
884
886 [[nodiscard]] static double yOf(double w) noexcept
887 {
888 const double s = std::sin(0.5 * w);
889 return 2.0 * s * s;
890 }
891
893 [[nodiscard]] static double sine(double x) noexcept
894 {
895 constexpr double k3 = 1.0 / 6.0, k5 = 1.0 / 20.0, k7 = 1.0 / 42.0, k9 = 1.0 / 72.0;
896 constexpr double k11 = 1.0 / 110.0, k13 = 1.0 / 156.0, k15 = 1.0 / 210.0;
897 const double x2 = x * x;
898 return x * (1.0 - x2 * k3 * (1.0 - x2 * k5 * (1.0 - x2 * k7 * (1.0 - x2 * k9
899 * (1.0 - x2 * k11 * (1.0 - x2 * k13 * (1.0 - x2 * k15)))))));
900 }
901
902 [[nodiscard]] static bool finite(const AnalogSection& h) noexcept
903 {
904 return std::isfinite(h.b2) && std::isfinite(h.b1) && std::isfinite(h.b0)
905 && std::isfinite(h.a2) && std::isfinite(h.a1) && std::isfinite(h.a0);
906 }
907
908 [[nodiscard]] static bool usable(const AnalogSection& h) noexcept
909 {
910 if (h.b2 == 0.0 && h.b1 == 0.0 && h.b0 == 0.0) return false;
911 if (h.isSecondOrder()) return h.a1 / h.a2 > 0.0 && h.a0 / h.a2 > 0.0;
912 return h.b2 == 0.0 && h.a1 != 0.0 && h.a0 / h.a1 > 0.0;
913 }
914
915 [[nodiscard]] static Shape shapeOf(const AnalogSection& h, double fs) noexcept
916 {
917 Shape s;
918 s.first = !h.isSecondOrder();
919 if (s.first)
920 {
921 if (h.b0 == 0.0) s.kind = 1;
922 s.allPass = h.b1 != 0.0 && h.b1 == -h.a1 && h.b0 == h.a0;
923 return s;
924 }
925 if (h.b0 == 0.0) s.kind = (h.b1 == 0.0) ? 2 : 1;
926 else if (h.b1 == 0.0 && h.b2 != 0.0 && h.b0 / h.b2 > 0.0)
927 {
928 const double w = std::sqrt(h.b0 / h.b2) / fs;
929 if (w < 0.98 * kPi)
930 {
931 s.kind = 3;
932 s.yNotch = yOf(w);
933 s.cosNotch = 1.0 - s.yNotch;
934 }
935 }
936 s.allPass = h.b1 != 0.0 && h.b2 == h.a2 && h.b0 == h.a0 && h.b1 == -h.a1;
937 return s;
938 }
939
946 [[nodiscard]] static Denominator invariantDenominator(const AnalogSection& h, double fs) noexcept
947 {
948 Denominator d;
949 if (!h.isSecondOrder())
950 {
951 const double x = -(h.a0 / h.a1) / fs;
952 const double p = std::exp(x), gap = -std::expm1(x);
953 d.d0 = gap * gap; d.d1 = 2.0 * p; d.d2 = 0.0;
954 d.a1 = -p; d.a2 = 0.0;
955 return d;
956 }
957 const double sigma = -0.5 * h.a1 / h.a2, det = sigma * sigma - h.a0 / h.a2;
958 double atNyquist = 0.0;
959 if (det < 0.0)
960 {
961 const double r = std::exp(sigma / fs), gap = -std::expm1(sigma / fs);
962 constexpr double kFree = 0.95 * kPi, kRoom = 0.04 * kPi;
963 double theta = std::sqrt(-det) / fs;
964 if (theta > kFree) theta = kFree + kRoom * (1.0 - std::exp(-(theta - kFree) / kRoom));
965 const double s = std::sin(0.5 * theta), bend = 4.0 * r * s * s;
966 const double atDc = gap * gap + bend, top = (1.0 + r) * (1.0 + r) - bend;
967 d.d0 = atDc * atDc;
968 atNyquist = top * top;
969 d.d2 = 4.0 * r * r;
970 d.a1 = -2.0 * r * std::cos(theta); d.a2 = r * r;
971 }
972 else
973 {
974 const double root = std::sqrt(det);
975 const double x1 = (sigma + root) / fs, x2 = (sigma - root) / fs;
976 const double p1 = std::exp(x1), p2 = std::exp(x2);
977 const double gap = std::expm1(x1) * std::expm1(x2), top = (1.0 + p1) * (1.0 + p2);
978 d.d0 = gap * gap;
979 atNyquist = top * top;
980 d.d2 = 4.0 * p1 * p2;
981 d.a1 = -(p1 + p2); d.a2 = p1 * p2;
982 }
983 d.d1 = 0.5 * (atNyquist - d.d0) - 2.0 * d.d2;
984 return d;
985 }
986
988 [[nodiscard]] static double power(const AnalogSection& h, double omega2) noexcept
989 {
990 const double nr = h.b0 - h.b2 * omega2, dr = h.a0 - h.a2 * omega2;
991 return (nr * nr + h.b1 * h.b1 * omega2) / (dr * dr + h.a1 * h.a1 * omega2);
992 }
993
999 static void cluster(const AnalogSection& h, const Band& band, double centre, double q, double y0, Samples& x) noexcept
1000 {
1001 const double half = 0.5 / std::max(q, 0.5);
1002 const double r = std::sqrt(1.0 + half * half) + half, s = std::sqrt(r);
1003 const double factors[kClusterPoints] = { 1.0 / (r * r), 1.0 / r, 1.0 / s, 1.0, s, r, r * r };
1004 const double low = 2.0 * kPi * band.lowHz_, high = 2.0 * kPi * band.highHz_;
1005 const double halfPeriod = 0.5 / band.fs_, scale = 1.0 / y0;
1006 for (const double ratio : factors)
1007 {
1008 const double omega = std::clamp(centre * ratio, low, high);
1009 const double sn = sine(omega * halfPeriod);
1010 x.t[x.n] = 2.0 * sn * sn * scale;
1011 x.m[x.n] = power(h, omega * omega);
1012 x.weight[x.n] = weightOf(omega);
1013 ++x.n;
1014 }
1015 }
1016
1017 static void gather(const AnalogSection& h, const Shape& shape, const Band& band, double y0, Samples& x) noexcept
1018 {
1019 const double scale = 1.0 / y0;
1020 x.n = Band::kPoints;
1021 for (int i = 0; i < Band::kPoints; ++i)
1022 {
1023 x.t[i] = band.y_[i] * scale;
1024 x.m[i] = power(h, band.omega2_[i]);
1025 x.weight[i] = band.weight_[i];
1026 }
1027 if (shape.first)
1028 {
1029 cluster(h, band, h.a0 / h.a1, 0.5, y0, x);
1030 if (h.b1 != 0.0 && h.b0 != 0.0) cluster(h, band, std::abs(h.b0 / h.b1), 0.5, y0, x);
1031 }
1032 else
1033 {
1034 cluster(h, band, std::sqrt(h.a0 / h.a2), std::sqrt(h.a0 * h.a2) / std::abs(h.a1), y0, x);
1035 if (shape.kind == 0)
1036 {
1037 if (h.b2 != 0.0 && h.b0 / h.b2 > 0.0 && h.b1 != 0.0)
1038 cluster(h, band, std::sqrt(h.b0 / h.b2), std::sqrt(h.b0 * h.b2) / std::abs(h.b1), y0, x);
1039 else if (h.b2 == 0.0 && h.b1 != 0.0)
1040 cluster(h, band, std::abs(h.b0 / h.b1), 0.5, y0, x);
1041 }
1042 }
1043 double largest = 0.0;
1044 for (int i = 0; i < x.n; ++i) largest = std::max(largest, x.m[i]);
1045 x.floor = kFloor * largest;
1046 x.tMax = 2.0 * scale;
1047 x.tz = shape.yNotch * scale;
1048 x.dc = (shape.kind == 0 || shape.kind == 3) ? (h.b0 / h.a0) * (h.b0 / h.a0) : 0.0;
1049 }
1050
1052 static void numeratorAt(const Shape& s, double t, double* out) noexcept
1053 {
1054 if (s.first) out[0] = t;
1055 else if (s.kind == 2) out[0] = t * t;
1056 else if (s.kind != 3) { out[0] = t; out[1] = t * t; }
1057 }
1058
1060 [[nodiscard]] static double heldAt(const Shape& s, const Samples& x, double t) noexcept
1061 {
1062 if (s.kind == 0) return x.dc;
1063 if (s.kind == 3) return x.dc * (t - x.tz) * (t - x.tz) / (x.tz * x.tz);
1064 return 0.0;
1065 }
1066
1068 [[nodiscard]] static bool solve(int n, double m[5][6]) noexcept
1069 {
1070 for (int c = 0; c < n; ++c)
1071 {
1072 int pivot = c;
1073 for (int r = c + 1; r < n; ++r)
1074 if (std::abs(m[r][c]) > std::abs(m[pivot][c])) pivot = r;
1075 if (!(std::abs(m[pivot][c]) > 1e-300)) return false;
1076 if (pivot != c)
1077 for (int k = 0; k <= n; ++k) std::swap(m[c][k], m[pivot][k]);
1078 for (int r = 0; r < n; ++r)
1079 {
1080 if (r == c) continue;
1081 const double f = m[r][c] / m[c][c];
1082 for (int k = c; k <= n; ++k) m[r][k] -= f * m[c][k];
1083 }
1084 }
1085 for (int r = 0; r < n; ++r) m[r][n] /= m[r][r];
1086 return true;
1087 }
1088
1104 static void pass(const Shape& s, const Samples& x, const double* p, bool together,
1105 double (*normal)[6], double& cost, double& worst) noexcept
1106 {
1107 const int nN = s.numeratorTerms(), nD = s.denominatorTerms();
1108 const int n = together ? nN + nD : nN;
1109 if (normal != nullptr)
1110 for (int a = 0; a < 5; ++a)
1111 for (int b = 0; b < 6; ++b) normal[a][b] = 0.0;
1112 cost = 0.0;
1113 worst = 0.0;
1114 for (int i = 0; i < x.n; ++i)
1115 {
1116 const double t = x.t[i], m = x.m[i], t2 = t * t;
1117 double row[5] = {};
1118 numeratorAt(s, t, row);
1119 double value = heldAt(s, x, t);
1120 for (int j = 0; j < nN; ++j) value += p[j] * row[j];
1121 const double was = 1.0 + p[nN] * t + (nD > 1 ? p[nN + 1] * t2 : 0.0);
1122 const double want = (m + x.floor) * was, have = value + x.floor * was;
1123 const double u = x.weight[i] / want, off = (have - want) * u;
1124 const double apart = off * off * want / have;
1125 cost += apart;
1126 worst = std::max(worst, apart);
1127 if (normal == nullptr) continue;
1128
1129 for (int j = 0; j < nN; ++j) row[j] *= u;
1130 if (together)
1131 {
1132 row[nN] = -u * m * t;
1133 if (nD > 1) row[nN + 1] = -u * m * t2;
1134 }
1135 for (int a = 0; a < n; ++a)
1136 {
1137 const double ra = row[a];
1138 for (int b = a; b < n; ++b) normal[a][b] += ra * row[b];
1139 normal[a][n] -= ra * off;
1140 }
1141 }
1142 }
1143
1150 [[nodiscard]] static bool advance(const Shape& s, bool together, double damping,
1151 double (*normal)[6], double* p) noexcept
1152 {
1153 const int n = together ? s.numeratorTerms() + s.denominatorTerms() : s.numeratorTerms();
1154 if (n == 0) return true;
1155 double scale[5];
1156 for (int a = 0; a < n; ++a)
1157 {
1158 if (!(normal[a][a] > 0.0)) return false;
1159 scale[a] = 1.0 / std::sqrt(normal[a][a]);
1160 }
1161 double system[5][6];
1162 for (int a = 0; a < n; ++a)
1163 {
1164 for (int b = a; b < n; ++b) system[a][b] = system[b][a] = normal[a][b] * scale[a] * scale[b];
1165 system[a][a] = 1.0 + damping;
1166 system[a][n] = normal[a][n] * scale[a];
1167 }
1168 if (!solve(n, system)) return false;
1169 for (int a = 0; a < n; ++a)
1170 {
1171 const double move = system[a][n] * scale[a];
1172 if (!std::isfinite(move)) return false;
1173 p[a] += move;
1174 }
1175 return true;
1176 }
1177
1179 [[nodiscard]] static bool positive(double d1, double d2, double tMax) noexcept
1180 {
1181 constexpr double kMargin = 1e-9;
1182 const double end = 1.0 + tMax * (d1 + tMax * d2);
1183 if (!(end > kMargin * (1.0 + tMax * (std::abs(d1) + tMax * std::abs(d2))))) return false;
1184 if (d2 > 0.0)
1185 {
1186 const double tv = -0.5 * d1 / d2;
1187 if (tv > 0.0 && tv < tMax)
1188 {
1189 const double low = 1.0 + 0.5 * d1 * tv;
1190 if (!(low > kMargin * (1.0 + tv * (std::abs(d1) + tv * std::abs(d2))))) return false;
1191 }
1192 }
1193 return true;
1194 }
1195
1201 static void clampNumerator(const Shape& s, const Samples& x, double* p) noexcept
1202 {
1203 if (s.kind == 0 && s.first)
1204 {
1205 if (x.dc + p[0] * x.tMax < 0.0) p[0] = -x.dc / x.tMax;
1206 }
1207 else if (s.kind == 0)
1208 {
1209 if (x.dc + x.tMax * (p[0] + x.tMax * p[1]) < 0.0) p[1] = -(x.dc + p[0] * x.tMax) / (x.tMax * x.tMax);
1210 if (p[1] > 0.0 && p[0] < 0.0 && -0.5 * p[0] / p[1] < x.tMax && p[0] * p[0] > 4.0 * x.dc * p[1])
1211 p[0] = -2.0 * std::sqrt(x.dc * p[1]);
1212 }
1213 else if (s.kind == 1 && !s.first)
1214 {
1215 p[0] = std::max(p[0], 0.0);
1216 if (p[0] + p[1] * x.tMax < 0.0) p[1] = -p[0] / x.tMax;
1217 }
1218 else if (s.kind != 3)
1219 {
1220 p[0] = std::max(p[0], 0.0);
1221 }
1222 }
1223
1225 [[nodiscard]] static bool factor(double k0, double k1, double k2, double& c0, double& c1, double& c2) noexcept
1226 {
1227 // A polynomial that touches zero at DC or at Nyquist may come out a
1228 // rounding error under it.
1229 const double slack = -1e-9 * (std::abs(k0) + 2.0 * std::abs(k1) + 4.0 * std::abs(k2));
1230 double atDc = k0, atNyquist = k0 + 2.0 * k1 + 4.0 * k2;
1231 if (!(atDc >= slack) || !(atNyquist >= slack)) return false;
1232 atDc = std::max(atDc, 0.0);
1233 atNyquist = std::max(atNyquist, 0.0);
1234 const double p = std::sqrt(atDc), m = std::sqrt(atNyquist);
1235 const double sum = 0.5 * (p + m);
1236 c1 = 0.5 * (p - m);
1237 const double disc = sum * sum - k2;
1238 if (disc < -1e-9 * (sum * sum + std::abs(k2))) return false;
1239 c0 = 0.5 * (sum + std::sqrt(std::max(0.0, disc)));
1240 if (!(c0 > 0.0)) return false;
1241 c2 = 0.25 * k2 / c0;
1242 return true;
1243 }
1244
1246 [[nodiscard]] static bool assemble(const Shape& s, const Samples& x, const double* p, double y0, BiquadCoeffs& out) noexcept
1247 {
1248 const int nN = s.numeratorTerms();
1249 const double scale = 1.0 / y0, scale2 = scale * scale;
1250 double c0 = 1.0, c1 = 0.0, c2 = 0.0;
1251 if (!factor(1.0, p[nN] * scale, s.first ? 0.0 : p[nN + 1] * scale2, c0, c1, c2)) return false;
1252
1253 double e0 = 0.0, e1 = 0.0, e2 = 0.0;
1254 if (s.kind == 0)
1255 {
1256 if (!factor(x.dc, p[0] * scale, s.first ? 0.0 : p[1] * scale2, e0, e1, e2)) return false;
1257 }
1258 else if (s.kind == 1 && !s.first)
1259 {
1260 // y (n1 + n2 y), and y is half the squared magnitude of 1 - z^-1.
1261 double f0 = 0.0, f1 = 0.0, unused = 0.0;
1262 if (!factor(0.5 * p[0] * scale, 0.5 * p[1] * scale2, 0.0, f0, f1, unused)) return false;
1263 e0 = f0; e1 = f1 - f0; e2 = -f1;
1264 }
1265 else if (s.kind == 1)
1266 {
1267 const double g = std::sqrt(0.5 * std::max(p[0], 0.0) * scale);
1268 e0 = g; e1 = -g;
1269 }
1270 else if (s.kind == 2)
1271 {
1272 // Two zeros at DC: y^2 is a quarter of the squared magnitude of (1 - z^-1)^2.
1273 const double g = 0.5 * std::sqrt(std::max(p[0], 0.0)) * scale;
1274 e0 = g; e1 = -2.0 * g; e2 = g;
1275 }
1276 else
1277 {
1278 // Zeros on the circle: (y - yz)^2 is a quarter of the squared magnitude
1279 // of 1 - 2 cos(wz) z^-1 + z^-2, and the gain is the one of DC.
1280 const double g = 0.5 * std::sqrt(x.dc) / s.yNotch;
1281 e0 = g; e1 = -2.0 * g * s.cosNotch; e2 = g;
1282 }
1283
1284 const double inverse = 1.0 / c0;
1285 out.b0 = e0 * inverse; out.b1 = e1 * inverse; out.b2 = e2 * inverse;
1286 out.a1 = c1 * inverse; out.a2 = c2 * inverse;
1287 return std::isfinite(out.b0) && std::isfinite(out.b1) && std::isfinite(out.b2)
1288 && std::isfinite(out.a1) && std::isfinite(out.a2);
1289 }
1290
1291 // -- Cascades: the prototypes ---------------------------------------------------
1292
1293 static constexpr double kLadderStep = 4.0;
1294 static constexpr double kDeep = 3981.0717055349733;
1295 static constexpr double kMiddle = 63.09573444801933;
1296
1297 [[nodiscard]] static AnalogSection& at(AnalogCascade& cascade, int i) noexcept
1298 {
1299 return cascade.sections[static_cast<size_t>(i)];
1300 }
1301
1302 [[nodiscard]] static double omegaOf(double hz) noexcept
1303 {
1304 return 2.0 * kPi * (std::isfinite(hz) ? std::max(hz, 1e-3) : 1000.0);
1305 }
1306
1308 [[nodiscard]] static AnalogSection scaled(const AnalogSection& n, double omega) noexcept
1309 {
1310 if (!n.isSecondOrder())
1311 return AnalogSection::make(0.0, n.b1, n.b0 * omega, 0.0, n.a1, n.a0 * omega);
1312 return AnalogSection::make(n.b2, n.b1 * omega, n.b0 * omega * omega, n.a2, n.a1 * omega, n.a0 * omega * omega);
1313 }
1314
1316 [[nodiscard]] static AnalogSection mirrored(const AnalogSection& n, double omega) noexcept
1317 {
1318 if (!n.isSecondOrder())
1319 return AnalogSection::make(0.0, n.b0, n.b1 * omega, 0.0, n.a0, n.a1 * omega);
1320 return AnalogSection::make(n.b0, n.b1 * omega, n.b2 * omega * omega, n.a0, n.a1 * omega, n.a2 * omega * omega);
1321 }
1322
1324 static void fractionalPole(double order, AnalogCascade& out) noexcept
1325 {
1326 const double gap = std::pow(kLadderStep, order); // from each pole to its zero
1327 const double leaving = 1.0 - order * order * order * order; // the last zero goes to infinity as the order nears 1
1328 double pole = std::pow(kLadderStep, 0.5 * (1.0 - order));
1329 for (int i = 0; i < 3; ++i)
1330 {
1331 const double p1 = pole, p2 = pole * kLadderStep;
1332 const double iz1 = 1.0 / (p1 * gap), iz2 = (i == 2 ? leaving : 1.0) / (p2 * gap);
1333 out.add(AnalogSection::make(iz1 * iz2, iz1 + iz2, 1.0, 1.0 / (p1 * p2), 1.0 / p1 + 1.0 / p2, 1.0));
1334 pole = p2 * kLadderStep;
1335 }
1336 }
1337
1339 [[nodiscard]] static AnalogCascade cutPrototype(double slopeDbPerOctave, double q) noexcept
1340 {
1341 AnalogCascade out;
1342 if (!std::isfinite(slopeDbPerOctave)) return out;
1343 const double order = std::clamp(slopeDbPerOctave, 0.0, kMaxCutSlope) / 6.0;
1344 if (order < 1e-6) return out;
1345 if (order < 1.0)
1346 {
1347 fractionalPole(order, out);
1348 return out;
1349 }
1350
1351 // Pole pairs on the unit semicircle at the angles of a real order.
1352 // The whole part of the order says what completes them: an odd one,
1353 // the last pair (two real poles when the order is exactly odd) with a
1354 // zero; an even one, a real pole with a zero.
1355 const int whole = static_cast<int>(std::floor(order));
1356 const int pairs = static_cast<int>(std::floor(order + 1.0)) / 2;
1357 const bool onPair = (whole % 2) == 1;
1358 const int plain = onPair ? pairs - 1 : pairs; // pairs that stay as the angles put them
1359 double damping[AnalogCascade::kMaxSections] = {};
1360 for (int k = 0; k < pairs; ++k)
1361 damping[k] = 2.0 * std::sin(kPi * (2 * k + 1) / (2.0 * order));
1362
1363 // The section that completes them is what keeps the cascade on the
1364 // curve 1 / (1 + w^(2 order)) between whole orders: at the corner
1365 // (-3 dB) and where that curve has fallen 36 dB and, when it is a
1366 // pair, 18 dB too. Written as powers the conditions are linear in
1367 // what the section has free (its zero and the terms of its poles),
1368 // so there is nothing to search. At a whole order the zero cancels a
1369 // pole or leaves, and what is left is the Butterworth filter.
1370 const double deep = std::pow(kDeep - 1.0, 1.0 / order); // w^2 at the 36 dB point
1371 double atCorner = 1.0, atDeep = 1.0; // attenuation of the plain pairs
1372 for (int k = 0; k < plain; ++k)
1373 {
1374 atCorner *= damping[k] * damping[k];
1375 atDeep *= (1.0 - deep) * (1.0 - deep) + damping[k] * damping[k] * deep;
1376 }
1377 const double lift = std::pow(std::clamp(q, 0.1, 100.0) / kButterworthQ, std::clamp(order - 1.0, 0.0, 1.0));
1378 for (int k = 0; k < plain; ++k)
1379 out.add(AnalogSection::make(0.0, 0.0, 1.0, 1.0, damping[k] / (k == 0 ? lift : 1.0), 1.0));
1380
1381 if (onPair)
1382 {
1383 // The pair 1 + alpha v + beta v^2 over 1 + z v, through three points.
1384 const double mid = std::pow(kMiddle - 1.0, 1.0 / order);
1385 double atMid = 1.0;
1386 for (int k = 0; k < plain; ++k)
1387 atMid *= (1.0 - mid) * (1.0 - mid) + damping[k] * damping[k] * mid;
1388 const double points[3] = { 1.0, mid, deep };
1389 const double asked[3] = { 2.0 / atCorner, kMiddle / atMid, kDeep / atDeep };
1390 double system[5][6] = {};
1391 for (int i = 0; i < 3; ++i)
1392 {
1393 system[i][0] = points[i];
1394 system[i][1] = points[i] * points[i];
1395 system[i][2] = -asked[i] * points[i];
1396 system[i][3] = asked[i] - 1.0;
1397 }
1398 double alpha = 0.0, beta = 0.0, zero2 = -1.0;
1399 if (solve(3, system))
1400 {
1401 alpha = system[0][3]; beta = system[1][3]; zero2 = system[2][3];
1402 }
1403 const double natural2 = beta > 0.0 ? 1.0 / std::sqrt(beta) : 0.0; // the frequency of the pair, squared
1404 const double damping2 = alpha * natural2 + 2.0;
1405 if (zero2 > -1e-9 && zero2 <= 1.0 + 1e-9 && natural2 > 0.0 && std::isfinite(natural2) && damping2 > 1e-6)
1406 {
1407 const double a1 = std::sqrt(damping2 * natural2) / (plain == 0 ? lift : 1.0);
1408 out.add(AnalogSection::make(0.0, std::sqrt(std::max(zero2, 0.0)) * natural2, natural2, 1.0, a1, natural2));
1409 }
1410 else
1411 {
1412 // Two points with the pair where its angle puts it.
1413 const double c = 2.0 / atCorner;
1414 const double slope = deep * (atDeep * c - kDeep);
1415 double z = 0.0;
1416 if (std::abs(slope) > 1e-12 * deep * kDeep)
1417 z = std::clamp((kDeep - atDeep * ((1.0 - deep) * (1.0 - deep) + c * deep)) / slope, 0.0, 1.0);
1418 out.add(AnalogSection::make(0.0, std::sqrt(z), 1.0, 1.0,
1419 std::sqrt(c * (1.0 + z)) / (plain == 0 ? lift : 1.0), 1.0));
1420 }
1421 }
1422 else
1423 {
1424 // The real pole 1 + u v over 1 + z v, through two points.
1425 const double c = 2.0 / atCorner;
1426 const double slope = deep * (atDeep * c - kDeep);
1427 double z = 0.0;
1428 if (std::abs(slope) > 1e-12 * deep * kDeep)
1429 z = std::clamp((kDeep - atDeep * (1.0 + (c - 1.0) * deep)) / slope, 0.0, 1.0);
1430 const double u = c * (1.0 + z) - 1.0;
1431 if (u > 1e-12) out.add(AnalogSection::make(0.0, std::sqrt(z), 1.0, 0.0, std::sqrt(u), 1.0));
1432 }
1433 return out;
1434 }
1435
1441 [[nodiscard]] static AnalogCascade shelfPrototype(double gainDb, double order, double q = kButterworthQ) noexcept
1442 {
1443 AnalogCascade out;
1444 const double nu = wholeOrPart(order);
1445 const double db = std::isfinite(gainDb) ? std::clamp(gainDb, -120.0, 120.0) : 0.0;
1446 // The Q acts on the sharpest pair, in its zeros as in its poles: the
1447 // gain of the pair at the corner does not depend on its damping.
1448 const double sharpest = kButterworthQ / (std::isfinite(q) ? std::clamp(q, 0.05, 100.0) : kButterworthQ);
1449 const double g = std::pow(10.0, db / (20.0 * nu)), beta = std::pow(10.0, -db / (40.0 * nu));
1450 if (nu == std::floor(nu))
1451 {
1452 const int n = static_cast<int>(nu);
1453 for (int i = 0; i < n / 2; ++i)
1454 {
1455 const double s = std::sin(kPi * (2 * i + 1) / (2.0 * n)) * (i == 0 ? sharpest : 1.0);
1456 out.add(AnalogSection::make(1.0, 2.0 * g * s * beta, g * g * beta * beta, 1.0, 2.0 * s * beta, beta * beta));
1457 }
1458 if (n % 2 == 1) out.add(AnalogSection::make(0.0, 1.0, g * beta, 0.0, 1.0, beta));
1459 return out;
1460 }
1461 // Between whole orders. Every section has its share of the gain and
1462 // its own corner factor (gain times the factor squared is 1), so the
1463 // plateaus and the half gain at the corner hold whatever the shares
1464 // and the dampings: whole pairs at the angles of the order, each with
1465 // two orders' worth of the gain, and one section with what is left.
1466 const int pairs = static_cast<int>(std::floor(0.5 * nu));
1467 const double rest = nu - 2.0 * pairs; // in (0, 2), not 1
1468 for (int i = 0; i < pairs; ++i)
1469 {
1470 const double s = std::sin(kPi * (2 * i + 1) / (2.0 * nu)) * (i == 0 ? sharpest : 1.0);
1471 out.add(AnalogSection::make(1.0, 2.0 * g * s * beta, g * g * beta * beta, 1.0, 2.0 * s * beta, beta * beta));
1472 }
1473 if (rest < 1.0)
1474 {
1475 // Under one order: a first-order shelf with that share, which leaves as the share goes to nothing.
1476 const double share = std::pow(g, rest), factor = 1.0 / std::sqrt(share);
1477 out.add(AnalogSection::make(0.0, 1.0, share * factor, 0.0, 1.0, factor));
1478 }
1479 else
1480 {
1481 // Over one: a pair. With one order's share it is the first-order
1482 // section times a root that its zero cancels (a damping of
1483 // g^(1/4) + g^(-1/4)); with two, the pair of the next whole
1484 // order. In between the damping goes from one to the other in
1485 // its logarithm.
1486 const double part = rest - 1.0;
1487 const double share = std::pow(g, 0.5 * rest), factor = 1.0 / std::sqrt(share);
1488 const double wide = std::pow(g, 0.25) + std::pow(g, -0.25);
1489 const double narrow = 2.0 * std::sin(kPi * (2 * pairs + 1) / (2.0 * (2 * pairs + 2)));
1490 double damping = std::exp((1.0 - part) * std::log(wide) + part * std::log(narrow));
1491 if (pairs == 0) damping *= std::pow(sharpest, part); // the Q comes in between orders 1 and 2
1492 out.add(AnalogSection::make(1.0, damping * share * factor, share * share * factor * factor, 1.0, damping * factor, factor * factor));
1493 }
1494 return out;
1495 }
1496
1498 [[nodiscard]] static double wholeOrPart(double order) noexcept
1499 {
1500 return std::isfinite(order) ? std::clamp(order, 1.0, static_cast<double>(kMaxOrder)) : 1.0;
1501 }
1502
1504 struct BandPair
1505 {
1506 double upper1 = 0.0, upper0 = 0.0;
1507 double lower1 = 0.0, lower0 = 0.0;
1508 };
1509
1515 [[nodiscard]] static BandPair bandPair(double c1, double c0, double width, double w0) noexcept
1516 {
1517 BandPair pair;
1518 if (c1 * c1 > 4.0 * c0)
1519 {
1520 // Two real roots: each is a quadratic of its own around the centre.
1521 const double spread = std::sqrt(c1 * c1 - 4.0 * c0);
1522 pair.upper1 = 0.5 * (c1 + spread) * width; pair.upper0 = w0 * w0;
1523 pair.lower1 = 0.5 * (c1 - spread) * width; pair.lower0 = w0 * w0;
1524 return pair;
1525 }
1526 const std::complex<double> root(-0.5 * c1, 0.5 * std::sqrt(std::max(0.0, 4.0 * c0 - c1 * c1)));
1527 const std::complex<double> sum = root * width;
1528 const std::complex<double> spread = std::sqrt(sum * sum - 4.0 * w0 * w0);
1529 std::complex<double> above = 0.5 * (sum + spread), below = 0.5 * (sum - spread);
1530 if (std::norm(above) < std::norm(below)) std::swap(above, below);
1531 pair.upper1 = -2.0 * above.real(); pair.upper0 = std::norm(above);
1532 pair.lower1 = -2.0 * below.real(); pair.lower0 = std::norm(below);
1533 return pair;
1534 }
1535
1537 struct BandFactors
1538 {
1539 double c1[2] = {}, c0[2] = {};
1540 int count = 0;
1541 double lead = 1.0;
1542 };
1543
1544 [[nodiscard]] static BandFactors bandFactors(double p2, double p1, double p0, double width, double w0) noexcept
1545 {
1546 BandFactors f;
1547 if (p2 != 0.0)
1548 {
1549 const BandPair pair = bandPair(p1 / p2, p0 / p2, width, w0);
1550 f.c1[0] = pair.upper1; f.c0[0] = pair.upper0;
1551 f.c1[1] = pair.lower1; f.c0[1] = pair.lower0;
1552 f.count = 2;
1553 f.lead = p2;
1554 }
1555 else if (p1 != 0.0)
1556 {
1557 f.c1[0] = p0 / p1 * width; f.c0[0] = w0 * w0;
1558 f.count = 1;
1559 f.lead = p1;
1560 }
1561 else
1562 {
1563 f.lead = p0;
1564 }
1565 return f;
1566 }
1567
1574 static void addBandForm(const AnalogSection& s, double width, double w0, AnalogCascade& out) noexcept
1575 {
1576 const BandFactors num = bandFactors(s.b2, s.b1, s.b0, width, w0), den = bandFactors(s.a2, s.a1, s.a0, width, w0);
1577 double gain = den.lead != 0.0 ? num.lead / den.lead : 1.0;
1578 for (int i = 0; i < den.count; ++i)
1579 {
1580 if (i < num.count) out.add(AnalogSection::make(gain, gain * num.c1[i], gain * num.c0[i], 1.0, den.c1[i], den.c0[i]));
1581 else out.add(AnalogSection::make(0.0, gain * width, 0.0, 1.0, den.c1[i], den.c0[i]));
1582 gain = 1.0;
1583 }
1584 }
1585
1586 [[nodiscard]] static AnalogCascade bandFilter(double hz, double q, double order, bool stop) noexcept
1587 {
1588 AnalogCascade out;
1589 const double nu = wholeOrPart(order);
1590 const double w0 = omegaOf(hz), w02 = w0 * w0, width = w0 / std::clamp(q, 1e-3, 1e3);
1591 if (nu != std::floor(nu))
1592 {
1593 // Between whole orders: the cut of that slope around the centre
1594 // (for the band-stop, turned over first: S to 1 / S).
1595 const AnalogCascade proto = cutPrototype(6.0 * nu, kButterworthQ);
1596 for (int i = 0; i < proto.count; ++i)
1597 {
1598 const AnalogSection& p = proto.sections[static_cast<size_t>(i)];
1599 addBandForm(stop ? AnalogSection::make(p.b0, p.b1, p.b2, p.a0, p.a1, p.a2) : p, width, w0, out);
1600 }
1601 return out;
1602 }
1603 const int n = static_cast<int>(nu);
1604 const double b2 = stop ? 1.0 : 0.0, b1 = stop ? 0.0 : width, b0 = stop ? w02 : 0.0;
1605 for (int i = 0; i < n / 2; ++i)
1606 {
1607 const BandPair pair = bandPair(2.0 * std::sin(kPi * (2 * i + 1) / (2.0 * n)), 1.0, width, w0);
1608 out.add(AnalogSection::make(b2, b1, b0, 1.0, pair.upper1, pair.upper0));
1609 out.add(AnalogSection::make(b2, b1, b0, 1.0, pair.lower1, pair.lower0));
1610 }
1611 if (n % 2 == 1) out.add(AnalogSection::make(b2, b1, b0, 1.0, width, w02));
1612 return out;
1613 }
1614
1615 // Elliptic functions through the Landen transformation (S. J. Orfanidis,
1616 // "Lecture Notes on Elliptic Filter Design", 2006).
1617
1619 struct Landen
1620 {
1621 double v[14] = {};
1622 int count = 0;
1623 };
1624
1625 [[nodiscard]] static Landen landen(double k, double complement) noexcept
1626 {
1627 Landen steps;
1628 double kc = complement;
1629 while (steps.count < 14 && k > 1e-18)
1630 {
1631 k /= 1.0 + kc;
1632 k *= k;
1633 steps.v[steps.count++] = k;
1634 kc = std::sqrt((1.0 - k) * (1.0 + k));
1635 }
1636 return steps;
1637 }
1638
1640 [[nodiscard]] static double quarterPeriod(const Landen& steps) noexcept
1641 {
1642 double period = 0.5 * kPi;
1643 for (int i = 0; i < steps.count; ++i) period *= 1.0 + steps.v[i];
1644 return period;
1645 }
1646
1648 [[nodiscard]] static std::complex<double> ascend(std::complex<double> w, const Landen& steps) noexcept
1649 {
1650 for (int i = steps.count - 1; i >= 0; --i)
1651 w = (1.0 + steps.v[i]) * w / (1.0 + steps.v[i] * w * w);
1652 return w;
1653 }
1654 [[nodiscard]] static std::complex<double> ellipticCd(std::complex<double> u, const Landen& steps) noexcept
1655 {
1656 return ascend(std::cos(u * (0.5 * kPi)), steps);
1657 }
1658 [[nodiscard]] static std::complex<double> ellipticSn(std::complex<double> u, const Landen& steps) noexcept
1659 {
1660 return ascend(std::sin(u * (0.5 * kPi)), steps);
1661 }
1662
1664 [[nodiscard]] static std::complex<double> ellipticArcSn(std::complex<double> w, double k, const Landen& steps) noexcept
1665 {
1666 double before = k;
1667 for (int i = 0; i < steps.count; ++i)
1668 {
1669 w = w / (1.0 + std::sqrt(1.0 - w * w * (before * before))) * (2.0 / (1.0 + steps.v[i]));
1670 before = steps.v[i];
1671 }
1672 return std::asin(w) * (2.0 / kPi);
1673 }
1674
1680 [[nodiscard]] static double ellipticModulus(int order, double rippleDb, double stopDb,
1681 double& ripple, double& stop) noexcept
1682 {
1683 const int n = std::clamp(order, 1, kMaxOrder);
1684 const double ap = std::isfinite(rippleDb) ? std::clamp(rippleDb, 1e-3, 3.0) : 0.05;
1685 const double as = std::isfinite(stopDb) ? std::clamp(stopDb, 20.0, 140.0) : 100.0;
1686 ripple = std::sqrt(std::expm1(ap * 0.23025850929940458)); // sqrt(10^(ap / 10) - 1)
1687 stop = std::sqrt(std::expm1(as * 0.23025850929940458));
1688 const double k1 = ripple / stop, k1c = std::sqrt((1.0 - k1) * (1.0 + k1));
1689 const double ratio = quarterPeriod(landen(k1c, k1)) / quarterPeriod(landen(k1, k1c));
1690 const double nome = std::exp(-kPi * ratio / n);
1691 double top = 0.0, bottom = 1.0;
1692 for (int m = 0; m < 12; ++m)
1693 {
1694 top += std::pow(nome, m * (m + 1.0));
1695 if (m > 0) bottom += 2.0 * std::pow(nome, 1.0 * m * m);
1696 }
1697 const double theta = top / bottom;
1698 return std::min(4.0 * std::sqrt(nome) * theta * theta, 1.0 - 1e-12);
1699 }
1700
1702 [[nodiscard]] static AnalogCascade ellipticPrototype(int order, double rippleDb, double stopDb) noexcept
1703 {
1704 AnalogCascade out;
1705 const int n = std::clamp(order, 1, kMaxOrder);
1706 double ripple = 0.0, stop = 0.0;
1707 const double k = ellipticModulus(n, rippleDb, stopDb, ripple, stop);
1708 const double k1 = ripple / stop;
1709 const Landen steps = landen(k, std::sqrt((1.0 - k) * (1.0 + k)));
1710 const Landen steps1 = landen(k1, std::sqrt((1.0 - k1) * (1.0 + k1)));
1711 const std::complex<double> j(0.0, 1.0);
1712 const double v0 = (-j * ellipticArcSn(j / ripple, k1, steps1)).real() / n;
1713
1714 for (int i = 0; i < n / 2; ++i)
1715 {
1716 const double u = (2.0 * i + 1.0) / n;
1717 const double zero = 1.0 / (k * ellipticCd(u, steps).real());
1718 const std::complex<double> pole = j * ellipticCd(std::complex<double>(u, -v0), steps);
1719 const double size = std::norm(pole);
1720 out.add(AnalogSection::make(1.0 / (zero * zero), 0.0, 1.0, 1.0 / size, -2.0 * pole.real() / size, 1.0));
1721 }
1722 if (n % 2 == 1)
1723 {
1724 const double pole = (j * ellipticSn(j * v0, steps)).real();
1725 out.add(AnalogSection::make(0.0, 0.0, 1.0, 0.0, -1.0 / pole, 1.0));
1726 }
1727 else if (out.count > 0)
1728 {
1729 // An even order ripples down from 0 dB: DC sits at the bottom of the ripple.
1730 const double gain = 1.0 / std::sqrt(1.0 + ripple * ripple);
1731 AnalogSection& first = at(out, 0);
1732 first.b2 *= gain; first.b0 *= gain;
1733 }
1734 return out;
1735 }
1736};
1737
1738} // namespace dspark
The frequencies a design is fitted over at one sample rate.
static constexpr int kPoints
Frequencies spread evenly in octaves.
void prepare(double sampleRate) noexcept
Sets the sample rate (an invalid one leaves 48 kHz).
double getHighHz() const noexcept
Band(double sampleRate) noexcept
static constexpr double kTopShare
The band never goes past this share of the rate.
double getSampleRate() const noexcept
double getLowHz() const noexcept
Digital sections from analog ones, and the cascades of an equalizer.
static bool bilinearPolynomial(const double *analogNum, const double *analogDen, int order, double sampleRate, double exactHz, double *digitalNum, double *digitalDen) noexcept
The bilinear transform of a whole analog network, polynomial to polynomial, with no factoring.
static int digital(const AnalogCascade &cascade, const Band &band, BiquadCoeffs *out, int capacity) noexcept
Every section of a cascade through digital().
static constexpr int kFitSteps
Most joint steps a fit takes.
static AnalogCascade bell(double hz, double q, double gainDb, double order) noexcept
Bell of any order: the higher the order, the flatter the top and the steeper the sides (6 dB per octa...
static int digital(const AnalogCascade &cascade, double sampleRate, BiquadCoeffs *out, int capacity) noexcept
Every section of a cascade through digital(), for one design.
static constexpr double kMaxCutSlope
Steepest cut, in decibels per octave.
static AnalogCascade bandStop(double hz, double q, double order) noexcept
Butterworth band-stop: a null at the centre, -3 dB at the two edges hz / q apart, and a floor that wi...
static AnalogCascade allPass(double hz, double q, double order) noexcept
All-pass of any order: the magnitude stays at 1 and the phase turns 180 degrees per order around the ...
static constexpr double kButterworthQ
The Q that leaves a cut maximally flat.
static BiquadCoeffs digital(const AnalogSection &section, const Band &band) noexcept
The biquad whose magnitude follows an analog section up to Nyquist.
static double tiltSlopeWithin(double pivotHz, double slopeDbPerOctave, double ceilingDb) noexcept
The slope a tilt can have at a pivot without lifting the end that rises (20 kHz for a positive slope,...
static AnalogCascade bandPass(double hz, double q, double order) noexcept
Butterworth band-pass: 0 dB at the centre, -3 dB at the two edges hz / q apart, 6 dB per octave on ea...
static AnalogCascade ellipticHighCut(double hz, int order=12, double rippleDb=0.05, double stopDb=100.0) noexcept
Elliptic high cut: flat to the corner within the ripple, then a wall down to the stop level.
static AnalogCascade highShelf(double hz, double gainDb, double order, double q=kButterworthQ) noexcept
High shelf of any order: lowShelf() mirrored about the corner.
static BiquadCoeffs bilinear(const AnalogSection &section, double sampleRate, double exactHz) noexcept
The bilinear transform of a section, with one frequency mapped exactly.
static constexpr int kMaxOrder
Highest order of bells, shelves and band filters.
static AnalogCascade highCut(double hz, double slopeDbPerOctave, double q=kButterworthQ) noexcept
High cut (low-pass) of any slope from 0 to 96 dB per octave.
static AnalogCascade lowCut(double hz, double slopeDbPerOctave, double q=kButterworthQ) noexcept
Low cut (high-pass) of any slope from 0 to 96 dB per octave: highCut() mirrored about the corner.
static double ellipticStopRatio(int order=12, double rippleDb=0.05, double stopDb=100.0) noexcept
Where the stop band of an elliptic cut starts, as a ratio to its corner (above 1; the same for the hi...
static AnalogCascade ellipticLowCut(double hz, int order=12, double rippleDb=0.05, double stopDb=100.0) noexcept
Elliptic low cut: ellipticHighCut() mirrored about the corner.
static AnalogCascade lowShelf(double hz, double gainDb, double order, double q=kButterworthQ) noexcept
Low shelf of any order from 1, whole or not: gainDb below the corner, half of it (in decibels) at the...
static BiquadCoeffs digital(const AnalogSection &section, double sampleRate) noexcept
digital() for one design: builds the Band of the rate on the spot.
static double magnitudeDb(const BiquadCoeffs *sections, int n, double hz, double sampleRate) noexcept
Magnitude in decibels of n digital sections in series at a frequency.
static AnalogCascade tilt(double pivotHz, double slopeDbPerOctave) noexcept
Tilt: the same slope across the whole band, 0 dB at the pivot.
Main namespace for the DSPark framework.
Up to kMaxSections analog sections in series.
double magnitudeDb(double hz) const noexcept
Magnitude of the whole cascade in decibels at a frequency in hertz.
std::array< AnalogSection, kMaxSections > sections
void add(const AnalogSection &section) noexcept
Appends a section (ignored once the cascade is full).
static constexpr int kMaxSections
double magnitudeSquared(double omega) const noexcept
Squared magnitude of the whole cascade at an angular frequency (rad/s).
A first or second order analog section, in radians per second: (b2 s^2 + b1 s + b0) / (a2 s^2 + a1 s ...
static AnalogSection lowShelfQ(double hz, double gainDb, double q) noexcept
Second-order low shelf by its Q: gainDb below the corner, half of it (in decibels) at the corner what...
static AnalogSection highPass(double hz, double q=0.7071067811865476) noexcept
Second-order high-pass, gain Q at the corner.
double magnitudeDb(double hz) const noexcept
Magnitude in decibels at a frequency in hertz.
static AnalogSection peak(double hz, double q, double gainDb) noexcept
Bell: gainDb at the centre, bandwidth hz / Q at half that gain in decibels.
static AnalogSection bandPass(double hz, double q=0.7071067811865476) noexcept
Band-pass, 0 dB at the centre.
bool isSecondOrder() const noexcept
Whether the denominator is of second order.
static AnalogSection allPass(double hz, double q=0.7071067811865476) noexcept
Second-order all-pass (the magnitude is 1; only the phase turns).
static AnalogSection firstOrderLowShelf(double hz, double gainDb) noexcept
First-order low shelf: gainDb at DC, half of it (in decibels) at the corner.
static AnalogSection firstOrderHighShelf(double hz, double gainDb) noexcept
First-order high shelf: gainDb at the top, half of it (in decibels) at the corner.
static AnalogSection tilt(double hz, double gainDb) noexcept
First-order tilt: 0 dB at the pivot, half of gainDb down far below it and half of it up far above.
static AnalogSection notch(double hz, double q=0.7071067811865476) noexcept
Notch at the centre, 0 dB away from it.
static AnalogSection lowPass(double hz, double q=0.7071067811865476) noexcept
Second-order low-pass, gain Q at the corner.
static AnalogSection firstOrderHighPass(double hz) noexcept
First-order high-pass, -3 dB at the corner.
static AnalogSection highShelfQ(double hz, double gainDb, double q) noexcept
Second-order high shelf by its Q: lowShelfQ() mirrored about the corner.
double magnitudeSquared(double omega) const noexcept
Squared magnitude at an angular frequency (rad/s).
static AnalogSection lowShelf(double hz, double gainDb, double slope=1.0) noexcept
Second-order low shelf: gainDb below the corner, half of it (in decibels) at the corner.
static AnalogSection tiltSecondOrder(double hz, double gainDb, double q=0.7071067811865476) noexcept
Second-order tilt: 0 dB at the pivot, half of gainDb down far below it and half of it up far above,...
static AnalogSection make(double b2, double b1, double b0, double a2, double a1, double a0) noexcept
Builds a section from its six coefficients.
double poleFrequency() const noexcept
The frequency of the poles, in hertz (the corner of a first-order section).
static AnalogSection highShelf(double hz, double gainDb, double slope=1.0) noexcept
Second-order high shelf: gainDb above the corner, half of it (in decibels) at the corner.
static AnalogSection firstOrderLowPass(double hz) noexcept
First-order low-pass, -3 dB at the corner.
Stores normalised biquad coefficients (b0, b1, b2, a1, a2), always double.
Definition Biquad.h:107