-
Notifications
You must be signed in to change notification settings - Fork 13
/
coeffs.hh
45 lines (39 loc) · 854 Bytes
/
coeffs.hh
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
/*
Coefficient array helper
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
namespace DSP {
template <int TAPS, typename TYPE, bool NORMALIZE = false>
class Coeffs
{
TYPE w[TAPS];
public:
template <typename FUNC>
Coeffs(FUNC *func)
{
for (int n = 0; n < TAPS; ++n)
w[n] = (*func)(n, TAPS);
if (NORMALIZE)
normalize();
}
template <typename FUNC0, typename FUNC1>
Coeffs(FUNC0 *func0, FUNC1 *func1)
{
for (int n = 0; n < TAPS; ++n)
w[n] = (*func0)(n, TAPS) * (*func1)(n, TAPS);
if (NORMALIZE)
normalize();
}
void normalize(TYPE divisor = 1)
{
TYPE sum(0);
for (int n = 0; n < TAPS; ++n)
sum += w[n];
for (int n = 0; n < TAPS; ++n)
w[n] /= divisor * abs(sum);
}
inline TYPE operator () (int n) { return n >= 0 && n < TAPS ? w[n] : 0; }
inline operator const TYPE * () const { return w; }
};
}