openMSX
hammingWindow.hh
Go to the documentation of this file.
1#ifndef HAMMINGWINDOW_HH
2#define HAMMINGWINDOW_HH
3
4#include "xrange.hh"
5
6#include <cassert>
7#include <cmath>
8#include <map>
9#include <numbers>
10#include <span>
11#include <vector>
12
13// Returns the coefficients of the 'hamming' window of length 'n'.
14// https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
15inline std::span<const float> hammingWindow(unsigned n)
16{
17 static std::map<unsigned, std::vector<float>> cache;
18 auto [it, inserted] = cache.try_emplace(n);
19 auto& window = it->second;
20
21 if (inserted) {
22 window.resize(n);
23 for (auto i : xrange(n)) {
24 window[i] = 0.53836 - 0.46164 * cos(2 * std::numbers::pi * i / (n - 1));
25 }
26 return window;
27 }
28
29 assert(window.size() == n);
30 return window;
31}
32
33#endif
std::span< const float > hammingWindow(unsigned n)
constexpr auto xrange(T e)
Definition xrange.hh:132