-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
36 lines (29 loc) · 1.06 KB
/
filter.py
File metadata and controls
36 lines (29 loc) · 1.06 KB
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
import numpy as np
from collections import deque
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / N
def moving_average(data):
# calculates the moving average of the data
# data : data to be averaged
# return : the filtered average of the data
return sum(data)/len(data)
class Filter():
def __init__(self, max_length):
self.queue = deque(maxlen=max_length)
self.max_length = max_length
def moving_average(self, data):
# calculates the moving average of the filter as well as keeps track
# of the time series data
# data : new data to be added to the queue to be filtered
# return : the filtered average for the filter, -1 if error
self.queue.appendleft(data)
queue_length = len(self.queue)
try:
# find the moving average
average = sum(self.queue) / queue_length
except:
average = -1
if queue_length >= self.max_length:
self.queue.pop()
return average