DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
LadderZdf.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
17#include "../DspMath.h"
18
19#include <algorithm>
20#include <array>
21#include <cmath>
22#include <type_traits>
23
24namespace dspark::detail
25{
27enum class LadderTap
28{
29 LP6,
30 LP12,
31 LP18,
32 LP24,
33 BP12,
34 HP24
35};
36
38template <FloatType T> struct LadderCoeffs
39{
40 T G, G2, G3, G4, ig, k, invFbDen;
41};
42
48template <FloatType T> [[nodiscard]] inline LadderCoeffs<T> makeLadderCoeffs(T g, T res) noexcept
49{
51 c.G = g / (T(1) + g);
52 c.G2 = c.G * c.G;
53 c.G3 = c.G2 * c.G;
54 c.G4 = c.G3 * c.G;
55 c.ig = T(1) / (T(1) + g);
56 c.k = res * T(4); // k = 4 -> self-oscillation
57 c.invFbDen = T(1) / (T(1) + c.k * c.G4); // zero-delay loop denominator
58 return c;
59}
60
70template <LadderTap Tap, FloatType T>
71[[nodiscard]] inline T ladderStep(T input, std::array<T, 4>& z, const LadderCoeffs<T>& c,
72 T drive) noexcept
73{
74 // Estimate the LP24 output from the integrator states (zero-delay logic)
75 const T Sest = c.G3 * c.ig * z[0]
76 + c.G2 * c.ig * z[1]
77 + c.G * c.ig * z[2]
78 + c.ig * z[3];
79
80 // Saturate the estimated feedback (cheap ZDF nonlinearity: the state
81 // contribution is shaped, the direct G4*u term stays linear).
82 // fastTanh clamps its input, so the saturated loop stays bounded.
83 T fbSignal = Sest;
84 if (drive > T(1))
85 fbSignal = fastTanh(fbSignal * drive) / drive;
86
87 // Resolve zero-delay loop (denominator precomputed with the coefficients)
88 const T u = (input - c.k * fbSignal) * c.invFbDen;
89
90 // Four cascaded TPT one-pole stages. Tap outputs carry no state
91 // between samples, so they stay in registers ({} avoids a spurious
92 // uninitialised-variable warning; the stores are dead after unroll).
93 std::array<T, 4> y {};
94 T x = u;
95 for (int i = 0; i < 4; ++i)
96 {
97 const T v = (x - z[i]) * c.G;
98 y[i] = v + z[i];
99 z[i] = y[i] + v;
100 x = y[i];
101 }
102
103 // Output tap mix (resolved at compile time per tap instantiation)
104 if constexpr (Tap == LadderTap::LP6) return y[0];
105 else if constexpr (Tap == LadderTap::LP12) return y[1];
106 else if constexpr (Tap == LadderTap::LP18) return y[2];
107 else if constexpr (Tap == LadderTap::LP24) return y[3];
108 else if constexpr (Tap == LadderTap::BP12) return y[0] - y[2];
109 else
110 {
111 // HP24: apply the binomial (1-L)^4 to `u` (the ladder's true input,
112 // post feedback), NOT to `input`. With resonance, input = u*(1+k) at
113 // DC, so the input-based form leaked DC with gain k/(1+k) - e.g. 67%
114 // of the DC passed straight through a "high-pass" at resonance 0.5.
115 // With u the DC gain is exactly 0 and the resonant peak is preserved.
116 return u - T(4)*y[0] + T(6)*y[1] - T(4)*y[2] + y[3];
117 }
118}
119
120// ============================================================================
121// Per-stage saturating ladder (implicit solve)
122// ============================================================================
123
133inline constexpr double kLadderStagesMaxGain = 6.313751514675041;
134
137{
138 double g, k, d, gOverD, tol;
141 double G, w0, w1, w2, w3, invLoop;
142};
143
152template <FloatType T>
153[[nodiscard]] inline LadderStageCoeffs makeLadderStageCoeffs(T g, T res, T drive) noexcept
154{
156 c.g = std::min(static_cast<double>(g), kLadderStagesMaxGain);
157 c.k = 4.0 * static_cast<double>(res);
158 c.d = static_cast<double>(drive);
159 c.gOverD = c.g / c.d;
160 // Newton stops once the largest stage update is below this many knee
161 // widths (1 / drive). Newton converges quadratically, so the error left
162 // is about 10 * tol^2 knee widths (measured 9.8e-8 at 1e-4, 1.6e-11 at
163 // 1e-7): below the sample type's resolution.
164 c.tol = std::is_same_v<T, float> ? 1e-4 : 1e-7;
165 const double ig = 1.0 / (1.0 + c.g);
166 c.G = c.g * ig;
167 c.w3 = ig;
168 c.w2 = c.G * ig;
169 c.w1 = c.G * c.w2;
170 c.w0 = c.G * c.w1;
171 c.invLoop = 1.0 / (1.0 + c.k * (c.G * c.G) * (c.G * c.G));
172 return c;
173}
174
182[[nodiscard]] inline double ladderTanh(double a) noexcept
183{
184 constexpr double kClamp = 6.297019181717114;
185 if (a > kClamp) return 1.0;
186 if (a < -kClamp) return -1.0;
187 const double a2 = a * a;
188 const double num = 34459425.0 + a2 * (4729725.0 + a2 * (135135.0 + a2 * (990.0 + a2)));
189 const double den = 34459425.0 + a2 * (16216200.0 + a2 * (945945.0 + a2 * (13860.0 + a2 * 45.0)));
190 return a * num / den;
191}
192
194[[nodiscard]] inline double ladderTanhRatio(double a) noexcept
195{
196 constexpr double kClamp = 6.297019181717114;
197 const double m = a < 0.0 ? -a : a;
198 if (m > kClamp) return 1.0 / m;
199 const double a2 = a * a;
200 const double num = 34459425.0 + a2 * (4729725.0 + a2 * (135135.0 + a2 * (990.0 + a2)));
201 const double den = 34459425.0 + a2 * (16216200.0 + a2 * (945945.0 + a2 * (13860.0 + a2 * 45.0)));
202 return num / den;
203}
204
206inline constexpr int kLadderStagesMaxIterations = 16;
207
218inline constexpr double kLadderStagesStateStartGain = 0.15;
219
234inline constexpr double kLadderStagesLinearStartLimit = 2.0;
235
277template <LadderTap Tap, FloatType T>
278[[nodiscard]] inline T ladderStepStages(T input, std::array<T, 4>& z, const LadderStageCoeffs& c) noexcept
279{
280 const double x = static_cast<double>(input);
281 const double g = c.g, k = c.k, d = c.d, gd = c.gOverD;
282 const double s0 = static_cast<double>(z[0]), s1 = static_cast<double>(z[1]);
283 const double s2 = static_cast<double>(z[2]), s3 = static_cast<double>(z[3]);
284
285 // 1) Start. Below kLadderStagesStateStartGain the states. Above it the
286 // drive-free ladder solved exactly, while every stage of it stays
287 // within kLadderStagesLinearStartLimit knee widths; otherwise a
288 // fixed-pivot solve: phi(v) ~ v * r_i with r_i = tanh(d s)/(d s) at
289 // the states. Each stage is then linear, y_i = a_i * y_{i-1} + b_i,
290 // and the loop closes in one division.
291 double u = 0.0, y0 = s0, y1 = s1, y2 = s2, y3 = s3;
292 bool pivot = false;
294 {
295 // Linear prediction in closed form (every stage from u and the
296 // states at once): y_i = G y_{i-1} + (1 - G) s_i.
297 const double G = c.G, H = 1.0 - G;
298 const double h0 = H * s0, h1 = H * s1, h2 = H * s2, h3 = H * s3;
299 u = (x - k * ((c.w0 * s0 + c.w1 * s1) + (c.w2 * s2 + c.w3 * s3))) * c.invLoop;
300 const double G2 = G * G;
301 const double l0 = G * u + h0;
302 const double l1 = G2 * u + (G * h0 + h1);
303 const double l2 = (G2 * G) * u + (G2 * h0 + (G * h1 + h2));
304 const double l3 = (G2 * G2) * u + ((G2 * G) * h0 + (G2 * h1 + (G * h2 + h3)));
305 const double reach = std::max(std::max(std::abs(u), std::abs(l0)),
306 std::max(std::max(std::abs(l1), std::abs(l2)), std::abs(l3))) * d;
307 pivot = !(reach < kLadderStagesLinearStartLimit);
308 y0 = l0; y1 = l1; y2 = l2; y3 = l3;
309 }
310 if (pivot)
311 {
312 const double r0 = ladderTanhRatio(d * (x - k * s3));
313 const double r1 = ladderTanhRatio(d * s0), r2 = ladderTanhRatio(d * s1);
314 const double r3 = ladderTanhRatio(d * s2), r4 = ladderTanhRatio(d * s3);
315 const double i1 = 1.0 / (1.0 + g * r1), i2 = 1.0 / (1.0 + g * r2);
316 const double i3 = 1.0 / (1.0 + g * r3), i4 = 1.0 / (1.0 + g * r4);
317 const double a1 = g * r0 * i1, a2 = g * r1 * i2, a3 = g * r2 * i3, a4 = g * r3 * i4;
318 const double b1 = s0 * i1, b2 = s1 * i2, b3 = s2 * i3, b4 = s3 * i4;
319 const double A = a1 * a2 * a3 * a4;
320 const double B = ((b1 * a2 + b2) * a3 + b3) * a4 + b4;
321 u = (x - k * B) / (1.0 + k * A);
322 y0 = a1 * u + b1;
323 y1 = a2 * y0 + b2;
324 y2 = a3 * y1 + b3;
325 y3 = a4 * y2 + b4;
326 }
327
328 // 2) Clipped Newton steps on r_i = y_i - s_i - g (phi_{i-1} - phi_i).
329 for (int it = 0; it < kLadderStagesMaxIterations; ++it)
330 {
331 u = x - k * y3;
332 const double t0 = ladderTanh(d * u), t1 = ladderTanh(d * y0);
333 const double t2 = ladderTanh(d * y1), t3 = ladderTanh(d * y2);
334 const double t4 = ladderTanh(d * y3);
335 const double p0 = 1.0 - t0 * t0, p1 = 1.0 - t1 * t1, p2 = 1.0 - t2 * t2;
336 const double p3 = 1.0 - t3 * t3, p4 = 1.0 - t4 * t4;
337 // Diagonal 1 + g phi'(y_i); sub-diagonal -g phi'(y_{i-1}); corner
338 // (row 1, column 4) g k phi'(u). The reciprocals are independent.
339 const double q1 = 1.0 / (1.0 + g * p1), q2 = 1.0 / (1.0 + g * p2);
340 const double q3 = 1.0 / (1.0 + g * p3), q4 = 1.0 / (1.0 + g * p4);
341 const double e1 = y0 - s0 - gd * (t0 - t1);
342 const double e2 = y1 - s1 - gd * (t1 - t2);
343 const double e3 = y2 - s2 - gd * (t2 - t3);
344 const double e4 = y3 - s3 - gd * (t3 - t4);
345 // Forward elimination: delta_i = P_i + Q_i * delta_4.
346 const double P1 = -e1 * q1, Q1 = -g * k * p0 * q1;
347 const double P2 = (g * p1 * P1 - e2) * q2, Q2 = g * p1 * Q1 * q2;
348 const double P3 = (g * p2 * P2 - e3) * q3, Q3 = g * p2 * Q2 * q3;
349 const double P4 = (g * p3 * P3 - e4) * q4, Q4 = g * p3 * Q3 * q4;
350 const double d4 = P4 / (1.0 - Q4); // Q4 <= 0: the pivot is >= 1
351 const double d1 = P1 + Q1 * d4, d2 = P2 + Q2 * d4, d3 = P3 + Q3 * d4;
352 double m = std::max(std::max(std::abs(d1), std::abs(d2)),
353 std::max(std::abs(d3), std::abs(d4))) * d;
354 const double scale = m > 1.0 ? 1.0 / m : 1.0;
355 y0 += scale * d1;
356 y1 += scale * d2;
357 y2 += scale * d3;
358 y3 += scale * d4;
359 if (m < c.tol) break; // (also false for NaN: the cap ends that case)
360 }
361 u = x - k * y3;
362
363 z[0] = static_cast<T>(2.0 * y0 - s0);
364 z[1] = static_cast<T>(2.0 * y1 - s1);
365 z[2] = static_cast<T>(2.0 * y2 - s2);
366 z[3] = static_cast<T>(2.0 * y3 - s3);
367
368 double out;
369 if constexpr (Tap == LadderTap::LP6) out = y0;
370 else if constexpr (Tap == LadderTap::LP12) out = y1;
371 else if constexpr (Tap == LadderTap::LP18) out = y2;
372 else if constexpr (Tap == LadderTap::LP24) out = y3;
373 else if constexpr (Tap == LadderTap::BP12) out = y0 - y2;
374 else out = u - 4.0 * y0 + 6.0 * y1 - 4.0 * y2 + y3;
375 return static_cast<T>(out);
376}
377
378// ============================================================================
379// Input-saturating ladder (sub-stepped, delay-free loop)
380// ============================================================================
381
382#if defined(_MSC_VER) && !defined(__clang__)
383 #define DSPARK_LADDER_INLINE __forceinline
384#elif defined(__GNUC__) || defined(__clang__)
385 #define DSPARK_LADDER_INLINE inline __attribute__((always_inline))
386#else
387 #define DSPARK_LADDER_INLINE inline
388#endif
389
391inline constexpr int kLadderInputSubsteps = 4;
392
394inline constexpr double kLadderTanhLimit = 6.297019181717114;
396
402inline void ladderTanhParts(double a2, double& num, double& den) noexcept
403{
404 const double a4 = a2 * a2;
405 const double a6 = a4 * a2;
406 num = ((34459425.0 + 4729725.0 * a2) + 135135.0 * a4) + a6 * (990.0 + a2);
407 den = ((34459425.0 + 16216200.0 * a2) + 945945.0 * a4) + a6 * (13860.0 + 45.0 * a2);
408}
409
412{
413 double G = 0.0, H = 1.0;
414 double d = 1.0, invD = 1.0;
415 double kd = 0.0;
416 double c = 0.0;
417 double onePlusC = 1.0;
418 double kappa = 0.0;
419 double cS[4] = {};
420 double cAhead[4] = {};
421 double pre = 0.0, post = 0.0;
422};
423
433{
434 double q[4] = {};
435 double ahead = 0.0;
436 double loopStates = 0.0;
437 double loopDrive = 0.0;
438 double input = 0.0;
439 double output = 0.0;
440};
441
443inline void ladderInputRefresh(LadderInputState& h, const LadderInputCoeffs& c) noexcept
444{
445 h.ahead = ((c.cAhead[0] * h.q[0] + c.cAhead[1] * h.q[1]) + c.cAhead[2] * h.q[2]) + c.cAhead[3] * h.q[3];
446 h.loopStates = c.kd * ((c.cS[0] * h.q[0] + c.cS[1] * h.q[1]) + (c.cS[2] * h.q[2] + c.cS[3] * h.q[3]));
447 h.loopDrive = 0.0;
448}
449
464template <FloatType T>
465[[nodiscard]] inline LadderInputCoeffs makeLadderInputCoeffs(double cutoffRatio, T res, T drive) noexcept
466{
468 const double ratio = cutoffRatio > 1e-6 ? std::min(cutoffRatio, 0.499) : 1e-6;
469 const double g = std::tan(pi<double> * ratio / kLadderInputSubsteps);
470 c.G = g / (1.0 + g);
471 c.H = 1.0 - c.G;
472 const double G2 = c.G * c.G;
473 const double power[5] = { 1.0, c.G, G2, G2 * c.G, G2 * G2 };
474 const double k = 4.0 * static_cast<double>(res);
475 c.d = static_cast<double>(drive);
476 c.invD = 1.0 / c.d;
477 c.kd = k * c.d;
478 c.c = k * power[4];
479 c.onePlusC = 1.0 + c.c;
480 // One sub-step of the four trapezoidal stages fed w: y_i = G^(i+1) w +
481 // sum_{j <= i} G^(i-j) H s_j, and each state moves to 2 y_i - s_i, so the
482 // states map as s' = a s + b w.
483 double a[4][4] = {}, b[4] = {};
484 for (int i = 0; i < 4; ++i)
485 {
486 for (int j = 0; j < 4; ++j)
487 a[i][j] = j < i ? 2.0 * power[i - j] * c.H : (j == i ? 1.0 - 2.0 * c.G : 0.0);
488 b[i] = 2.0 * power[i + 1];
489 c.cS[i] = c.H * power[3 - i];
490 }
491 // The feedback sum one sub-step on is therefore (cS a) s + (cS b) w.
492 double perUnit = 0.0;
493 for (int j = 0; j < 4; ++j)
494 {
495 double sum = 0.0;
496 for (int i = 0; i < 4; ++i) sum += c.cS[i] * a[i][j];
497 c.cAhead[j] = c.kd * sum;
498 perUnit += c.cS[j] * b[j];
499 }
500 c.kappa = k * perUnit;
501 // The input is interpolated linearly over the sub-steps and the output is
502 // their mean; they lose 5.0 and 2.5 dB at 0.417 fs. A one-pole correction
503 // on each side, y = (1 + b) x - b y[-1], restores them within 0.25 dB up
504 // to there.
505 c.pre = 0.30;
506 c.post = 0.14;
507 return c;
508}
509
512{
513 double q0 = 0.0, q1 = 0.0, q2 = 0.0, q3 = 0.0;
514 double ahead = 0.0, kl = 0.0, kw = 0.0, R = 0.0;
515 double x = 0.0, xs = 0.0, dx = 0.0, acc = 0.0;
516
518 template <FloatType T>
519 DSPARK_LADDER_INLINE void begin(T input, const LadderInputState& h, const LadderInputCoeffs& c) noexcept
520 {
521 const double xPrev = h.input;
522 x = (1.0 + c.pre) * static_cast<double>(input) - c.pre * xPrev;
523 q0 = h.q[0];
524 q1 = h.q[1];
525 q2 = h.q[2];
526 q3 = h.q[3];
527 dx = (x - xPrev) * (1.0 / kLadderInputSubsteps);
528 xs = xPrev + dx;
529 ahead = h.ahead;
530 // The same two terms a sub-step hands the next one, so the last
531 // saturator output of a sample reaches the first loop drive of the
532 // next through one subtraction as well.
533 R = (c.d * xs - h.loopStates) - h.loopDrive;
534 acc = 0.0;
535 }
536
538 template <LadderTap Tap>
539 DSPARK_LADDER_INLINE void substep(const LadderInputCoeffs& c) noexcept
540 {
541 // Saturated loop input, one Newton step from the explicit value:
542 // with tanh(R) = R nu / de, t / (1 + c (1 - t^2)) is
543 // R nu de / ((1 + c) de^2 - c R^2 nu^2). It is divided out twice,
544 // plain for the stages and scaled by kappa for the next loop drive,
545 // which then follows the division by one subtraction. Past the limit
546 // of the rational tanh the saturator is at 1 and the Newton step
547 // leaves it there (1 - t^2 = 0): a branch, with nothing to divide. A
548 // NaN drive takes it too, as -1.
549 kl = ahead;
550 double wk;
551 const double r2 = R * R;
552 if (r2 < kLadderTanhLimit2) [[likely]]
553 {
554 double nu, de;
555 ladderTanhParts(r2, nu, de);
556 const double nd = nu * de;
557 const double den = c.onePlusC * (de * de) - (c.c * r2) * (nu * nu);
558 wk = (R * nd) / den;
559 kw = ((c.kappa * R) * nd) / den;
560 }
561 else
562 {
563 wk = R > 0.0 ? 1.0 : -1.0;
564 kw = R > 0.0 ? c.kappa : -c.kappa;
565 }
566 xs += dx;
567 R = (c.d * xs - kl) - kw;
568 // The four stages in turn; each state moves to twice its output minus
569 // itself.
570 const double w = wk * c.invD;
571 const double y0 = c.G * w + c.H * q0;
572 const double y1 = c.G * y0 + c.H * q1;
573 const double y2 = c.G * y1 + c.H * q2;
574 const double y3 = c.G * y2 + c.H * q3;
575 if constexpr (Tap == LadderTap::LP6) acc += y0;
576 else if constexpr (Tap == LadderTap::LP12) acc += y1;
577 else if constexpr (Tap == LadderTap::LP18) acc += y2;
578 else if constexpr (Tap == LadderTap::LP24) acc += y3;
579 else if constexpr (Tap == LadderTap::BP12) acc += y0 - y2;
580 else
581 {
582 // HP24: the binomial (1 - L)^4 on the cascade's input, the
583 // saturated loop input (see ladderStep()).
584 acc += ((w - 4.0 * y0) + 6.0 * y1) + (y3 - 4.0 * y2);
585 }
586 q0 = 2.0 * y0 - q0;
587 q1 = 2.0 * y1 - q1;
588 q2 = 2.0 * y2 - q2;
589 q3 = 2.0 * y3 - q3;
590 // For the sub-step after the next; the last stage's state joins last.
591 ahead = ((c.cAhead[0] * q0 + c.cAhead[1] * q1) + c.cAhead[2] * q2) + c.cAhead[3] * q3;
592 }
593
595 template <FloatType T>
596 [[nodiscard]] DSPARK_LADDER_INLINE T finish(LadderInputState& h, const LadderInputCoeffs& c) const noexcept
597 {
598 h.q[0] = q0;
599 h.q[1] = q1;
600 h.q[2] = q2;
601 h.q[3] = q3;
602 h.ahead = ahead;
603 h.loopStates = kl;
604 h.loopDrive = kw;
605 h.input = x;
606 const double y = (1.0 + c.post) * (acc * (1.0 / kLadderInputSubsteps)) - c.post * h.output;
607 h.output = y;
608 return static_cast<T>(y);
609 }
610};
611
643template <LadderTap Tap, FloatType T>
644[[nodiscard]] DSPARK_LADDER_INLINE T ladderStepInput(T input, LadderInputState& h, const LadderInputCoeffs& c) noexcept
645{
646 LadderInputLane lane;
647 lane.begin(input, h, c);
648 for (int j = 0; j < kLadderInputSubsteps; ++j) lane.substep<Tap>(c);
649 return lane.finish<T>(h, c);
650}
651
665template <LadderTap Tap, FloatType T>
666DSPARK_LADDER_INLINE void ladderStepInputPair(T& a, T& b, LadderInputState& ha, LadderInputState& hb,
667 const LadderInputCoeffs& c) noexcept
668{
669 LadderInputLane la, lb;
670 la.begin(a, ha, c);
671 lb.begin(b, hb, c);
672 for (int j = 0; j < kLadderInputSubsteps; ++j)
673 {
674 la.substep<Tap>(c);
675 lb.substep<Tap>(c);
676 }
677 a = la.finish<T>(ha, c);
678 b = lb.finish<T>(hb, c);
679}
680
681#undef DSPARK_LADDER_INLINE
682} // namespace dspark::detail
constexpr double kLadderStagesLinearStartLimit
Largest stage level of the linear prediction, in knee widths, at which ladderStepStages() starts Newt...
Definition LadderZdf.h:234
double ladderTanh(double a) noexcept
tanh(a) by its [9/8] continued-fraction convergent, clamped to +-1 where the convergent reaches 1 (|a...
Definition LadderZdf.h:182
constexpr int kLadderInputSubsteps
Sub-steps per sample of the input-saturating ladder.
Definition LadderZdf.h:391
constexpr int kLadderStagesMaxIterations
Newton step cap of ladderStepStages() (the worst case measured is 10).
Definition LadderZdf.h:206
constexpr double kLadderStagesMaxGain
Largest prewarped integrator gain of the per-stage model: tan(0.45 pi), a cutoff of 0....
Definition LadderZdf.h:133
constexpr double kLadderTanhLimit
Where ladderTanh()'s rational function reaches 1, and that squared.
Definition LadderZdf.h:394
DSPARK_LADDER_INLINE void ladderStepInputPair(T &a, T &b, LadderInputState &ha, LadderInputState &hb, const LadderInputCoeffs &c) noexcept
ladderStepInput() on two channels at once, sub-step by sub-step.
Definition LadderZdf.h:666
void ladderInputRefresh(LadderInputState &h, const LadderInputCoeffs &c) noexcept
Recomputes the carried sums after the states or the coefficients changed.
Definition LadderZdf.h:443
double ladderTanhRatio(double a) noexcept
ladderTanh(a) / a, finite at a = 0 (where it is 1).
Definition LadderZdf.h:194
LadderInputCoeffs makeLadderInputCoeffs(double cutoffRatio, T res, T drive) noexcept
Derives the coefficients of the input-saturating ladder.
Definition LadderZdf.h:465
constexpr double kLadderStagesStateStartGain
Integrator gain below which ladderStepStages() falls back to the integrator states instead of the fix...
Definition LadderZdf.h:218
void ladderTanhParts(double a2, double &num, double &den) noexcept
The two polynomials of ladderTanh() in a^2 (tanh(a) = a num / den), each summed as two halves so that...
Definition LadderZdf.h:402
T ladderStep(T input, std::array< T, 4 > &z, const LadderCoeffs< T > &c, T drive) noexcept
Advances the four TPT one-pole stages and the resolved feedback loop by one sample.
Definition LadderZdf.h:71
LadderStageCoeffs makeLadderStageCoeffs(T g, T res, T drive) noexcept
Derives the coefficients of the per-stage saturating ladder.
Definition LadderZdf.h:153
DSPARK_LADDER_INLINE T ladderStepInput(T input, LadderInputState &h, const LadderInputCoeffs &c) noexcept
Advances the input-saturating ladder by one sample.
Definition LadderZdf.h:644
constexpr double kLadderTanhLimit2
Definition LadderZdf.h:395
LadderCoeffs< T > makeLadderCoeffs(T g, T res) noexcept
Derives the ladder coefficients.
Definition LadderZdf.h:48
T ladderStepStages(T input, std::array< T, 4 > &z, const LadderStageCoeffs &c) noexcept
Advances the per-stage saturating ladder by one sample.
Definition LadderZdf.h:278
LadderTap
Output tap mix of the four-stage cascade.
Definition LadderZdf.h:28
T fastTanh(T x) noexcept
Fast tanh approximation using a Pade [5,4] rational function.
Definition DspMath.h:169
Coefficients derived from g and the resonance.
Definition LadderZdf.h:39
Coefficients of ladderStepInput(), derived by makeLadderInputCoeffs().
Definition LadderZdf.h:412
double H
One stage per sub-step: y = G in + H s, then s = 2 y - s.
Definition LadderZdf.h:413
double cAhead[4]
k d times that sum one sub-step on, from the states now.
Definition LadderZdf.h:420
double invD
Level scale of the saturator and its reciprocal.
Definition LadderZdf.h:414
double post
Level corrections of the two rate changes.
Definition LadderZdf.h:421
double kappa
k times what one unit of saturator output adds to the next feedback sum.
Definition LadderZdf.h:418
double cS[4]
Feedback sum of the states: the last stage's output with no input.
Definition LadderZdf.h:419
double c
Loop gain past the saturator, k G^4.
Definition LadderZdf.h:416
One channel's registers while ladderStepInput() runs a sample.
Definition LadderZdf.h:512
DSPARK_LADDER_INLINE void substep(const LadderInputCoeffs &c) noexcept
One sub-step: the loop input, the stages and the taps.
Definition LadderZdf.h:539
DSPARK_LADDER_INLINE T finish(LadderInputState &h, const LadderInputCoeffs &c) const noexcept
Stores the state and returns the level-corrected mean of the sub-steps.
Definition LadderZdf.h:596
DSPARK_LADDER_INLINE void begin(T input, const LadderInputState &h, const LadderInputCoeffs &c) noexcept
Loads the state and the first sub-step's loop drive.
Definition LadderZdf.h:519
State of ladderStepInput(), in double whatever the sample type is.
Definition LadderZdf.h:433
double loopStates
That term as the last sub-step formed it, from the states it started from,.
Definition LadderZdf.h:436
double output
Previous output.
Definition LadderZdf.h:439
double input
Previous input after its level correction.
Definition LadderZdf.h:438
double loopDrive
and kappa times its saturator output: together, k d times the feedback sum of q.
Definition LadderZdf.h:437
double q[4]
Integrator states.
Definition LadderZdf.h:434
double ahead
cAhead . q: k d times the feedback sum q reaches a sub-step on with no input.
Definition LadderZdf.h:435
Coefficients of ladderStepStages(), derived by makeLadderStageCoeffs().
Definition LadderZdf.h:137