-
Notifications
You must be signed in to change notification settings - Fork 14
/
Normalizer.h
executable file
·59 lines (45 loc) · 878 Bytes
/
Normalizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include <cmath>
#include <complex>
#include <numeric>
#include <span>
template<class T>
class Normalizer
{
public:
Normalizer()
{
}
void calibrate(const std::span<const std::complex<T>> data)
{
target = rms(data);
}
void normalize(const std::span<std::complex<T>> data) const
{
const T a = target;
const T b = rms(data);
if (b == 0)
{
return;
}
const T c = std::sqrt(a / b);
for (size_t i = 0; i < data.size(); ++i)
{
data[i].real(data[i].real() * c);
}
}
private:
T target;
static T rms(const std::span<const std::complex<T>> data)
{
// without 1/N and sqrt
return std::accumulate(
data.begin(),
data.end(),
T(0),
[](const T result, const std::complex<T>& value)
{
return result + value.real() * value.real();
});
}
};