Gleitenden Durchschnitt implementiert
This commit is contained in:
parent
118b0d87ff
commit
a0638c76e5
2
INSTALL
2
INSTALL
|
@ -1,6 +1,8 @@
|
|||
Das Programm kann direkt gestartet werden. Eine Installation ist nicht
|
||||
erforderlich. Die unten angegebenen Abhängigkeiten müssen erfüllt sein.
|
||||
|
||||
Python muß mindestens Version 3.10 sein.
|
||||
|
||||
apt-get install python3-cairo python3-gi python3-gi-cairo gir1.2-rsvg-2.0 \
|
||||
python-serial python3-nmea2 python3-smbus2 python3-bme280
|
||||
|
||||
|
|
|
@ -1,25 +1,56 @@
|
|||
'''
|
||||
"""
|
||||
Moving Average
|
||||
returns a float so no integer rounding applied
|
||||
|
||||
Sortierung einer Liste nach Alter? FIFO?
|
||||
|
||||
'''
|
||||
|
||||
import time
|
||||
"""
|
||||
|
||||
class mAvg():
|
||||
|
||||
def __init__(self, interval):
|
||||
self.interval = interval
|
||||
self.avg = None
|
||||
self.data = []
|
||||
def __init__(self, size):
|
||||
self.size = size
|
||||
self.data = [0] * size
|
||||
self.nval = 0
|
||||
self.sum = 0
|
||||
self.next = 0
|
||||
|
||||
def addVal(self, value):
|
||||
self.data.append((time.time(), value))
|
||||
# add a new value und return new current avg
|
||||
if self.nval < self.size:
|
||||
# list is not filled
|
||||
self.nval += 1
|
||||
self.sum += value
|
||||
else:
|
||||
# list is filled
|
||||
self.sum = self.sum - self.data[self.next] - value
|
||||
self.data.[self.next] = value
|
||||
self.next += 1
|
||||
# if end of list reached, start over from beginning
|
||||
if self.next = self.size:
|
||||
self.next = 0
|
||||
return self.sum / self.nval
|
||||
|
||||
def setInterval(self, interval):
|
||||
pass
|
||||
def getAvg(self, points=None):
|
||||
if self.nval == 0:
|
||||
return None
|
||||
# get avg of all collected data
|
||||
if not points:
|
||||
return self.sum / self.nval
|
||||
# get avg of subset
|
||||
if points > self.nval:
|
||||
return None
|
||||
sum = 0
|
||||
i = self.next
|
||||
p = points
|
||||
while p > 0:
|
||||
p -= 1
|
||||
if i == 0:
|
||||
i = self.size - 1
|
||||
else:
|
||||
i -= 1
|
||||
sum += self.data[i]
|
||||
return sum / points
|
||||
|
||||
def getAvg(self):
|
||||
return self.avg
|
||||
|
||||
def reset(self):
|
||||
self.nval = 0
|
||||
self.next = 0
|
||||
self.sum = 0
|
||||
|
|
Loading…
Reference in New Issue