-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filter.h
60 lines (47 loc) · 1.21 KB
/
Filter.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
60
#pragma once
class Filter
{
float factor = 0;
float sampleRate;
float state[2] = {0, 0};
public:
Filter(float sampleRate) : sampleRate(sampleRate)
{
}
void updateLowpass(float cutFreqHz)
{
factor = 1.0f / (1.0f / (2.0f * 3.14159265f * 1.0f / sampleRate * cutFreqHz) + 1.0f);
}
void updateHighpass(float cutFreqHz)
{
factor = 1.0f / (2.0f * 3.14159265f * 1.0f / sampleRate * cutFreqHz + 1.0f);
}
float processHighpass(float input)
{
state[0] = factor * (input + state[0] - state[1]);
state[1] = input;
return state[0];
}
float processLowpass(float input)
{
state[0] = factor * input + (1.0f - factor) * state[1];
state[1] = state[0];
return state[0];
}
};
class LowpassFilter : Filter
{
public:
LowpassFilter(float sampleRate) : Filter(sampleRate) {}
inline float process(float input) { return processLowpass(input); }
inline void update(float hz) { updateLowpass(hz); }
};
class DcFilter : Filter
{
public:
DcFilter(float sampleRate) : Filter(sampleRate)
{
updateHighpass(10);
}
inline float process(float input) { return processHighpass(input); }
};