DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
SpectralEnvelope.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
29#include "DspMath.h"
30
31#include <algorithm>
32#include <array>
33#include <cmath>
34#include <vector>
35
36namespace dspark {
37
44template <FloatType T>
46{
47public:
56 void prepare(int numBins, double widthOctaves = 1.0 / 3.0, double minBins = 1.0)
57 {
58 n_ = std::max(2, numBins);
59 minBins_ = std::isfinite(minBins) ? std::max(1.0, minBins) : 1.0;
60 coeff_.assign(static_cast<size_t>(n_), T(0));
61 setWidth(widthOctaves);
62 }
63
65 void setWidth(double widthOctaves) noexcept
66 {
67 if (!std::isfinite(widthOctaves)) return;
68 width_ = std::clamp(widthOctaves, 0.05, 4.0);
69 // A one-pole run both ways has an equivalent width of about
70 // 2 c / (1 - c) bins; the width wanted at bin k is k (2^(w/2) -
71 // 2^(-w/2)) bins, at least minBins_.
72 // Two passes of a smoothing 1 / sqrt(2) as wide have the same
73 // equivalent width as one, with far less of a tail.
74 const double span = (std::pow(2.0, 0.5 * width_) - std::pow(2.0, -0.5 * width_)) / std::sqrt(static_cast<double>(passes_));
75 for (int k = 0; k < n_; ++k)
76 {
77 double bins = span * k;
78 if (taperExponent_ > 0.0 && taperBin_ > 0.0 && k > taperBin_) bins *= std::pow(taperBin_ / k, taperExponent_);
79 bins = std::max(minBins_, bins);
80 coeff_[static_cast<size_t>(k)] = static_cast<T>(bins / (bins + 2.0));
81 }
82 }
83
90 void setPasses(int passes) noexcept
91 {
92 passes_ = passes >= 2 ? 2 : 1;
93 setWidth(width_);
94 }
95
104 void setTaper(double cornerBin, double exponent) noexcept
105 {
106 if (!std::isfinite(cornerBin) || !std::isfinite(exponent)) return;
107 taperBin_ = std::max(0.0, cornerBin);
108 taperExponent_ = std::clamp(exponent, 0.0, 2.0);
109 setWidth(width_);
110 }
111
112 [[nodiscard]] int getPasses() const noexcept { return passes_; }
113
114 [[nodiscard]] double getWidth() const noexcept { return width_; }
115 [[nodiscard]] int getNumBins() const noexcept { return n_; }
116
122 void compute(const T* in, T* out) const noexcept
123 {
124 if (out != in) std::copy(in, in + n_, out);
125 computeInterleaved<1>(out);
126 }
127
135 template <int Lanes>
136 void computeInterleaved(T* values) const noexcept
137 {
138 static_assert(Lanes >= 1 && Lanes <= 8);
139 if (n_ < 2) return;
140 const T* coeff = coeff_.data();
141 for (int pass = 0; pass < passes_; ++pass)
142 {
143 std::array<T, static_cast<size_t>(Lanes)> y;
144 std::copy_n(values, Lanes, y.data());
145 for (int k = 1; k < n_; ++k)
146 {
147 T* v = values + Lanes * k;
148 for (int j = 0; j < Lanes; ++j)
149 {
150 const auto u = static_cast<size_t>(j);
151 y[u] = v[j] + coeff[k] * (y[u] - v[j]);
152 v[j] = y[u];
153 }
154 }
155 for (int k = n_ - 2; k >= 0; --k)
156 {
157 T* v = values + Lanes * k;
158 for (int j = 0; j < Lanes; ++j)
159 {
160 const auto u = static_cast<size_t>(j);
161 y[u] = v[j] + coeff[k] * (y[u] - v[j]);
162 v[j] = y[u];
163 }
164 }
165 }
166 }
167
175 void resonances(const T* in, T* out) const noexcept
176 {
177 compute(in, out);
178 for (int k = 0; k < n_; ++k) out[k] = std::max(T(0), in[k] - out[k]);
179 }
180
187 void excess(const T* in, T* out) const noexcept
188 {
189 compute(in, out);
190 for (int k = 0; k < n_; ++k) out[k] = in[k] - out[k];
191 }
192
193private:
194 int n_ = 0, passes_ = 1;
195 double width_ = 1.0 / 3.0, minBins_ = 1.0, taperBin_ = 0.0, taperExponent_ = 0.0;
196 std::vector<T> coeff_;
197};
198
223template <FloatType T>
225{
226public:
236 void prepare(const double* window, int windowSize, int transform, double widthOctaves = 1.0 / 3.0, double minBins = 1.0)
237 {
238 n_ = std::max(2, windowSize);
239 m_ = std::max(n_, transform);
240 bins_ = m_ / 2 + 1;
241 int peak = 0;
242 double energy = 0.0;
243 for (int i = 0; i < n_; ++i)
244 {
245 if (window[i] > window[peak]) peak = i;
246 energy += window[i] * window[i];
247 }
248 peak_ = peak;
249 const double width = std::clamp(std::isfinite(widthOctaves) ? widthOctaves : 1.0 / 3.0, 0.05, 4.0);
250 const double least = std::isfinite(minBins) ? std::max(1.0, minBins) : 1.0;
251 const double span = std::pow(2.0, 0.5 * width) - std::pow(2.0, -0.5 * width);
252 coeff_.resize(static_cast<size_t>(bins_));
253 turnRe_.resize(static_cast<size_t>(bins_));
254 turnIm_.resize(static_cast<size_t>(bins_));
255 gain_.resize(static_cast<size_t>(bins_));
256 re_.assign(static_cast<size_t>(bins_), 0.0);
257 im_.assign(static_cast<size_t>(bins_), 0.0);
258 const double step = 2.0 * 3.141592653589793 / static_cast<double>(m_);
259 for (int k = 0; k < bins_; ++k)
260 {
261 const double bins = std::max(least, span * k);
262 coeff_[static_cast<size_t>(k)] = bins / (bins + 2.0);
263 // The origin of time moved to the window's peak: a turn of the phase by bin.
264 const double angle = step * static_cast<double>((static_cast<long long>(k) * peak) % m_);
265 turnRe_[static_cast<size_t>(k)] = std::cos(angle);
266 turnIm_[static_cast<size_t>(k)] = std::sin(angle);
267 }
268 // The energy of the whole window over that of each bin's own: found at
269 // a few coefficients (it moves slowly with the bin) and read between them.
270 std::vector<double> cosine(static_cast<size_t>(n_));
271 for (int i = 0; i < n_; ++i) cosine[static_cast<size_t>(i)] = std::cos(step * static_cast<double>(i - peak));
272 const auto gainOf = [&](double c) {
273 double own = 0.0;
274 const double top = (1.0 - c) * (1.0 - c), flat = 1.0 + c * c;
275 for (int i = 0; i < n_; ++i)
276 {
277 const double w = window[i] * top / (flat - 2.0 * c * cosine[static_cast<size_t>(i)]);
278 own += w * w;
279 }
280 return own > 0.0 ? energy / own : 1.0;
281 };
282 // The knots sit evenly in the logarithm of the smoothing's width, where
283 // the logarithm of the gain is close to a line.
284 constexpr int kKnots = 48;
285 const auto widthOf = [](double c) { return 2.0 * c / (1.0 - c); }; // the smoothing's width in bins
286 const double wLow = std::log(widthOf(coeff_.front())), wHigh = std::log(widthOf(coeff_.back()));
287 std::array<double, kKnots> knot {};
288 for (int j = 0; j < kKnots; ++j)
289 {
290 const double bins = std::exp(wLow + (wHigh - wLow) * j / (kKnots - 1.0));
291 knot[static_cast<size_t>(j)] = std::log(gainOf(bins / (bins + 2.0)));
292 }
293 for (int k = 0; k < bins_; ++k)
294 {
295 const double x = wHigh > wLow ? (std::log(widthOf(coeff_[static_cast<size_t>(k)])) - wLow) / (wHigh - wLow) * (kKnots - 1.0) : 0.0;
296 const int j = std::clamp(static_cast<int>(x), 0, kKnots - 2);
297 const double t = x - j;
298 gain_[static_cast<size_t>(k)] = std::exp(knot[static_cast<size_t>(j)] + t * (knot[static_cast<size_t>(j + 1)] - knot[static_cast<size_t>(j)]));
299 }
300 }
301
302 [[nodiscard]] int getNumBins() const noexcept { return bins_; }
304 [[nodiscard]] double getGain(int bin) const noexcept { return gain_[static_cast<size_t>(std::clamp(bin, 0, bins_ - 1))]; }
305
312 void smooth(const double* spectrum, double* re, double* im) const noexcept
313 {
314 for (int k = 0; k < bins_; ++k)
315 {
316 const auto i = static_cast<size_t>(k);
317 const double xr = spectrum[2 * k], xi = spectrum[2 * k + 1];
318 re[k] = xr * turnRe_[i] - xi * turnIm_[i];
319 im[k] = xr * turnIm_[i] + xi * turnRe_[i];
320 }
321 for (int k = 1; k < bins_; ++k)
322 {
323 const double c = coeff_[static_cast<size_t>(k)];
324 re[k] += c * (re[k - 1] - re[k]);
325 im[k] += c * (im[k - 1] - im[k]);
326 }
327 for (int k = bins_ - 2; k >= 0; --k)
328 {
329 const double c = coeff_[static_cast<size_t>(k)];
330 re[k] += c * (re[k + 1] - re[k]);
331 im[k] += c * (im[k + 1] - im[k]);
332 }
333 }
334
341 void power(const double* spectrum, T* out) noexcept
342 {
343 smooth(spectrum, re_.data(), im_.data());
344 for (int k = 0; k < bins_; ++k)
345 {
346 const auto i = static_cast<size_t>(k);
347 out[k] = static_cast<T>(gain_[i] * (re_[i] * re_[i] + im_[i] * im_[i]));
348 }
349 }
350
351private:
352 int n_ = 0, m_ = 0, bins_ = 0, peak_ = 0;
353 std::vector<double> coeff_, turnRe_, turnIm_, gain_, re_, im_;
354};
355
356} // namespace dspark
Envelope of a spectrum, smoothing proportional to frequency.
void setTaper(double cornerBin, double exponent) noexcept
Narrows the smoothing above a corner: its width in octaves falls as (corner / frequency) to the expon...
void compute(const T *in, T *out) const noexcept
The envelope of a spectrum.
void setWidth(double widthOctaves) noexcept
Width of the smoothing in octaves (0.05 to 4; non-finite values are ignored).
void prepare(int numBins, double widthOctaves=1.0/3.0, double minBins=1.0)
Sets the bins and the width.
void computeInterleaved(T *values) const noexcept
The envelopes of several spectra at once, in place: each what compute() gives for its own....
void setPasses(int passes) noexcept
Passes of the smoothing: 1 (the default) or 2. Two passes keep the equivalent width and lose the long...
double getWidth() const noexcept
int getNumBins() const noexcept
void resonances(const T *in, T *out) const noexcept
The resonances, never negative: given decibels, the spectrum over its envelope in decibels (soft); gi...
void excess(const T *in, T *out) const noexcept
The spectrum less its envelope, with its sign (what stands over the envelope is positive,...
int getPasses() const noexcept
A frame's spectrum read through a window that shortens as frequency rises, from the one transform of ...
void smooth(const double *spectrum, double *re, double *im) const noexcept
The smoothed complex spectrum of a frame.
void prepare(const double *window, int windowSize, int transform, double widthOctaves=1.0/3.0, double minBins=1.0)
Sets the window, the transform and the width.
void power(const double *spectrum, T *out) noexcept
The power of each bin through its own window, scaled to read as through the whole one.
int getNumBins() const noexcept
double getGain(int bin) const noexcept
Main namespace for the DSPark framework.