71 assert(output !=
nullptr);
72 if (size <= 0)
return;
73 std::fill(output, output + size, T(1));
82 static void triangular(T* output,
int size,
bool periodic =
true) noexcept
84 assert(output !=
nullptr);
85 if (size <= 0)
return;
86 if (size == 1) { output[0] = T(1);
return; }
88 const int N = periodic ? size : size - 1;
89 const double halfN =
static_cast<double>(N) / 2.0;
91 for (
int i = 0; i < size; ++i)
92 output[i] =
static_cast<T
>(1.0 - std::abs((
static_cast<double>(i) - halfN) / halfN));
101 static void hann(T* output,
int size,
bool periodic =
true) noexcept
103 static constexpr double kCoeffs[] = { 0.5, -0.5 };
104 cosineSum(output, size, periodic, kCoeffs);
113 static void hamming(T* output,
int size,
bool periodic =
true) noexcept
115 static constexpr double kCoeffs[] = { 0.54, -0.46 };
116 cosineSum(output, size, periodic, kCoeffs);
125 static void blackman(T* output,
int size,
bool periodic =
true) noexcept
127 static constexpr double kCoeffs[] = { 0.42, -0.5, 0.08 };
128 cosineSum(output, size, periodic, kCoeffs);
140 static constexpr double kCoeffs[] = { 0.35875, -0.48829, 0.14128, -0.01168 };
141 cosineSum(output, size, periodic, kCoeffs);
150 static void flatTop(T* output,
int size,
bool periodic =
true) noexcept
153 static constexpr double kCoeffs[] = { 0.21557895, -0.41663158, 0.277263158,
154 -0.083578947, 0.006947368 };
155 cosineSum(output, size, periodic, kCoeffs);
165 static void kaiser(T* output,
int size, T beta,
bool periodic =
true) noexcept
167 assert(output !=
nullptr);
168 if (size <= 0)
return;
169 if (size == 1) { output[0] = T(1);
return; }
171 const int N = periodic ? size : size - 1;
172 const double b =
static_cast<double>(beta);
173 const double invDenominator = 1.0 / besselI0(b);
175 for (
int i = 0; i < size; ++i)
177 const double x = 2.0 *
static_cast<double>(i) /
static_cast<double>(N) - 1.0;
179 const double arg = b * std::sqrt(std::max(0.0, 1.0 - x * x));
180 output[i] =
static_cast<T
>(besselI0(arg) * invDenominator);
196 static void apply(T* signal,
const T* window,
int size)
noexcept
198 assert(signal !=
nullptr && window !=
nullptr);
203 for (
int i = 0; i < size; ++i)
204 signal[i] *= window[i];
218 [[nodiscard]]
static T
coherentGain(
const T* window,
int size)
noexcept
220 assert(window !=
nullptr);
221 if (size <= 0)
return T(0);
224 for (
int i = 0; i < size; ++i)
225 sum +=
static_cast<double>(window[i]);
227 return static_cast<T
>(sum /
static_cast<double>(size));
240 [[nodiscard]]
static T
energyGain(
const T* window,
int size)
noexcept
242 assert(window !=
nullptr);
243 if (size <= 0)
return T(0);
246 for (
int i = 0; i < size; ++i)
248 const double w =
static_cast<double>(window[i]);
252 return static_cast<T
>(std::sqrt(sumSq /
static_cast<double>(size)));
262 template <std::
size_t NumTerms>
263 static void cosineSum(T* output,
int size,
bool periodic,
264 const double (&coeffs)[NumTerms])
noexcept
266 assert(output !=
nullptr);
267 if (size <= 0)
return;
268 if (size == 1) { output[0] = T(1);
return; }
270 const int N = periodic ? size : size - 1;
271 const double twoPiInvN = 2.0 * std::numbers::pi_v<double> /
static_cast<double>(N);
273 for (
int i = 0; i < size; ++i)
275 const double x = twoPiInvN *
static_cast<double>(i);
276 double acc = coeffs[0];
277 for (std::size_t t = 1; t < NumTerms; ++t)
278 acc += coeffs[t] * std::cos(
static_cast<double>(t) * x);
279 output[i] =
static_cast<T
>(acc);
290 [[nodiscard]]
static double besselI0(
double x)
noexcept
294 const double halfX = x / 2.0;
296 for (
int k = 1; k < 50; ++k)
298 const double f = halfX /
static_cast<double>(k);
301 if (term < sum * 1e-12)
break;
Main namespace for the DSPark framework.
Static utility generating window functions for DSP analysis/synthesis.
static void kaiser(T *output, int size, T beta, bool periodic=true) noexcept
Kaiser window with configurable shape parameter beta.
static void hann(T *output, int size, bool periodic=true) noexcept
Hann (raised cosine) window.
static void triangular(T *output, int size, bool periodic=true) noexcept
Triangular (Bartlett) window.
static void blackman(T *output, int size, bool periodic=true) noexcept
Blackman window.
static void blackmanHarris(T *output, int size, bool periodic=true) noexcept
Blackman-Harris window (4-term, -92 dB side lobes).
static void hamming(T *output, int size, bool periodic=true) noexcept
Hamming window.
static T coherentGain(const T *window, int size) noexcept
Computes the coherent gain of a window (mean of its samples).
static T energyGain(const T *window, int size) noexcept
Computes the energy gain (RMS) of a window.
static void apply(T *signal, const T *window, int size) noexcept
Applies a window to a signal buffer in-place.
static void flatTop(T *output, int size, bool periodic=true) noexcept
Flat-top window (amplitude-accurate: scallop loss < 0.01 dB).
static void rectangular(T *output, int size) noexcept
Rectangular window (no windowing – all ones).