-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter.py
36 lines (30 loc) · 1.19 KB
/
filter.py
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
class Filter:
"""
Simple parametric mean value filter
The adjustable smallest and largest values are discarded and the mean value is calculated from the remaining values.
"""
def __init__(self, cut=1, avg=2):
"""
Init filter with setup
:param cut: number of values to discard
:param avg: number of values for averaging
"""
self.buf = [] # sample buffer
self.cut = cut # number of lowest and highest values to discard
self.avg = avg # number of values for averaging
def __call__(self, input):
"""
Filter
:param input: raw value
:return: filtered val
"""
if isinstance(input, (int, float)):
self.buf.append(input)
self.buf = self.buf[-(2 * self.cut + self.avg):] # limit to filterlength
if len(self.buf) > (2 * self.cut): # with sufficient values
flt = sorted(self.buf)[self.cut:-self.cut] # cut min and max
return round(sum(flt) / len(flt)) # average values
elif len(self.buf) > 0:
return round(sum(self.buf) / len(self.buf)) # only average for to short buffer
else:
return 0