Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
polyblep.hpp
Go to the documentation of this file.
1/**
2 * @file polyblep.hpp
3 * @brief Polynomial BLEP kernels, and the sub-frame time of a crossing
4 *
5 * A BLEP kernel band-limits a discontinuity at a known time between frames.
6 * The kernel is the smoothing function the naive waveform is
7 * convolved with before sampling. Its integral is the band-limited step, and
8 * where that differs from the naive step on the frames the kernel touches is
9 * the step residual; the slope residual is the same for a corner, from the
10 * kernel's second integral. Corrected output is the naive waveform convolved
11 * with the kernel, and residuals from events closer than a kernel's span add
12 * linearly. A non-negative kernel with unit area, as Bspline4 and Linear2 are,
13 * never takes the output outside the naive range. Sinc16 rings: its step
14 * overshoots by 8.2 % of the step's height on either side, so a pulse between
15 * -1 and 1 reaches 1.16.
16 *
17 * A kernel is a policy type for BlepCorrector<Kernel>:
18 * @code
19 * static constexpr uint8_t FRAMES; // frames the kernel touches
20 * static constexpr uint8_t LATENCY; // of those, how many lie before the event
21 * static constexpr BlepResidual<FRAMES> step(float t); // a unit step
22 * static constexpr BlepResidual<FRAMES> slope(float t); // a unit slope change per frame
23 * @endcode
24 *
25 * `t` is the fraction of a frame elapsed since the event, read at the first
26 * frame after it, in [0, 1). `at[i]` corrects frame i - LATENCY relative to
27 * that frame; a caller adds `height * at[i]` for a step of `height` and
28 * `delta * at[i]` for a slope change of `delta` per frame, so its output
29 * trails the naive waveform by LATENCY frames.
30 */
31
32#ifndef SBL_DSP_MATH_POLYBLEP_HPP_
33#define SBL_DSP_MATH_POLYBLEP_HPP_
34
35#include <cstdint>
37
38namespace sbl::dsp::math {
39
40/** @brief The largest float below 1, the upper bound of a sub-frame time. */
41inline constexpr float POLYBLEP_T_MAX = 0.99999994f;
42
43/**
44 * @brief A kernel's corrections for the N frames it touches, oldest first.
45 *
46 * @tparam N Frames the kernel touches
47 */
48template<uint8_t N>
50 float at[N]; /**< at[i] corrects frame i - LATENCY relative to the first frame after the event */
51};
52
53/**
54 * @brief The cubic B-spline over four frames, two before the event and two after.
55 *
56 * From Valimaki, Pekonen and Nam (2012), Table VII, and Esqueda, Valimaki
57 * and Bilbao (2016), Table 1. A harmonic at f is scaled by sinc^4(f / fs).
58 * The residuals match the numeric integrals of the B-spline in
59 * test_polyblep.hpp.
60 */
61struct Bspline4 {
62 static constexpr uint8_t FRAMES = 4; /**< Frames the kernel touches */
63 static constexpr uint8_t LATENCY = 2; /**< Frames before the event */
64
65 /**
66 * @brief The residual of a unit step.
67 *
68 * At t = 0 the residual is {0, 1/24, -1/2, -1/24}: the step response at integer offsets, less the step.
69 *
70 * @param t Fraction of a frame since the event, in [0, 1)
71 * @return Corrections for the four frames, oldest first
72 */
73 static constexpr BlepResidual<4> step(float t) {
74 const float t2 = t * t;
75 const float t3 = t2 * t;
76 const float t4 = t3 * t;
77 const float u = 1.0f - t;
78 const float u2 = u * u;
79 return {{
80 t4 * (1.0f / 24.0f),
81 -t4 * (1.0f / 8.0f) + t3 * (1.0f / 6.0f) + t2 * (1.0f / 4.0f) + t * (1.0f / 6.0f) + (1.0f / 24.0f),
82 t4 * (1.0f / 8.0f) - t3 * (1.0f / 3.0f) + t * (2.0f / 3.0f) - 0.5f,
83 -(u2 * u2) * (1.0f / 24.0f),
84 }};
85 }
86
87 /**
88 * @brief The residual of a unit slope change per frame.
89 *
90 * Non-negative: a smoothed convex corner lies above the naive one on both sides.
91 *
92 * @param t Fraction of a frame since the event, in [0, 1)
93 * @return Corrections for the four frames, oldest first
94 */
95 static constexpr BlepResidual<4> slope(float t) {
96 const float t2 = t * t;
97 const float t3 = t2 * t;
98 const float t4 = t3 * t;
99 const float t5 = t4 * t;
100 const float u = 1.0f - t;
101 const float u2 = u * u;
102 return {{
103 t5 * (1.0f / 120.0f),
104 -t5 * (1.0f / 40.0f) + t4 * (1.0f / 24.0f) + t3 * (1.0f / 12.0f) + t2 * (1.0f / 12.0f) +
105 t * (1.0f / 24.0f) + (1.0f / 120.0f),
106 t5 * (1.0f / 40.0f) - t4 * (1.0f / 12.0f) + t2 * (1.0f / 3.0f) - t * 0.5f + (7.0f / 30.0f),
107 u2 * u2 * u * (1.0f / 120.0f),
108 }};
109 }
110};
111
112/**
113 * @brief The triangular kernel over two frames, one before the event and one after: Plaits' polyBLEP.
114 *
115 * The kernel of stmlib/dsp/polyblep.h (MIT). A harmonic at f is scaled by
116 * sinc^2(f / fs). The residuals match the numeric integrals of the B-spline
117 * in test_polyblep.hpp.
118 */
119struct Linear2 {
120 static constexpr uint8_t FRAMES = 2; /**< Frames the kernel touches */
121 static constexpr uint8_t LATENCY = 1; /**< Frames before the event */
122
123 /**
124 * @brief The residual of a unit step; at t = 0 it is {0, -1/2}.
125 *
126 * @param t Fraction of a frame since the event, in [0, 1)
127 * @return Corrections for the two frames, oldest first
128 */
129 static constexpr BlepResidual<2> step(float t) {
130 const float u = 1.0f - t;
131 return {{0.5f * t * t, -0.5f * u * u}};
132 }
133
134 /**
135 * @brief The residual of a unit slope change per frame.
136 *
137 * @param t Fraction of a frame since the event, in [0, 1)
138 * @return Corrections for the two frames, oldest first
139 */
140 static constexpr BlepResidual<2> slope(float t) {
141 const float u = 1.0f - t;
142 return {{t * t * t * (1.0f / 6.0f), u * u * u * (1.0f / 6.0f)}};
143 }
144};
145
146/**
147 * @brief A Kaiser-windowed sinc over sixteen frames, eight before the event and eight after.
148 *
149 * The cores' default: a sinc low-pass at 0.40 of the sample rate under a
150 * Kaiser window of beta 7. At 48 kHz it is within 0.4 dB to 15 kHz and
151 * 3.3 dB down at 18 kHz, where Bspline4 is 5.8 and 8.4 dB down. Its
152 * residuals are degree-7 polynomials per frame, generated with
153 * scripts/generate_sinc_kernel.py into sinc16_coefficients.hpp, and match
154 * the numeric integrals of the windowed sinc in test_polyblep.hpp.
155 */
156struct Sinc16 {
157 static constexpr uint8_t FRAMES = sinc16::FRAMES; /**< Frames the kernel touches */
158 static constexpr uint8_t LATENCY = FRAMES / 2; /**< Frames before the event */
159
160 /**
161 * @brief The residual of a unit step, from sinc16::STEP.
162 *
163 * @param t Fraction of a frame since the event, in [0, 1)
164 * @return Corrections for the sixteen frames, oldest first
165 */
166 static constexpr BlepResidual<FRAMES> step(float t) { return evaluate(sinc16::STEP, t); }
167
168 /**
169 * @brief The residual of a unit slope change per frame, from sinc16::SLOPE.
170 *
171 * @param t Fraction of a frame since the event, in [0, 1)
172 * @return Corrections for the sixteen frames, oldest first
173 */
174 static constexpr BlepResidual<FRAMES> slope(float t) { return evaluate(sinc16::SLOPE, t); }
175
176private:
177 static constexpr uint8_t TOP = sinc16::DEGREE; /**< the index of a polynomial's leading coefficient */
178
179 /** Eight frames' values, the group evaluate() advances together. */
180 struct Group {
181 float v[8];
182 };
183
184 /**
185 * @brief Frames i .. i + 7 of a table, each its polynomial in s by Horner's rule
186 *
187 * The eight chains advance together in eight variables. On the Cortex-M7 a step of a chain waits
188 * several cycles on the step before it, and chains overlap only while they stay in registers: one
189 * chain at a time cost 1258 cycles for the sixteen frames, and sixteen together through memory 1238
190 * (sound-byte-libs#158).
191 */
192 static constexpr Group horner8(const float (&c)[FRAMES][sinc16::DEGREE + 1], uint8_t i, float s) {
193 float v0 = c[i][TOP];
194 float v1 = c[i + 1][TOP];
195 float v2 = c[i + 2][TOP];
196 float v3 = c[i + 3][TOP];
197 float v4 = c[i + 4][TOP];
198 float v5 = c[i + 5][TOP];
199 float v6 = c[i + 6][TOP];
200 float v7 = c[i + 7][TOP];
201 for (int k = TOP - 1; k >= 0; --k) {
202 v0 = v0 * s + c[i][k];
203 v1 = v1 * s + c[i + 1][k];
204 v2 = v2 * s + c[i + 2][k];
205 v3 = v3 * s + c[i + 3][k];
206 v4 = v4 * s + c[i + 4][k];
207 v5 = v5 * s + c[i + 5][k];
208 v6 = v6 * s + c[i + 6][k];
209 v7 = v7 * s + c[i + 7][k];
210 }
211 return {{v0, v1, v2, v3, v4, v5, v6, v7}};
212 }
213
214 /** Each frame's polynomial in s = 2t - 1, eight frames at a time. */
215 static constexpr BlepResidual<FRAMES> evaluate(const float (&c)[FRAMES][sinc16::DEGREE + 1], float t) {
216 static_assert(FRAMES == 16, "SBL ERROR: Sinc16::evaluate takes its frames as two groups of eight");
217 const float s = 2.0f * t - 1.0f;
218 const Group a = horner8(c, 0, s);
219 const Group b = horner8(c, 8, s);
220 return {{a.v[0], a.v[1], a.v[2], a.v[3], a.v[4], a.v[5], a.v[6], a.v[7], b.v[0], b.v[1], b.v[2], b.v[3], b.v[4],
221 b.v[5], b.v[6], b.v[7]}};
222 }
223};
224
225/**
226 * @brief Sub-frame time at which a linear segment crossed a moving threshold
227 *
228 * A value went from `before` to `after` over one frame while the threshold
229 * went from `threshold_before` to `threshold_after`; the caller knows the
230 * segment crossed. Returns the fraction of the frame elapsed since the
231 * crossing, read at `after`: t = (after - th_after) / ((after - before) -
232 * (th_after - th_before)). A moving threshold is a pulse edge under PWM.
233 *
234 * @param before Value at the frame before
235 * @param after Value at the frame after
236 * @param threshold_before Threshold at the frame before
237 * @param threshold_after Threshold at the frame after
238 * @return Fraction of a frame since the crossing, clamped to [0, POLYBLEP_T_MAX];
239 * 0 when the value and the threshold moved by the same amount, and NaN
240 * for a NaN input
241 */
242constexpr float polyblep_time(float before, float after, float threshold_before, float threshold_after) {
243 const float denominator = (after - before) - (threshold_after - threshold_before);
244 if (denominator == 0.0f) {
245 return 0.0f;
246 }
247 const float t = (after - threshold_after) / denominator;
248 return t < 0.0f ? 0.0f : (t > POLYBLEP_T_MAX ? POLYBLEP_T_MAX : t);
249}
250
251/**
252 * @brief Sub-frame time at which a linear segment crossed a fixed threshold
253 *
254 * For a phase wrap, pass the phase before the wrap less one cycle as
255 * `before`, the wrapped phase as `after` and 0 as the threshold.
256 *
257 * @param before Value at the frame before
258 * @param after Value at the frame after
259 * @param threshold Threshold for both frames
260 * @return Fraction of a frame since the crossing, in [0, POLYBLEP_T_MAX];
261 * 0 when the value did not move
262 */
263constexpr float polyblep_time(float before, float after, float threshold) {
264 return polyblep_time(before, after, threshold, threshold);
265}
266
267} // namespace sbl::dsp::math
268
269#endif // SBL_DSP_MATH_POLYBLEP_HPP_
Math for the audio stack.
Definition crossfade.hpp:18
constexpr float polyblep_time(float before, float after, float threshold_before, float threshold_after)
Sub-frame time at which a linear segment crossed a moving threshold.
Definition polyblep.hpp:242
constexpr float POLYBLEP_T_MAX
The largest float below 1, the upper bound of a sub-frame time.
Definition polyblep.hpp:41
Generated by scripts/generate_sinc_kernel.py; do not edit.
A kernel's corrections for the N frames it touches, oldest first.
Definition polyblep.hpp:49
The cubic B-spline over four frames, two before the event and two after.
Definition polyblep.hpp:61
static constexpr BlepResidual< 4 > slope(float t)
The residual of a unit slope change per frame.
Definition polyblep.hpp:95
static constexpr uint8_t LATENCY
Definition polyblep.hpp:63
static constexpr uint8_t FRAMES
Definition polyblep.hpp:62
static constexpr BlepResidual< 4 > step(float t)
The residual of a unit step.
Definition polyblep.hpp:73
The triangular kernel over two frames, one before the event and one after: Plaits' polyBLEP.
Definition polyblep.hpp:119
static constexpr uint8_t FRAMES
Definition polyblep.hpp:120
static constexpr BlepResidual< 2 > slope(float t)
The residual of a unit slope change per frame.
Definition polyblep.hpp:140
static constexpr uint8_t LATENCY
Definition polyblep.hpp:121
static constexpr BlepResidual< 2 > step(float t)
The residual of a unit step; at t = 0 it is {0, -1/2}.
Definition polyblep.hpp:129
A Kaiser-windowed sinc over sixteen frames, eight before the event and eight after.
Definition polyblep.hpp:156
static constexpr uint8_t LATENCY
Definition polyblep.hpp:158
static constexpr uint8_t FRAMES
Definition polyblep.hpp:157
static constexpr BlepResidual< FRAMES > step(float t)
The residual of a unit step, from sinc16::STEP.
Definition polyblep.hpp:166
static constexpr BlepResidual< FRAMES > slope(float t)
The residual of a unit slope change per frame, from sinc16::SLOPE.
Definition polyblep.hpp:174