forked from SensibilityTestbed/indoor-localization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moving_average_hanning.r2py
52 lines (32 loc) · 1.07 KB
/
moving_average_hanning.r2py
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
"""
<Program Name>
moving_average_hanning.r2py
<Purpose>
This is a script for hanning moving average filter to filter out
the raw acceleration data. Introducing an preset maximum
sample interval to avoid device lag.
"""
# output: y[n] = (x[n] + 2*x[n-1] + x[n-2])/4
# the delay = 1 sample
class HanningFilter:
def __init__(self, current_time, threshold):
self.mag_list = []
self.window_size = 2
self.last_time = current_time
self.threshold = threshold
def hanning_filter(self, data, current_time):
# avoid device lag
# different sampling time interval will reduce the filter's accuracy
if current_time - self.last_time > self.threshold:
self.mag_list = []
length = len(self.mag_list)
if length < self.window_size:
self.mag_list.append(data)
filtered_magNoG = data
else:
filtered_magNoG = (self.mag_list[0] + 2 * self.mag_list[1] + data)/4
self.mag_list[0] = self.mag_list[1]
self.mag_list[1] = data
self.last_time = current_time
return filtered_magNoG
# -*- mode: python;-*-