marvin 0.0.1
Pure C++ audio helper library
 
Loading...
Searching...
No Matches
marvin_Windows.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef MARVIN_MATHWINDOWS_H
12#define MARVIN_MATHWINDOWS_H
15#include <marvin/library/marvin_Literals.h>
16#include <numbers>
17#include <cmath>
19
31
35 template <marvin::FloatType SampleType, size_t NumPoints>
36 class PrecomputedWindow final {
37 public:
42 explicit PrecomputedWindow(std::array<SampleType, NumPoints>&& lut) : m_lut(std::move(lut)) {
43 }
44
50 [[nodiscard]] SampleType operator()(SampleType proportion) const {
51 using namespace marvin::literals;
52 const auto rescaled = proportion * (NumPoints - 1_sz);
53 const auto truncated = std::trunc(rescaled);
54 const auto delta = rescaled - truncated;
55 const auto index0{ static_cast<size_t>(truncated) };
56 const auto index1 = index0 + 1;
57 const auto pointA = m_lut[index0];
58 const auto pointB = m_lut[index1];
59 const auto interpolated = marvin::math::lerp(pointA, pointB, delta);
60 return interpolated;
61 }
62
63 private:
64 std::array<SampleType, NumPoints> m_lut;
65 };
66
73 template <FloatType SampleType>
74 [[nodiscard]] SampleType sine(SampleType n, SampleType N) {
75 static constexpr auto pi = std::numbers::pi_v<SampleType>;
76 const auto window = std::sin((pi * n) / (N - 1));
77 return window;
78 }
79
88 template <FloatType SampleType>
89 [[nodiscard]] SampleType tukey(SampleType n, SampleType NumPoints, SampleType alpha) {
90 // 0.5 * (1 - cos(2pin/alphaN)), 0 <= n <= aN / 2
91 // 1, aN/2 <= n <= N / 2
92 // w[N - n] = w[n], 0 <= n <= N / 2
93 const auto N{ NumPoints - 1 };
94 const auto alphaN = alpha * N;
95 const auto NOverTwo = N / static_cast<SampleType>(2.0);
96 const auto aNOverTwo = alphaN / static_cast<SampleType>(2.0);
97 if (n >= 0 && n <= aNOverTwo) {
98 // 0.5 * (1 - cos(2pin / alphaN))
99 constexpr static auto twoPi = static_cast<SampleType>(2.0) * std::numbers::pi_v<SampleType>;
100 const auto cosine = std::cos((twoPi * n) / alphaN);
101 const auto res = static_cast<SampleType>(0.5) * (static_cast<SampleType>(1.0 - cosine));
102 return res;
103 } else if (n > aNOverTwo && n <= NOverTwo) {
104 return static_cast<SampleType>(1.0);
105 } else {
106 const auto reverseIndex = N - n;
107 return tukey(reverseIndex, N, alpha);
108 }
109 }
110
117 template <FloatType SampleType>
118 [[nodiscard]] SampleType blackmanHarris(SampleType n, SampleType N) {
119 constexpr static auto a0{ 0.35875 };
120 constexpr static auto a1{ 0.48829 };
121 constexpr static auto a2{ 0.14128 };
122 constexpr static auto a3{ 0.01168 };
123 constexpr static auto pi{ std::numbers::pi_v<double> };
124 constexpr static auto twoPi{ pi * 2.0 };
125 constexpr static auto fourPi{ pi * 4.0 };
126 constexpr static auto sixPi{ pi * 6.0 };
127 const auto position = static_cast<double>(n) / static_cast<double>(N - 1);
128 const auto a1Term = a1 * std::cos(position * twoPi);
129 const auto a2Term = a2 * std::cos(position * fourPi);
130 const auto a3Term = a3 * std::cos(position * sixPi);
131 const auto result = a0 - a1Term + a2Term - a3Term;
132 return static_cast<SampleType>(result);
133 }
134
145 template <FloatType SampleType>
146 [[nodiscard]] SampleType cosineSum(SampleType n, SampleType N, SampleType alpha) {
147 // w[n] = a0 - (1 - a0) * cos(2pi * n/N)
148 const auto a0 = alpha;
149 const auto a1 = static_cast<SampleType>(1.0) - alpha;
150 constexpr static auto twoPi = std::numbers::pi_v<SampleType> * static_cast<SampleType>(2.0);
151 const auto ratio = n / (N - 1);
152 const auto cosine = std::cos(twoPi * ratio);
153 const auto a1Scaled = a1 * cosine;
154 const auto res = a0 - a1Scaled;
155 return res;
156 }
157
164 template <FloatType SampleType>
165 [[nodiscard]] SampleType hann(SampleType n, SampleType N) {
166 return cosineSum(n, N, static_cast<SampleType>(0.5));
167 }
168
175 template <FloatType SampleType>
176 [[nodiscard]] SampleType hamming(SampleType n, SampleType N) {
177 constexpr static auto alpha = static_cast<SampleType>(25.0 / 46.0);
178 return cosineSum(n, N, alpha);
179 }
180} // namespace marvin::math::windows
181#endif
SampleType operator()(SampleType proportion) const
Definition marvin_Windows.h:50
PrecomputedWindow(std::array< SampleType, NumPoints > &&lut)
Definition marvin_Windows.h:42
Various windowing functions. An interactive graph with more intuition than the textual documentation ...
Definition marvin_Windows.h:18
SampleType cosineSum(SampleType n, SampleType N, SampleType alpha)
Definition marvin_Windows.h:146
SampleType blackmanHarris(SampleType n, SampleType N)
Definition marvin_Windows.h:118
SampleType sine(SampleType n, SampleType N)
Definition marvin_Windows.h:74
SampleType hamming(SampleType n, SampleType N)
Definition marvin_Windows.h:176
SampleType hann(SampleType n, SampleType N)
Definition marvin_Windows.h:165
SampleType tukey(SampleType n, SampleType NumPoints, SampleType alpha)
Definition marvin_Windows.h:89
WindowType
Definition marvin_Windows.h:23
@ Hann
Definition marvin_Windows.h:28
@ Sine
Definition marvin_Windows.h:24
@ Hamming
Definition marvin_Windows.h:29
@ BlackmanHarris
Definition marvin_Windows.h:26
@ Tukey
Definition marvin_Windows.h:25
@ CosineSum
Definition marvin_Windows.h:27
T lerp(T start, T end, T ratio) noexcept
Definition marvin_Math.h:32