-
Notifications
You must be signed in to change notification settings - Fork 2
/
LowPassFilter.cs
44 lines (38 loc) · 950 Bytes
/
LowPassFilter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PimaxCrystalAdvanced
{
public class LowPassFilter
{
private readonly float[] _samples;
private int _index;
public LowPassFilter(int count)
{
_samples = new float[count - 1];
for (var i = 0; i < count - 1; i++)
{
_samples[i] = 0.0f;
}
}
private float Sum()
{
float weight = 0;
foreach (var sample in _samples)
{
weight += sample;
}
return weight;
}
public float FilterValue(float newValue)
{
_index++;
if (_samples.Length == _index)
_index = 0;
_samples[_index] = newValue;
return Sum() / _samples.Length;
}
}
}