DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
Adaa.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
41#include "../DspMath.h"
42#include "LogCosh.h"
43
44#include <algorithm>
45#include <cmath>
46#include <numbers>
47#include <utility>
48
49namespace dspark::detail
50{
51
54{
55 double u = 0.0;
56 double g = 0.0;
57 double aux = 0.0;
58};
59
62template <class Family>
64{
65 static constexpr double kTolDiv = 1e-3;
66 static constexpr double kTolDd2 = 1e-4;
67
68 [[nodiscard]] static double div(double a, double b, double ga, double gb) noexcept
69 {
70 const double d = a - b;
71 if (std::abs(d) > kTolDiv) return (ga - gb) / d;
72 const double m = 0.5 * (a + b);
73 return Family::d1(m) + Family::d3(m) * (d * d * (1.0 / 24.0));
74 }
75
77 [[nodiscard]] static double twice(double x0, double x1, double x2,
78 double g0, double g1, double g2) noexcept
79 {
80 const double d02 = x0 - x2;
81 if (std::abs(d02) > kTolDd2)
82 return 2.0 * (div(x0, x1, g0, g1) - div(x1, x2, g1, g2)) / d02;
83 // Confluent form: x0 and x2 merge into their mean.
84 const double xb = 0.5 * (x0 + x2), e = xb - x1;
85 if (std::abs(e) > kTolDd2)
86 return 2.0 * (Family::d1(xb) - div(xb, x1, Family::g(xb), g1)) / e;
87 return Family::d2((x0 + x1 + x2) * (1.0 / 3.0));
88 }
89};
90
91// ---------------------------------------------------------------------------
92// Hard clip: f = clamp(u, -1, 1)
93// ---------------------------------------------------------------------------
95{
96 static double g(double u) noexcept
97 {
98 const double a = std::abs(u);
99 return a <= 1.0 ? u * u * u * (1.0 / 6.0)
100 : std::copysign(0.5 * a * a - 0.5 * a + 1.0 / 6.0, u);
101 }
102 static double d1(double u) noexcept
103 {
104 const double a = std::abs(u);
105 return a <= 1.0 ? 0.5 * u * u : a - 0.5;
106 }
107 static double d2(double u) noexcept { return std::clamp(u, -1.0, 1.0); }
108 static double d3(double u) noexcept { return std::abs(u) < 1.0 ? 1.0 : 0.0; }
109
110 static AdaaPoint point(double u) noexcept { return {u, g(u), 0.0}; }
111
114 static constexpr bool kClosedForm = true;
115 static double value(double u0, double u1, double u2) noexcept
116 {
117 double y = 0.0;
118 (void)exact({u0, 0.0, 0.0}, {u1, 0.0, 0.0}, {u2, 0.0, 0.0}, y);
119 return y;
120 }
121
128 static bool exact(const AdaaPoint& p0, const AdaaPoint& p1, const AdaaPoint& p2,
129 double& y) noexcept
130 {
131 double lo = p0.u, mid = p1.u, hi = p2.u;
132 if (lo > mid) std::swap(lo, mid);
133 if (mid > hi) std::swap(mid, hi);
134 if (lo > mid) std::swap(lo, mid);
135 if (lo >= 1.0) { y = 1.0; return true; }
136 if (hi <= -1.0) { y = -1.0; return true; }
137 const double mean = (lo + mid + hi) * (1.0 / 3.0);
138 if (hi <= 1.0 && lo >= -1.0) { y = mean; return true; }
139 y = mean - rampAbove(lo, mid, hi, mean, 1.0) + rampBelow(lo, mid, hi, mean, -1.0);
140 return true;
141 }
142
144 static double rampAbove(double a, double b, double c, double mean, double tau) noexcept
145 {
146 if (tau >= c) return 0.0;
147 if (tau <= a) return mean - tau;
148 if (tau >= b)
149 {
150 const double r = c - tau;
151 return r * r * r / (3.0 * (c - a) * (c - b));
152 }
153 const double l = tau - a;
154 return (mean - tau) + l * l * l / (3.0 * (c - a) * (b - a));
155 }
156
158 static double rampBelow(double a, double b, double c, double mean, double tau) noexcept
159 {
160 if (tau <= a) return 0.0;
161 if (tau >= c) return tau - mean;
162 if (tau <= b)
163 {
164 const double l = tau - a;
165 return l * l * l / (3.0 * (c - a) * (b - a));
166 }
167 const double r = c - tau;
168 return (tau - mean) + r * r * r / (3.0 * (c - a) * (c - b));
169 }
170};
171
172// ---------------------------------------------------------------------------
173// tanh: F1 = log cosh, F2 = integral of log cosh
174// ---------------------------------------------------------------------------
176{
179 static constexpr double kOuter = 2.0;
180
181 static double g(double u) noexcept { return integralLogCosh(u); }
182 static double d1(double u) noexcept { return logCosh(u); }
183 static double d2(double u) noexcept { return std::tanh(u); }
184 static double d3(double u) noexcept { const double t = std::tanh(u); return 1.0 - t * t; }
185
188 {
189 // The tail is negative, so the odd extension multiplies by the sign
190 // (copysign would take its magnitude).
191 static double g(double u) noexcept
192 {
193 const double r = tail(std::abs(u));
194 return u < 0.0 ? -r : r;
195 }
196 static double d1(double u) noexcept { return std::log1p(std::exp(-2.0 * std::abs(u))); }
197 static double d2(double u) noexcept
198 {
199 const double z = std::exp(-2.0 * std::abs(u));
200 const double v = 2.0 * z / (1.0 + z); // tanh(u) - sign(u)
201 return u < 0.0 ? v : -v;
202 }
203 static double d3(double u) noexcept { const double t = std::tanh(u); return 1.0 - t * t; }
204 };
205
208 static double tail(double a) noexcept
209 {
210 if (a < 2.0) return integralLogCoshTail(a);
211 if (a > 19.0) return 0.0; // |tail| < 1.6e-17
212 const double z = std::exp(-2.0 * a);
213 double li = -1.0 / 81.0;
214 for (int k = 8; k >= 1; --k)
215 li = li * z + (k % 2 == 0 ? 1.0 : -1.0) / (k * k);
216 return 0.5 * (li * z);
217 }
218
219 static AdaaPoint point(double u) noexcept
220 {
221 const double a = std::abs(u);
222 if (a < 1.0) return {u, integralLogCosh(u), 0.0};
223 const double r = tail(a);
224 return {u, std::copysign(0.5 * a * a - a * 0.6931471805599453 + (r + 0.4112335167120566), u),
225 u < 0.0 ? -r : r};
226 }
227
228 static bool exact(const AdaaPoint& p0, const AdaaPoint& p1, const AdaaPoint& p2,
229 double& y) noexcept
230 {
231 const double lo = std::min(p0.u, std::min(p1.u, p2.u)), hi = std::max(p0.u, std::max(p1.u, p2.u));
232 if (!(lo >= kOuter || hi <= -kOuter)) return false;
233 const double s = lo >= kOuter ? 1.0 : -1.0;
234 y = s + AdaaSecondDifference<Remainder>::twice(p0.u, p1.u, p2.u, p0.aux, p1.aux, p2.aux);
235 return true;
236 }
237};
238
239// ---------------------------------------------------------------------------
240// Clamped fastSin() polynomial: f = p(clamp(u, -pi/2, pi/2)), p(pi/2) = 1
241// ---------------------------------------------------------------------------
245{
246 static constexpr double poly(double v) noexcept
247 {
248 constexpr auto c = fastSinPolynomial<double>;
249 const double v2 = v * v;
250 return v * (c[0] + v2 * (c[1] + v2 * (c[2] + v2 * (c[3] + v2 * c[4]))));
251 }
252 static constexpr double poly1(double v) noexcept // antiderivative, even
253 {
254 constexpr auto c = fastSinPolynomial<double>;
255 const double v2 = v * v;
256 return v2 * (c[0] / 2.0 + v2 * (c[1] / 4.0 + v2 * (c[2] / 6.0 + v2 * (c[3] / 8.0 + v2 * c[4] / 10.0))));
257 }
258 static constexpr double poly2(double v) noexcept // second antiderivative, odd
259 {
260 constexpr auto c = fastSinPolynomial<double>;
261 const double v2 = v * v;
262 return v * v2 * (c[0] / 6.0 + v2 * (c[1] / 20.0 + v2 * (c[2] / 42.0 + v2 * (c[3] / 72.0 + v2 * c[4] / 110.0))));
263 }
264 static constexpr double polySlope(double v) noexcept
265 {
266 constexpr auto c = fastSinPolynomial<double>;
267 const double v2 = v * v;
268 return c[0] + v2 * (3.0 * c[1] + v2 * (5.0 * c[2] + v2 * (7.0 * c[3] + v2 * 9.0 * c[4])));
269 }
270};
271
273{
274 static constexpr double kHalfPi = std::numbers::pi * 0.5;
275 static constexpr double kEdge = poly(kHalfPi), kEdge1 = poly1(kHalfPi), kEdge2 = poly2(kHalfPi);
276
277 static double g(double u) noexcept
278 {
279 const double a = std::abs(u);
280 if (a <= kHalfPi) return poly2(u);
281 const double e = a - kHalfPi;
282 return std::copysign(kEdge2 + kEdge1 * e + 0.5 * kEdge * e * e, u);
283 }
284 static double d1(double u) noexcept
285 {
286 const double a = std::abs(u);
287 return a <= kHalfPi ? poly1(u) : kEdge1 + kEdge * (a - kHalfPi);
288 }
289 static double d2(double u) noexcept { return poly(std::clamp(u, -kHalfPi, kHalfPi)); }
290 static double d3(double u) noexcept { return std::abs(u) < kHalfPi ? polySlope(u) : 0.0; }
291
292 static AdaaPoint point(double u) noexcept { return {u, g(u), 0.0}; }
293
294 static bool exact(const AdaaPoint& p0, const AdaaPoint& p1, const AdaaPoint& p2,
295 double& y) noexcept
296 {
297 const double lo = std::min(p0.u, std::min(p1.u, p2.u)), hi = std::max(p0.u, std::max(p1.u, p2.u));
298 if (lo >= kHalfPi) { y = kEdge; return true; }
299 if (hi <= -kHalfPi) { y = -kEdge; return true; }
300 return false;
301 }
302};
303
304// ---------------------------------------------------------------------------
305// Golden-ratio rational knee: f = u for |u| <= k = 1/phi, else
306// sign (1 - r^2 / w) with r = 1 - k and w = |u| - k + r.
307// ---------------------------------------------------------------------------
309{
310 static constexpr double kKnee = 0.61803398874989484820; // 1 / phi
311 static constexpr double kRange = 1.0 - kKnee;
312 static constexpr double kLogRange = -0.96242365011920689500; // ln(1 - 1/phi)
313
314 static double g(double u) noexcept
315 {
316 const double a = std::abs(u);
317 if (a <= kKnee) return u * u * u * (1.0 / 6.0);
318 const double e = a - kKnee, w = e + kRange;
319 return std::copysign(kKnee * kKnee * kKnee / 6.0 + 0.5 * kKnee * kKnee * e + 0.5 * e * e
320 - kRange * kRange * (w * (std::log(w) - kLogRange) - e), u);
321 }
322 static double d1(double u) noexcept
323 {
324 const double a = std::abs(u);
325 if (a <= kKnee) return 0.5 * u * u;
326 const double e = a - kKnee;
327 return 0.5 * kKnee * kKnee + e - kRange * kRange * std::log1p(e / kRange);
328 }
329 static double d2(double u) noexcept
330 {
331 const double a = std::abs(u);
332 return a <= kKnee ? u : std::copysign(1.0 - kRange * kRange / (a - kKnee + kRange), u);
333 }
334 static double d3(double u) noexcept
335 {
336 const double a = std::abs(u);
337 if (a <= kKnee) return 1.0;
338 const double w = a - kKnee + kRange;
339 return kRange * kRange / (w * w);
340 }
341
342 static AdaaPoint point(double u) noexcept
343 {
344 const double a = std::abs(u);
345 if (a <= kKnee) return {u, u * u * u * (1.0 / 6.0), 0.0};
346 const double e = a - kKnee, w = e + kRange, lw = std::log(w);
347 return {u, std::copysign(kKnee * kKnee * kKnee / 6.0 + 0.5 * kKnee * kKnee * e + 0.5 * e * e
348 - kRange * kRange * (w * (lw - kLogRange) - e), u), lw};
349 }
350
352 static double logSlope(double x, double y, double lx, double ly) noexcept
353 {
354 const double q = (x - y) / (x + y);
355 if (std::abs(q) > 0.05) return (lx - ly) / (x - y);
356 // ln(x/y) = 2 atanh(q): odd series in q, no cancellation.
357 const double q2 = q * q;
358 return 2.0 / (x + y)
359 * (1.0 + q2 * (1.0 / 3 + q2 * (1.0 / 5 + q2 * (1.0 / 7 + q2 * (1.0 / 9 + q2 * (1.0 / 11))))));
360 }
361
362 static bool exact(const AdaaPoint& p0, const AdaaPoint& p1, const AdaaPoint& p2,
363 double& y) noexcept
364 {
365 const double lo = std::min(p0.u, std::min(p1.u, p2.u)), hi = std::max(p0.u, std::max(p1.u, p2.u));
366 if (lo >= -kKnee && hi <= kKnee) { y = (p0.u + p1.u + p2.u) * (1.0 / 3.0); return true; }
367 const bool up = lo >= kKnee, down = hi <= -kKnee;
368 if (!(up || down) || !(hi - lo > AdaaSecondDifference<AdaaRationalKnee>::kTolDd2))
369 return false;
370 // Average of 1 - r^2/w over the B-spline with knots a <= b <= c in w:
371 // 2/(c - a) * (c ln(c/b)/(c - b) - a ln(b/a)/(b - a)).
372 // Knots in w = |u| - k + r, sorted with their logarithms.
373 double w0 = std::abs(p0.u) - kKnee + kRange, w1 = std::abs(p1.u) - kKnee + kRange;
374 double w2 = std::abs(p2.u) - kKnee + kRange, l0 = p0.aux, l1 = p1.aux, l2 = p2.aux;
375 if (w0 > w1) { std::swap(w0, w1); std::swap(l0, l1); }
376 if (w1 > w2) { std::swap(w1, w2); std::swap(l1, l2); }
377 if (w0 > w1) { std::swap(w0, w1); std::swap(l0, l1); }
378 const double h = 2.0 / (w2 - w0) * (w2 * logSlope(w2, w1, l2, l1) - w0 * logSlope(w1, w0, l1, l0));
379 y = std::copysign(1.0 - kRange * kRange * h, up ? 1.0 : -1.0);
380 return true;
381 }
382};
383
384// ---------------------------------------------------------------------------
385// sin (not saturating): F1 = -cos, F2 = -sin; bounded, so always generic.
386// ---------------------------------------------------------------------------
388{
389 static double g(double u) noexcept { return -std::sin(u); }
390 static double d1(double u) noexcept { return -std::cos(u); }
391 static double d2(double u) noexcept { return std::sin(u); }
392 static double d3(double u) noexcept { return std::cos(u); }
393 static AdaaPoint point(double u) noexcept { return {u, -std::sin(u), 0.0}; }
394 static bool exact(const AdaaPoint&, const AdaaPoint&, const AdaaPoint&, double&) noexcept
395 {
396 return false;
397 }
398};
399
410class Adaa2
411{
412public:
413 void reset(double u = 0.0) noexcept
414 {
415 u1_ = u2_ = u;
416 valid_ = false;
417 }
418
419 void track(double u) noexcept
420 {
421 u2_ = u1_;
422 u1_ = u;
423 valid_ = false;
424 }
425
426 void invalidate() noexcept { valid_ = false; }
427
428 template <class Curve>
429 [[nodiscard]] double process(double u) noexcept
430 {
431 double y = 0.0;
432 if constexpr (requires { Curve::kClosedForm; })
433 {
434 y = Curve::value(u, u1_, u2_);
435 }
436 else
437 {
438 if (!valid_)
439 {
440 p1_ = Curve::point(u1_);
441 p2_ = Curve::point(u2_);
442 valid_ = true;
443 }
444 const AdaaPoint p0 = Curve::point(u);
445 if (!Curve::exact(p0, p1_, p2_, y))
446 y = AdaaSecondDifference<Curve>::twice(p0.u, p1_.u, p2_.u, p0.g, p1_.g, p2_.g);
447 p2_ = p1_;
448 p1_ = p0;
449 }
450 u2_ = u1_;
451 u1_ = u;
452 return y;
453 }
454
456 [[nodiscard]] double previousInput() const noexcept { return u1_; }
457
458private:
459 AdaaPoint p1_ {}, p2_ {};
460 double u1_ = 0.0, u2_ = 0.0;
461 bool valid_ = false;
462};
463
464} // namespace dspark::detail
Per-stream second-order ADAA state.
Definition Adaa.h:411
void invalidate() noexcept
Definition Adaa.h:426
double process(double u) noexcept
Definition Adaa.h:429
void reset(double u=0.0) noexcept
Definition Adaa.h:413
double previousInput() const noexcept
Definition Adaa.h:456
void track(double u) noexcept
Definition Adaa.h:419
double integralLogCosh(double x) noexcept
Definition LogCosh.h:48
double logCosh(double x) noexcept
Stable log(cosh(x)), shared by the tanh antiderivative kernels.
Definition LogCosh.h:18
double integralLogCoshTail(double a) noexcept
Definition LogCosh.h:34
static double d2(double u) noexcept
Definition Adaa.h:107
static constexpr bool kClosedForm
Definition Adaa.h:114
static double d1(double u) noexcept
Definition Adaa.h:102
static AdaaPoint point(double u) noexcept
Definition Adaa.h:110
static double value(double u0, double u1, double u2) noexcept
Definition Adaa.h:115
static bool exact(const AdaaPoint &p0, const AdaaPoint &p1, const AdaaPoint &p2, double &y) noexcept
Definition Adaa.h:128
static double rampAbove(double a, double b, double c, double mean, double tau) noexcept
Definition Adaa.h:144
static double d3(double u) noexcept
Definition Adaa.h:108
static double g(double u) noexcept
Definition Adaa.h:96
static double rampBelow(double a, double b, double c, double mean, double tau) noexcept
Definition Adaa.h:158
double aux
Curve specific (tanh remainder, knee logarithm).
Definition Adaa.h:57
double u
Input.
Definition Adaa.h:55
double g
Second antiderivative F2(u).
Definition Adaa.h:56
static double logSlope(double x, double y, double lx, double ly) noexcept
Definition Adaa.h:352
static double d1(double u) noexcept
Definition Adaa.h:322
static double d2(double u) noexcept
Definition Adaa.h:329
static constexpr double kLogRange
Definition Adaa.h:312
static AdaaPoint point(double u) noexcept
Definition Adaa.h:342
static double g(double u) noexcept
Definition Adaa.h:314
static constexpr double kKnee
Definition Adaa.h:310
static bool exact(const AdaaPoint &p0, const AdaaPoint &p1, const AdaaPoint &p2, double &y) noexcept
Definition Adaa.h:362
static constexpr double kRange
Definition Adaa.h:311
static double d3(double u) noexcept
Definition Adaa.h:334
static constexpr double kTolDiv
Definition Adaa.h:65
static double div(double a, double b, double ga, double gb) noexcept
Definition Adaa.h:68
static double twice(double x0, double x1, double x2, double g0, double g1, double g2) noexcept
Definition Adaa.h:77
static constexpr double kTolDd2
Definition Adaa.h:66
static constexpr double poly2(double v) noexcept
Definition Adaa.h:258
static constexpr double poly1(double v) noexcept
Definition Adaa.h:252
static constexpr double poly(double v) noexcept
Definition Adaa.h:246
static constexpr double polySlope(double v) noexcept
Definition Adaa.h:264
static double d1(double u) noexcept
Definition Adaa.h:284
static double d2(double u) noexcept
Definition Adaa.h:289
static double g(double u) noexcept
Definition Adaa.h:277
static AdaaPoint point(double u) noexcept
Definition Adaa.h:292
static constexpr double kEdge1
Definition Adaa.h:275
static double d3(double u) noexcept
Definition Adaa.h:290
static bool exact(const AdaaPoint &p0, const AdaaPoint &p1, const AdaaPoint &p2, double &y) noexcept
Definition Adaa.h:294
static constexpr double kHalfPi
Definition Adaa.h:274
static constexpr double kEdge
Definition Adaa.h:275
static constexpr double kEdge2
Definition Adaa.h:275
static AdaaPoint point(double u) noexcept
Definition Adaa.h:393
static double g(double u) noexcept
Definition Adaa.h:389
static double d1(double u) noexcept
Definition Adaa.h:390
static double d2(double u) noexcept
Definition Adaa.h:391
static double d3(double u) noexcept
Definition Adaa.h:392
static bool exact(const AdaaPoint &, const AdaaPoint &, const AdaaPoint &, double &) noexcept
Definition Adaa.h:394
static double d3(double u) noexcept
Definition Adaa.h:203
static double d2(double u) noexcept
Definition Adaa.h:197
static double d1(double u) noexcept
Definition Adaa.h:196
static double g(double u) noexcept
Definition Adaa.h:191
static constexpr double kOuter
Definition Adaa.h:179
static double g(double u) noexcept
Definition Adaa.h:181
static double d2(double u) noexcept
Definition Adaa.h:183
static double tail(double a) noexcept
Definition Adaa.h:208
static bool exact(const AdaaPoint &p0, const AdaaPoint &p1, const AdaaPoint &p2, double &y) noexcept
Definition Adaa.h:228
static double d3(double u) noexcept
Definition Adaa.h:184
static double d1(double u) noexcept
Definition Adaa.h:182
static AdaaPoint point(double u) noexcept
Definition Adaa.h:219