-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEmaFilter.cs
More file actions
35 lines (30 loc) · 745 Bytes
/
EmaFilter.cs
File metadata and controls
35 lines (30 loc) · 745 Bytes
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SmoothMouse
{
// Exponential moving average filter
// Where the amount of smoothing (alpha) is adjustable online
public class EmaFilter
{
private double _lastValue;
public EmaFilter()
{
_lastValue = double.NaN;
}
public double Filter(double value, double alpha)
{
if (double.IsNaN(_lastValue))
{
_lastValue = value;
}
else
{
_lastValue = alpha * value + (1 - alpha) * _lastValue;
}
return _lastValue;
}
}
}