11#ifndef MARVIN_MATHWINDOWS_H
12#define MARVIN_MATHWINDOWS_H
15#include <marvin/library/marvin_Literals.h>
35 template <marvin::FloatType SampleType,
size_t NumPo
ints>
42 explicit PrecomputedWindow(std::array<SampleType, NumPoints>&& lut) : m_lut(std::move(lut)) {
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];
64 std::array<SampleType, NumPoints> m_lut;
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));
88 template <FloatType SampleType>
89 [[nodiscard]] SampleType
tukey(SampleType n, SampleType NumPoints, SampleType alpha) {
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) {
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));
103 }
else if (n > aNOverTwo && n <= NOverTwo) {
104 return static_cast<SampleType
>(1.0);
106 const auto reverseIndex = N - n;
107 return tukey(reverseIndex, N, alpha);
117 template <FloatType SampleType>
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);
145 template <FloatType SampleType>
146 [[nodiscard]] SampleType
cosineSum(SampleType n, SampleType N, SampleType alpha) {
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;
164 template <FloatType SampleType>
165 [[nodiscard]] SampleType
hann(SampleType n, SampleType N) {
166 return cosineSum(n, N,
static_cast<SampleType
>(0.5));
175 template <FloatType SampleType>
176 [[nodiscard]] SampleType
hamming(SampleType n, SampleType N) {
177 constexpr static auto alpha =
static_cast<SampleType
>(25.0 / 46.0);
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