-
Notifications
You must be signed in to change notification settings - Fork 2
/
OneEuroFilter.cs
70 lines (59 loc) · 1.59 KB
/
OneEuroFilter.cs
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
/// courtesy of https://github.com/jaantollander/OneEuroFilter
/// and https://jaantollander.com/post/noise-filtering-using-one-euro-filter/
public class OneEuroFilter
{
public OneEuroFilter(double minCutoff, double beta)
{
firstTime = true;
this.minCutoff = minCutoff;
this.beta = beta;
xFilt = new LowpassFilter();
dxFilt = new LowpassFilter();
dcutoff = 1;
}
protected bool firstTime;
protected double minCutoff;
protected double beta;
protected LowpassFilter xFilt;
protected LowpassFilter dxFilt;
protected double dcutoff;
public double Filter(double x, double rate)
{
double dx = firstTime ? 0 : (x - xFilt.Last()) * rate;
if (firstTime)
{
firstTime = false;
}
var edx = dxFilt.Filter(dx, Alpha(rate, dcutoff));
var cutoff = minCutoff + beta * Math.Abs(edx);
return xFilt.Filter(x, Alpha(rate, cutoff));
}
protected double Alpha(double rate, double cutoff)
{
var tau = 1.0 / (2 * Math.PI * cutoff);
var te = 1.0 / rate;
return 1.0 / (1.0 + tau / te);
}
}
public class LowpassFilter
{
protected bool firstTime = true;
protected double hatXPrev;
public double Last()
{
return hatXPrev;
}
public double Filter(double x, double alpha)
{
double hatX = 0;
if (firstTime)
{
firstTime = false;
hatX = x;
}
else
hatX = alpha * x + (1 - alpha) * hatXPrev;
hatXPrev = hatX;
return hatX;
}
}