MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Taper.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file Taper.hpp
5 * @brief Discrete taper (window) coefficient generation and in-place application
6 * for MayaFlux::Kinesis
7 *
8 * Pure numerical functions producing taper coefficient vectors and applying
9 * them in-place to contiguous double-precision spans.
10 * No MayaFlux type dependencies. Domain-agnostic.
11 *
12 * Two usage patterns are supported:
13 *
14 * 1. Generate then apply separately:
15 * auto t = Kinesis::Discrete::hann(n);
16 * Kinesis::Discrete::apply_taper(signal, t);
17 *
18 * 2. Apply directly without materialising coefficients:
19 * Kinesis::Discrete::apply_hann(signal);
20 *
21 * All generation functions return a vector of length @p n.
22 * The single-sample edge case always returns { 1.0 }.
23 *
24 * SIMD notes:
25 * apply_taper and apply_rectangular are auto-vectorisable under
26 * -O2 -march=native. Generation functions invoke std::cos and are scalar.
27 */
28
30
31// ============================================================================
32// Coefficient generation
33// ============================================================================
34
35/**
36 * @brief Hann (raised cosine) taper coefficients
37 * @param n Length in samples
38 * @return Coefficient vector of length n
39 */
40[[nodiscard]] MAYAFLUX_API std::vector<double> hann(size_t n);
41
42/**
43 * @brief Hamming taper coefficients
44 * @param n Length in samples
45 * @return Coefficient vector of length n
46 */
47[[nodiscard]] MAYAFLUX_API std::vector<double> hamming(size_t n);
48
49/**
50 * @brief Blackman taper coefficients
51 * @param n Length in samples
52 * @return Coefficient vector of length n
53 */
54[[nodiscard]] MAYAFLUX_API std::vector<double> blackman(size_t n);
55
56/**
57 * @brief Rectangular (boxcar) taper coefficients
58 *
59 * Unity across a contiguous support region, zero elsewhere. The default
60 * arguments give unity across the whole length, which is the identity
61 * taper; a narrower @p support gates a region and suppresses the rest,
62 * which is the form used for frame extraction, impulse response
63 * truncation, and segment isolation ahead of a transform.
64 *
65 * A @p support of 0 yields an all-zero vector. An @p offset at or beyond
66 * @p n leaves no unity region.
67 *
68 * @param n Length in samples
69 * @param support Width of the unity region; defaults to the full length
70 * @param offset Index where the unity region begins
71 * @return Coefficient vector of length n
72 */
73[[nodiscard]] MAYAFLUX_API std::vector<double> rectangular(
74 size_t n,
75 size_t support = std::numeric_limits<size_t>::max(),
76 size_t offset = 0);
77
78/**
79 * @brief Trapezoid taper coefficients with configurable flat region
80 *
81 * Linear ramp-in over @p fade_len samples, unity plateau, linear ramp-out
82 * over @p fade_len samples. If 2 * fade_len >= n the ramps are clamped so
83 * they meet at the centre with no plateau.
84 *
85 * @param n Length in samples
86 * @param fade_len Ramp length in samples at each end
87 * @return Coefficient vector of length n
88 */
89[[nodiscard]] MAYAFLUX_API std::vector<double> trapezoid(size_t n, size_t fade_len);
90
91// ============================================================================
92// In-place application
93// ============================================================================
94
95/**
96 * @brief Multiply @p data element-wise by a precomputed taper
97 *
98 * Applied cyclically if @p taper is shorter than @p data; truncated if longer.
99 * The common case (equal sizes) is a straight element-wise multiply.
100 *
101 * @param data Target span (modified in place)
102 * @param taper Coefficient span
103 */
104MAYAFLUX_API void apply_taper(std::span<double> data, std::span<const double> taper) noexcept;
105
106/**
107 * @brief Apply a Hann taper in-place without materialising coefficients
108 * @param data Target span (modified in place)
109 */
110MAYAFLUX_API void apply_hann(std::span<double> data) noexcept;
111
112/**
113 * @brief Apply a Hamming taper in-place without materialising coefficients
114 * @param data Target span (modified in place)
115 */
116MAYAFLUX_API void apply_hamming(std::span<double> data) noexcept;
117
118/**
119 * @brief Apply a Blackman taper in-place without materialising coefficients
120 * @param data Target span (modified in place)
121 */
122MAYAFLUX_API void apply_blackman(std::span<double> data) noexcept;
123
124/**
125 * @brief Apply a rectangular taper in-place without materialising coefficients
126 *
127 * Zeroes everything outside the support region and leaves the region
128 * itself untouched, since its coefficients are unity. The default
129 * arguments gate nothing and leave @p data unchanged.
130 *
131 * @param data Target span (modified in place)
132 * @param support Width of the unity region; defaults to the full length
133 * @param offset Index where the unity region begins
134 */
135MAYAFLUX_API void apply_rectangular(
136 std::span<double> data,
137 size_t support = std::numeric_limits<size_t>::max(),
138 size_t offset = 0) noexcept;
139
140/**
141 * @brief Apply a trapezoid taper in-place without materialising coefficients
142 * @param data Target span (modified in place)
143 * @param fade_len Ramp length in samples at each end
144 */
145MAYAFLUX_API void apply_trapezoid(std::span<double> data, size_t fade_len) noexcept;
146
147} // namespace MayaFlux::Kinesis::Discrete
float offset
void apply_trapezoid(std::span< double > data, size_t fade_len) noexcept
Apply a trapezoid taper in-place without materialising coefficients.
Definition Taper.cpp:146
std::vector< double > hann(size_t n)
Hann (raised cosine) taper coefficients.
Definition Taper.cpp:11
std::vector< double > blackman(size_t n)
Blackman taper coefficients.
Definition Taper.cpp:37
void apply_blackman(std::span< double > data) noexcept
Apply a Blackman taper in-place without materialising coefficients.
Definition Taper.cpp:117
void apply_taper(std::span< double > data, std::span< const double > taper) noexcept
Multiply data element-wise by a precomputed taper.
Definition Taper.cpp:82
std::vector< double > rectangular(size_t n, size_t support, size_t offset)
Rectangular (boxcar) taper coefficients.
Definition Taper.cpp:52
std::vector< double > trapezoid(size_t n, size_t fade_len)
Trapezoid taper coefficients with configurable flat region.
Definition Taper.cpp:62
void apply_hann(std::span< double > data) noexcept
Apply a Hann taper in-place without materialising coefficients.
Definition Taper.cpp:97
std::vector< double > hamming(size_t n)
Hamming taper coefficients.
Definition Taper.cpp:24
void apply_hamming(std::span< double > data) noexcept
Apply a Hamming taper in-place without materialising coefficients.
Definition Taper.cpp:107
void apply_rectangular(std::span< double > data, size_t support, size_t offset) noexcept
Apply a rectangular taper in-place without materialising coefficients.
Definition Taper.cpp:129