Repository navigation
Expand file tree
/
Copy pathGoertzelAlgorithm.hpp
More file actions
110 lines (94 loc) · 2.97 KB
/
Copy pathGoertzelAlgorithm.hpp
File metadata and controls
110 lines (94 loc) · 2.97 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC optimize("O3", "fast-math")
#endif
#include "numerical/math/CompilerOptimizations.hpp"
#include "numerical/math/ComplexNumber.hpp"
#include <cmath>
#include <cstddef>
#include <numbers>
#include <type_traits>
namespace analysis
{
template<typename T>
class GoertzelAlgorithm
{
static_assert(std::is_floating_point_v<T>, "GoertzelAlgorithm supports floating-point types only");
public:
GoertzelAlgorithm(std::size_t k, std::size_t blockLength);
GoertzelAlgorithm(T targetHz, T sampleHz, std::size_t blockLength);
OPTIMIZE_FOR_SPEED void Push(T x);
bool Ready() const;
math::Complex<T> Result() const;
T Magnitude() const;
void Reset();
static T Coefficient(std::size_t k, std::size_t blockLength);
private:
T coeff;
T cosine;
T sine;
std::size_t blockSize;
T s1;
T s2;
std::size_t sampleCount;
};
template<typename T>
GoertzelAlgorithm<T>::GoertzelAlgorithm(std::size_t k, std::size_t blockLength)
: coeff{ T{ 2 } * std::cos(T{ 2 } * std::numbers::pi_v<T> * static_cast<T>(k) / static_cast<T>(blockLength)) }
, cosine{ std::cos(T{ 2 } * std::numbers::pi_v<T> * static_cast<T>(k) / static_cast<T>(blockLength)) }
, sine{ std::sin(T{ 2 } * std::numbers::pi_v<T> * static_cast<T>(k) / static_cast<T>(blockLength)) }
, blockSize{ blockLength }
, s1{ T{ 0 } }
, s2{ T{ 0 } }
, sampleCount{ 0 }
{}
template<typename T>
GoertzelAlgorithm<T>::GoertzelAlgorithm(T targetHz, T sampleHz, std::size_t blockLength)
: GoertzelAlgorithm(
static_cast<std::size_t>(targetHz / sampleHz * static_cast<T>(blockLength) + T{ 0.5 }),
blockLength)
{}
template<typename T>
OPTIMIZE_FOR_SPEED void GoertzelAlgorithm<T>::Push(T x)
{
T s0{ x + coeff * s1 - s2 };
s2 = s1;
s1 = s0;
++sampleCount;
}
template<typename T>
bool GoertzelAlgorithm<T>::Ready() const
{
return sampleCount >= blockSize;
}
template<typename T>
math::Complex<T> GoertzelAlgorithm<T>::Result() const
{
T real{ s1 - s2 * cosine };
T imag{ s2 * sine };
return math::Complex<T>{ real, imag };
}
template<typename T>
T GoertzelAlgorithm<T>::Magnitude() const
{
return std::sqrt(s1 * s1 + s2 * s2 - coeff * s1 * s2);
}
template<typename T>
void GoertzelAlgorithm<T>::Reset()
{
s1 = T{ 0 };
s2 = T{ 0 };
sampleCount = 0;
}
template<typename T>
T GoertzelAlgorithm<T>::Coefficient(std::size_t k, std::size_t blockLength)
{
return T{ 2 } * std::cos(T{ 2 } * std::numbers::pi_v<T> * static_cast<T>(k) / static_cast<T>(blockLength));
}
}
#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD
namespace analysis
{
extern template class GoertzelAlgorithm<float>;
}
#endif