MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Taper.cpp
Go to the documentation of this file.
1#include "Taper.hpp"
2
3#include <algorithm>
4
6
7// ============================================================================
8// Coefficient generation
9// ============================================================================
10
11std::vector<double> hann(size_t n)
12{
13 if (n == 0)
14 return {};
15 if (n == 1)
16 return { 1.0 };
17 std::vector<double> w(n);
18 const double scale = std::numbers::pi * 2.0 / static_cast<double>(n - 1);
19 for (size_t i = 0; i < n; ++i)
20 w[i] = 0.5 * (1.0 - std::cos(scale * static_cast<double>(i)));
21 return w;
22}
23
24std::vector<double> hamming(size_t n)
25{
26 if (n == 0)
27 return {};
28 if (n == 1)
29 return { 1.0 };
30 std::vector<double> w(n);
31 const double scale = std::numbers::pi * 2.0 / static_cast<double>(n - 1);
32 for (size_t i = 0; i < n; ++i)
33 w[i] = 0.54 - 0.46 * std::cos(scale * static_cast<double>(i));
34 return w;
35}
36
37std::vector<double> blackman(size_t n)
38{
39 if (n == 0)
40 return {};
41 if (n == 1)
42 return { 1.0 };
43 std::vector<double> w(n);
44 const double scale = std::numbers::pi * 2.0 / static_cast<double>(n - 1);
45 for (size_t i = 0; i < n; ++i) {
46 const double x = scale * static_cast<double>(i);
47 w[i] = 0.42 - 0.5 * std::cos(x) + 0.08 * std::cos(2.0 * x);
48 }
49 return w;
50}
51
52std::vector<double> rectangular(size_t n, size_t support, size_t offset)
53{
54 std::vector<double> w(n, 0.0);
55 const size_t start = std::min(offset, n);
56 const size_t end = std::min(start + support, n);
57 for (size_t i = start; i < end; ++i)
58 w[i] = 1.0;
59 return w;
60}
61
62std::vector<double> trapezoid(size_t n, size_t fade_len)
63{
64 if (n == 0)
65 return {};
66 if (n == 1)
67 return { 1.0 };
68 std::vector<double> w(n, 1.0);
69 const size_t ramp = std::min(fade_len, n / 2);
70 for (size_t i = 0; i < ramp; ++i) {
71 const double r = static_cast<double>(i) / static_cast<double>(ramp);
72 w[i] = r;
73 w[n - 1 - i] = r;
74 }
75 return w;
76}
77
78// ============================================================================
79// In-place application
80// ============================================================================
81
82void apply_taper(std::span<double> data, std::span<const double> taper) noexcept
83{
84 if (data.empty() || taper.empty())
85 return;
86 const size_t n = data.size();
87 const size_t tn = taper.size();
88 if (n == tn) {
89 for (size_t i = 0; i < n; ++i)
90 data[i] *= taper[i];
91 } else {
92 for (size_t i = 0; i < n; ++i)
93 data[i] *= taper[i % tn];
94 }
95}
96
97void apply_hann(std::span<double> data) noexcept
98{
99 const size_t n = data.size();
100 if (n <= 1)
101 return;
102 const double scale = std::numbers::pi * 2.0 / static_cast<double>(n - 1);
103 for (size_t i = 0; i < n; ++i)
104 data[i] *= 0.5 * (1.0 - std::cos(scale * static_cast<double>(i)));
105}
106
107void apply_hamming(std::span<double> data) noexcept
108{
109 const size_t n = data.size();
110 if (n <= 1)
111 return;
112 const double scale = std::numbers::pi * 2.0 / static_cast<double>(n - 1);
113 for (size_t i = 0; i < n; ++i)
114 data[i] *= 0.54 - 0.46 * std::cos(scale * static_cast<double>(i));
115}
116
117void apply_blackman(std::span<double> data) noexcept
118{
119 const size_t n = data.size();
120 if (n <= 1)
121 return;
122 const double scale = std::numbers::pi * 2.0 / static_cast<double>(n - 1);
123 for (size_t i = 0; i < n; ++i) {
124 const double x = scale * static_cast<double>(i);
125 data[i] *= 0.42 - 0.5 * std::cos(x) + 0.08 * std::cos(2.0 * x);
126 }
127}
128
129void apply_rectangular(std::span<double> data, size_t support, size_t offset) noexcept
130{
131 const size_t n = data.size();
132 if (n == 0 || support == 0) {
133 std::ranges::fill(data, 0.0);
134 return;
135 }
136
137 const size_t start = std::min(offset, n);
138 const size_t end = std::min(start + support, n);
139
140 for (size_t i = 0; i < start; ++i)
141 data[i] = 0.0;
142 for (size_t i = end; i < n; ++i)
143 data[i] = 0.0;
144}
145
146void apply_trapezoid(std::span<double> data, size_t fade_len) noexcept
147{
148 const size_t n = data.size();
149 if (n == 0)
150 return;
151 const size_t ramp = std::min(fade_len, n / 2);
152 for (size_t i = 0; i < ramp; ++i) {
153 const double r = static_cast<double>(i) / static_cast<double>(ramp);
154 data[i] *= r;
155 data[n - 1 - i] *= r;
156 }
157}
158
159} // namespace MayaFlux::Kinesis::Discrete
Discrete taper (window) coefficient generation and in-place application for MayaFlux::Kinesis.
float scale
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