I’m looking for a way to calculate the mean value from a series of arrays. The window should contain 3 values and slide to the third last place in the array, then it saves the last 2 values and if the new array arrives within a certain time (e.g. 2 seconds), the last two values are added to the new array and the data is recalculated. If it takes longer than the time, the array starts again and the last two values are discarded. The data is then plotted live (plotting already works, I’m just not getting any further with the calculation). Here is my attempt so far, unfortunately my window freezes.
# -*- coding: latin-1 -*-
import numpy as np
import time
def smooth_data(data, previous_remainder=None):
"""
Glättet eine Sequenz von Datenpunkten, indem für jedes Fenster von drei aufeinanderfolgenden Datenpunkten der Mittelwert berechnet wird.
Parameter:
- data: Eine Sequenz von Datenpunkten, die geglättet werden sollen.
- previous_remainder: Die letzten beiden Datenpunkte des vorherigen Arrays, die in das aktuelle Array integriert werden sollen.
Rückgabewerte:
- smoothed_data: Eine Liste mit den geglätteten Daten.
- remainder: Die letzten beiden Datenpunkte des verarbeiteten Arrays, die für den nächsten Durchlauf verwendet werden sollen.
"""
if previous_remainder is not None:
data = np.concatenate((previous_remainder, data))
smoothed_data = []
remainder = data[-2:]
for i in range(len(data) - 2):
window = data[i:i+3]
mean_value = np.mean(window)
smoothed_data.append(mean_value)
return smoothed_data, remainder
def continuous_data_processing(data, time_window=3.0):
previous_remainder = None
last_data = None
Sensordaten_last = None
while True:
Sensordaten_aktuell = data
current_time = time.time()
if Sensordaten_aktuell is not None and Sensordaten_last is not None and not np.array_equal(Sensordaten_last, Sensordaten_aktuell):
if last_data is not None and current_time - last_data > time_window:
previous_remainder = None
filtered_data, remainder = smooth_data(Sensordaten_aktuell, previous_remainder)
yield filtered_data # Verwenden Sie yield, um die geglätteten Daten zurückzugeben
last_data = current_time
previous_remainder = remainder
Sensordaten_last = Sensordaten_aktuell
I have write the code above.