56 void prepare(
int numBins,
double widthOctaves = 1.0 / 3.0,
double minBins = 1.0)
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));
67 if (!std::isfinite(widthOctaves))
return;
68 width_ = std::clamp(widthOctaves, 0.05, 4.0);
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)
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));
92 passes_ = passes >= 2 ? 2 : 1;
104 void setTaper(
double cornerBin,
double exponent)
noexcept
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);
112 [[nodiscard]]
int getPasses() const noexcept {
return passes_; }
114 [[nodiscard]]
double getWidth() const noexcept {
return width_; }
122 void compute(
const T* in, T* out)
const noexcept
124 if (out != in) std::copy(in, in + n_, out);
125 computeInterleaved<1>(out);
138 static_assert(Lanes >= 1 && Lanes <= 8);
140 const T* coeff = coeff_.data();
141 for (
int pass = 0; pass < passes_; ++pass)
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)
147 T* v = values + Lanes * k;
148 for (
int j = 0; j < Lanes; ++j)
150 const auto u =
static_cast<size_t>(j);
151 y[u] = v[j] + coeff[k] * (y[u] - v[j]);
155 for (
int k = n_ - 2; k >= 0; --k)
157 T* v = values + Lanes * k;
158 for (
int j = 0; j < Lanes; ++j)
160 const auto u =
static_cast<size_t>(j);
161 y[u] = v[j] + coeff[k] * (y[u] - v[j]);
178 for (
int k = 0; k < n_; ++k) out[k] = std::max(T(0), in[k] - out[k]);
187 void excess(
const T* in, T* out)
const noexcept
190 for (
int k = 0; k < n_; ++k) out[k] = in[k] - out[k];
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_;
223template <FloatType T>
236 void prepare(
const double* window,
int windowSize,
int transform,
double widthOctaves = 1.0 / 3.0,
double minBins = 1.0)
238 n_ = std::max(2, windowSize);
239 m_ = std::max(n_, transform);
243 for (
int i = 0; i < n_; ++i)
245 if (window[i] > window[peak]) peak = i;
246 energy += window[i] * window[i];
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)
261 const double bins = std::max(least, span * k);
262 coeff_[
static_cast<size_t>(k)] = bins / (bins + 2.0);
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);
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) {
274 const double top = (1.0 - c) * (1.0 - c), flat = 1.0 + c * c;
275 for (
int i = 0; i < n_; ++i)
277 const double w = window[i] * top / (flat - 2.0 * c * cosine[
static_cast<size_t>(i)]);
280 return own > 0.0 ? energy / own : 1.0;
284 constexpr int kKnots = 48;
285 const auto widthOf = [](
double c) {
return 2.0 * c / (1.0 - c); };
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)
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)));
293 for (
int k = 0; k < bins_; ++k)
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)]));
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))]; }
312 void smooth(
const double* spectrum,
double* re,
double* im)
const noexcept
314 for (
int k = 0; k < bins_; ++k)
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];
321 for (
int k = 1; k < bins_; ++k)
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]);
327 for (
int k = bins_ - 2; k >= 0; --k)
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]);
341 void power(
const double* spectrum, T* out)
noexcept
343 smooth(spectrum, re_.data(), im_.data());
344 for (
int k = 0; k < bins_; ++k)
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]));
352 int n_ = 0, m_ = 0, bins_ = 0, peak_ = 0;
353 std::vector<double> coeff_, turnRe_, turnIm_, gain_, re_, im_;
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.