DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
WindowFunctions.h
1// DSPark -- Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi -- MIT License
3
4#pragma once
5
35#include <algorithm>
36#include <cassert>
37#include <cmath>
38#include <cstddef>
39#include <numbers>
40
41namespace dspark {
42
58template <typename T>
60{
61 // Prevent instantiation (static-only utility class)
62 WindowFunctions() = delete;
63
69 static void rectangular(T* output, int size) noexcept
70 {
71 assert(output != nullptr);
72 if (size <= 0) return;
73 std::fill(output, output + size, T(1));
74 }
75
82 static void triangular(T* output, int size, bool periodic = true) noexcept
83 {
84 assert(output != nullptr);
85 if (size <= 0) return;
86 if (size == 1) { output[0] = T(1); return; }
87
88 const int N = periodic ? size : size - 1;
89 const double halfN = static_cast<double>(N) / 2.0;
90
91 for (int i = 0; i < size; ++i)
92 output[i] = static_cast<T>(1.0 - std::abs((static_cast<double>(i) - halfN) / halfN));
93 }
94
101 static void hann(T* output, int size, bool periodic = true) noexcept
102 {
103 static constexpr double kCoeffs[] = { 0.5, -0.5 };
104 cosineSum(output, size, periodic, kCoeffs);
105 }
106
113 static void hamming(T* output, int size, bool periodic = true) noexcept
114 {
115 static constexpr double kCoeffs[] = { 0.54, -0.46 };
116 cosineSum(output, size, periodic, kCoeffs);
117 }
118
125 static void blackman(T* output, int size, bool periodic = true) noexcept
126 {
127 static constexpr double kCoeffs[] = { 0.42, -0.5, 0.08 };
128 cosineSum(output, size, periodic, kCoeffs);
129 }
130
137 static void blackmanHarris(T* output, int size, bool periodic = true) noexcept
138 {
139 // Harris 1978, 4-term minimum side-lobe coefficients.
140 static constexpr double kCoeffs[] = { 0.35875, -0.48829, 0.14128, -0.01168 };
141 cosineSum(output, size, periodic, kCoeffs);
142 }
143
150 static void flatTop(T* output, int size, bool periodic = true) noexcept
151 {
152 // ISO 18431-2 / MATLAB flattopwin coefficients.
153 static constexpr double kCoeffs[] = { 0.21557895, -0.41663158, 0.277263158,
154 -0.083578947, 0.006947368 };
155 cosineSum(output, size, periodic, kCoeffs);
156 }
157
165 static void kaiser(T* output, int size, T beta, bool periodic = true) noexcept
166 {
167 assert(output != nullptr);
168 if (size <= 0) return;
169 if (size == 1) { output[0] = T(1); return; }
170
171 const int N = periodic ? size : size - 1;
172 const double b = static_cast<double>(beta);
173 const double invDenominator = 1.0 / besselI0(b);
174
175 for (int i = 0; i < size; ++i)
176 {
177 const double x = 2.0 * static_cast<double>(i) / static_cast<double>(N) - 1.0;
178 // Protected against rounding making (1 - x*x) strictly negative.
179 const double arg = b * std::sqrt(std::max(0.0, 1.0 - x * x));
180 output[i] = static_cast<T>(besselI0(arg) * invDenominator);
181 }
182 }
183
193 static void asymmetricHann(T* output, int size, int fall) noexcept
194 {
195 assert(output != nullptr);
196 if (size <= 0) return;
197 if (size == 1) { output[0] = T(1); return; }
198 const int f = std::clamp(fall, 1, size - 1);
199 const int rise = size - f;
200 constexpr double kPi = 3.14159265358979323846;
201 for (int i = 0; i < rise; ++i)
202 output[i] = static_cast<T>(0.5 - 0.5 * std::cos(kPi * static_cast<double>(i) / static_cast<double>(rise)));
203 for (int i = 0; i < f; ++i)
204 output[rise + i] = static_cast<T>(0.5 + 0.5 * std::cos(kPi * static_cast<double>(i) / static_cast<double>(f)));
205 }
206
207 // -- Utility methods -------------------------------------------------------
208
219 static void apply(T* signal, const T* window, int size) noexcept
220 {
221 assert(signal != nullptr && window != nullptr);
222
223 // No alignment assumption: std::assume_aligned<32> on a pointer that is
224 // not actually 32-byte aligned is undefined behaviour, and these buffers
225 // come from arbitrary callers (sub-views, vectors -> typically 16-byte).
226 for (int i = 0; i < size; ++i)
227 signal[i] *= window[i];
228 }
229
241 [[nodiscard]] static T coherentGain(const T* window, int size) noexcept
242 {
243 assert(window != nullptr);
244 if (size <= 0) return T(0);
245
246 double sum = 0.0;
247 for (int i = 0; i < size; ++i)
248 sum += static_cast<double>(window[i]);
249
250 return static_cast<T>(sum / static_cast<double>(size));
251 }
252
263 [[nodiscard]] static T energyGain(const T* window, int size) noexcept
264 {
265 assert(window != nullptr);
266 if (size <= 0) return T(0);
267
268 double sumSq = 0.0;
269 for (int i = 0; i < size; ++i)
270 {
271 const double w = static_cast<double>(window[i]);
272 sumSq += w * w;
273 }
274
275 return static_cast<T>(std::sqrt(sumSq / static_cast<double>(size)));
276 }
277
278private:
285 template <std::size_t NumTerms>
286 static void cosineSum(T* output, int size, bool periodic,
287 const double (&coeffs)[NumTerms]) noexcept
288 {
289 assert(output != nullptr);
290 if (size <= 0) return;
291 if (size == 1) { output[0] = T(1); return; }
292
293 const int N = periodic ? size : size - 1;
294 const double twoPiInvN = 2.0 * std::numbers::pi_v<double> / static_cast<double>(N);
295
296 for (int i = 0; i < size; ++i)
297 {
298 const double x = twoPiInvN * static_cast<double>(i);
299 double acc = coeffs[0];
300 for (std::size_t t = 1; t < NumTerms; ++t)
301 acc += coeffs[t] * std::cos(static_cast<double>(t) * x);
302 output[i] = static_cast<T>(acc);
303 }
304 }
305
313 [[nodiscard]] static double besselI0(double x) noexcept
314 {
315 double sum = 1.0;
316 double term = 1.0;
317 const double halfX = x / 2.0;
318
319 for (int k = 1; k < 50; ++k)
320 {
321 const double f = halfX / static_cast<double>(k);
322 term *= f * f;
323 sum += term;
324 if (term < sum * 1e-12) break; // converged
325 }
326
327 return sum;
328 }
329};
330
331} // namespace dspark
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 asymmetricHann(T *output, int size, int fall) noexcept
Asymmetric window for low-latency analysis: the rising half of a Hann window over the first size - fa...
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).