-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowPassFilter.pde
66 lines (54 loc) · 1.28 KB
/
LowPassFilter.pde
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
class LowPassFilter
{
float mCutoff;
float mLastOutput;
float mLastInput;
float mCurrentOutput;
float mLastMillis;
LowPassFilter(float cutoff, float v) {
mCutoff = cutoff;
mLastOutput = v;
mLastInput = v;
mCurrentOutput = v;
mLastMillis = millis();
}
LowPassFilter(float cutoff) {
this(cutoff,0.0f);
}
LowPassFilter() {
this(1);
}
void setCutoff(float cutoff)
{
mCutoff = cutoff;
}
float write(float v)
{
float millis = millis();
float timeSinceLastFrame = millis-mLastMillis;
if (timeSinceLastFrame<30) {
return mCurrentOutput;
}
mLastOutput = mCurrentOutput;
float sr = 1 / timeSinceLastFrame;
float coef = mCutoff * TWO_PI / sr;
if (coef > 1)
coef = 1;
else if (coef < 0)
coef = 0;
float feedback = 1 - coef;
mCurrentOutput = (coef * v) + (feedback * mLastOutput);
mLastInput = v;
mLastMillis = millis;
return mCurrentOutput;
}
float read()
{
return write(mLastInput);
}
float set(float v) {
mCurrentOutput = v;
mLastInput = v;
return v;
}
};