考虑一个小型监控设备,它每10秒显示一次平均温度。
timestamp value
20190304000000 62.7
20190304000010 62.5
20190304000020 62.8
....
....如何在不增加内存占用的情况下计算和更新平均值。也就是说,整个数据存储(永久或内存)是不可能的
发布于 2019-03-06 14:08:12
除了其他答案之外,您可能还想使用IIR filter来获取Exponential moving average:filter that applies weighting factors which decrease exponentially,因此最后一个值比旧值有更大的影响
newAverage = OldAverage * (1-alpha) + NewValue * alpha其中,alpha是与时间/常数减少有关的小值,如0.1
发布于 2019-03-06 13:55:44
保存总数和记录的温度数量的计数。然后,每次报告答案时,将总和除以计数,以避免复合浮点错误。
from itertools import count
temperature_sum = 0
for temperature_count in count(1):
temperature_sum += read_from_sensor()
print("Average: {}".format(temperature_sum / temperature_count))发布于 2019-03-06 13:44:52
我们需要两个变量
int count;
float average;
void main() {
do while(true) {
float temperature = ReadFromSensor(); //not included
average = ((average*count) + temperature)/ ++count;
cout << "average: " << average << endl;
}
}https://stackoverflow.com/questions/55016337
复制相似问题