forked from SensibilityTestbed/indoor-localization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moving_average.r2py
56 lines (40 loc) · 1.39 KB
/
moving_average.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
53
54
55
56
"""
<Program Name>
moving_average.r2py
<Purpose>
This is a script for moving average filter to filter out
the raw acceleration data. Introducing an preset maximum
sample interval to avoid device lag.
"""
# output: y = sum(x[i])/length, i in range (0, window_size)
# minimum window size is 4
# the delay = window_size/2
class MovingAverageFilter:
def __init__(self, moving_average_sample, current_time, threshold):
self.mag_list = []
self.window_size = moving_average_sample
self.last_time = current_time
self.threshold = threshold
def moving_average_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)
filtered_magNoG = 0.0
if length < self.window_size:
self.mag_list.append(data)
length += 1
for i in range(0,length):
filtered_magNoG = filtered_magNoG + self.mag_list[i]
filtered_magNoG = filtered_magNoG/length
elif length == self.window_size:
for i in range(0, length-1):
self.mag_list[i] = self.mag_list[i+1]
filtered_magNoG = filtered_magNoG + self.mag_list[i]
self.mag_list[length-1] = data
filtered_magNoG = filtered_magNoG + data
filtered_magNoG = filtered_magNoG/length
self.last_time = current_time
return filtered_magNoG
# -*- mode: python;-*-