26#include "DenormalGuard.h"
43 constexpr double kPi = 3.14159265358979323846;
44 return index % 2 == 0 ? 0.0 : 2.0 / (kPi *
static_cast<double>(index));
90 assert(sampleRate > 0.0);
91 if (!(sampleRate > 0.0))
return;
93 sampleRate_ = sampleRate;
116 const int side = parity_;
117 T* line = lines_[
static_cast<size_t>(side)].data();
118 const int wp = writePos_[
static_cast<size_t>(side)];
120 line[wp + kNonZero] = input;
126 writePos_[
static_cast<size_t>(side)] = (wp + 1 == kNonZero) ? 0 : (wp + 1);
130 const int other = side ^ 1;
131 const int op = writePos_[
static_cast<size_t>(other)];
132 const T re = lines_[
static_cast<size_t>(other)]
133 [
static_cast<size_t>(op + kNonZero - 1 - (kCenter - 1) / 2)];
145 std::span<T> outReal,
146 std::span<T> outImag)
noexcept
149 assert(outReal.size() >= input.size() && outImag.size() >= input.size());
152 const size_t numSamples =
153 std::min(input.size(), std::min(outReal.size(), outImag.size()));
154 for (
size_t i = 0; i < numSamples; ++i)
156 const auto res =
process(input[i]);
157 outReal[i] = res.real;
158 outImag[i] = res.imag;
165 for (
auto& l : lines_) l.fill(T(0));
166 writePos_ = { 0, 0 };
176 static constexpr int kTaps = 191;
177 static constexpr int kCenter = kTaps / 2;
178 static constexpr int kNonZero = (kTaps + 1) / 2;
183 [[nodiscard]]
static const std::array<T, kNonZero>& nonZeroKernel() noexcept
185 static const std::array<T, kNonZero> kernel = []
187 std::array<T, kNonZero> k {};
188 constexpr double kPi = 3.14159265358979323846;
189 for (
int m = 0; m < kNonZero; ++m)
193 const int lag = 2 * (kNonZero - 1 - m);
195 const double w = 0.42
196 - 0.5 * std::cos(2.0 * kPi *
static_cast<double>(lag) /
static_cast<double>(kTaps - 1))
197 + 0.08 * std::cos(4.0 * kPi *
static_cast<double>(lag) /
static_cast<double>(kTaps - 1));
198 k[
static_cast<size_t>(m)] =
static_cast<T
>(ideal * w);
205 double sampleRate_ = 0.0;
206 bool isPrepared_ =
false;
208 std::array<int, 2> writePos_ { 0, 0 };
212 const T* kernelData_ = nonZeroKernel().data();
217 std::array<std::array<T, kNonZero * 2>, 2> lines_ {};
243template <FloatType T>
256 for (
auto& s : sections_) s = {};
263 double re = 0.0, im = 0.0;
264 step(
static_cast<double>(input), re, im);
265 return {
static_cast<T
>(re),
static_cast<T
>(im) };
272 double re = 0.0, im = 0.0;
273 step(
static_cast<double>(input), re, im);
274 return static_cast<T
>(std::sqrt(re * re + im * im));
287 constexpr int kChunk = 64;
290 for (
int start = 0; start < numSamples; start += kChunk)
292 const int n = std::min(kChunk, numSamples - start);
293 for (
int i = 0; i < n; ++i)
294 re[i] = im[i] =
static_cast<double>(in[start + i]);
295 for (
int k = 0; k < kSections; ++k)
296 Section::runPair(sections_[
static_cast<size_t>(k)], re, kReal[k],
297 sections_[
static_cast<size_t>(kSections + k)], im, kImag[k], n);
298 for (
int i = 0; i < n; ++i)
300 const double imDelayed = delayedImag_;
301 delayedImag_ = im[i];
302 out[start + i] =
static_cast<T
>(std::sqrt(re[i] * re[i] + imDelayed * imDelayed));
308 static constexpr int kSections = 7;
310 inline void step(
double input,
double& re,
double& im)
noexcept
314 for (
int k = 0; k < kSections; ++k)
316 re = sections_[
static_cast<size_t>(k)].
process(re, kReal[k]);
317 quad = sections_[
static_cast<size_t>(kSections + k)].
process(quad, kImag[k]);
326 double x1 = 0.0, x2 = 0.0, y1 = 0.0, y2 = 0.0;
330 inline double process(
double x,
double c)
noexcept
332 const double y = (c * x - x2) + c * y2;
341 static inline void runPair(Section& p,
double* dp,
double cp,
342 Section& q,
double* dq,
double cq,
int n)
noexcept
344 double px1 = p.x1, px2 = p.x2, py1 = p.y1, py2 = p.y2;
345 double qx1 = q.x1, qx2 = q.x2, qy1 = q.y1, qy2 = q.y2;
346 for (
int i = 0; i < n; ++i)
348 const double xp = dp[i];
349 const double xq = dq[i];
350 const double yp = (cp * xp - px2) + cp * py2;
351 const double yq = (cq * xq - qx2) + cq * qy2;
352 px2 = px1; px1 = xp; py2 = py1; py1 = yp;
353 qx2 = qx1; qx1 = xq; qy2 = qy1; qy1 = yq;
357 p.x1 = px1; p.x2 = px2; p.y1 = py1; p.y2 = py2;
358 q.x1 = qx1; q.x2 = qx2; q.y1 = qy1; q.y2 = qy2;
362 static constexpr double kReal[kSections] = {
363 0.0849750635881296, 0.5135957829646070, 0.8197816632773615, 0.9420612863757530,
364 0.9822486171863649, 0.9947076541867014, 0.9986500580725346 };
365 static constexpr double kImag[kSections] = {
366 0.2887645620083060, 0.6955314021190576, 0.8968265992273647, 0.9678214299445055,
367 0.9902622348692460, 0.9971984927047717, 0.9995996402566010 };
369 std::array<Section, 2 * kSections> sections_ {};
370 double delayedImag_ = 0.0;
RAII scope guard to disable denormalised (subnormal) floating-point numbers.
Zero-latency analytic pair from two allpass chains (a 90-degree phase-difference network).
Result process(T input) noexcept
Processes one sample into the analytic pair. RT-safe.
void reset() noexcept
Clears the allpass states. RT-safe.
T magnitude(T input) noexcept
Processes one sample and returns the analytic magnitude sqrt(real^2 + imag^2): a ripple-free envelope...
void magnitudeBlock(const T *in, T *out, int numSamples) noexcept
Analytic magnitude of a block: out[i] = |analytic(in[i])|.
90-degree phase-differencing network (analytic-signal generator).
static constexpr int getLatencySamples() noexcept
FIR group-delay latency applied to both outputs, in samples.
void reset() noexcept
Resets the delay line. Mandatory when seeking or starting playback.
void prepare(double sampleRate) noexcept
Prepares the transformer. The FIR kernel is sample-rate independent; sampleRate is accepted for API s...
Result process(T input) noexcept
Processes one sample, returning the analytic signal {real, imag}.
void processBlock(std::span< const T > input, std::span< T > outReal, std::span< T > outImag) noexcept
Processes a block of samples. Optimized for CPU cache.
double hilbertIdealImpulse(std::int64_t index) noexcept
float dotProduct(const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int count) noexcept
Computes the dot product of two arrays.
Main namespace for the DSPark framework.
T real
In-phase component (allpass-filtered input).
T imag
Quadrature component, 90 degrees behind real (like the Hilbert transform of a cosine).
T real
In-phase component (input delayed by the FIR group delay).
T imag
Quadrature component (90-deg shifted).