计算可变参数的移动平均值 [英] Calculating moving-averages of variable parameters

查看:116
本文介绍了计算可变参数的移动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数属性,每秒更新一次,信号强度值介于0 - 100之间。

I have an integer property which is updated every second with a signal-strength value ranging from 0 - 100.

我希望能保持一个在过去的10次,25次,50次测量中持续测量移动平均值。

I'd like to be able to keep an ongoing measure of the moving average over the last 10, 25, 50 measurements.

这样做最有效的方法是什么?

What's the most efficient way of doing this?

我正在考虑使用NSMutableArray实现一组FIFO队列,并在每次在阵列具有必要条件时在末尾添加新值时弹出前导值条目数量。但是,我不确定是否有更有效的方法可以做到这一点。

I'm currently thinking of implementing a set of FIFO queues using NSMutableArray and popping the leading value every time I add a new one at the end once the array has the requisite number of entries. However, I'm not sure if there's a more efficient way of doing this or not.

推荐答案

队列是正确的方法。真正的效率来自你如何重新计算平均值。

A queue is the right way. The real efficiency comes with how you recalculate the average.

它应该用:

avg = avg + newSample/N - [queue dequeue]/N
[queue enqueue:newSample]

即新的运行平均值只是旧的平均值减去您丢弃的最旧值的权重,加上您排队的最新值的权重。

i.e. the new running average is simply the old average minus the weight of the oldest value you dropped, plus the weight of the newest value you enqueued.

这篇关于计算可变参数的移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆