OXYGENHUB
◆ main openmsx / src / utils / hammingWindow.hh
751 B · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef HAMMINGWINDOW_HH
2
#define HAMMINGWINDOW_HH
3
4
#include "MemBuffer.hh"
5
#include "xrange.hh"
6
7
#include <cassert>
8
#include <cmath>
9
#include <map>
10
#include <numbers>
11
#include <span>
12
13
// Returns the coefficients of the 'hamming' window of length 'n'.
14
//     https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
15
inline std::span<const float> hammingWindow(unsigned n)
16
{
17
	static std::map<unsigned, openmsx::MemBuffer<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] = float(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