Calculate rolling / moving average in C++(在 C++ 中计算滚动/移动平均值)
问题描述
我知道这可以通过 boost 实现:
I know this is achievable with boost as per:
使用boost::accumulators,如何重置滚动窗口大小,是否保留额外的历史记录?
但我真的很想避免使用 boost.我已经用谷歌搜索过,但没有找到任何合适或可读的例子.
But I really would like to avoid using boost. I have googled and not found any suitable or readable examples.
基本上,我想使用最近的 1000 个数字作为数据样本来跟踪正在进行的浮点数流的移动平均值.
Basically I want to track the moving average of an ongoing stream of a stream of floating point numbers using the most recent 1000 numbers as a data sample.
实现这一目标的最简单方法是什么?
What is the easiest way to achieve this?
我尝试使用圆形阵列、指数移动平均线和更简单的移动平均线,发现圆形阵列的结果最适合我的需要.
I experimented with using a circular array, exponential moving average and a more simple moving average and found that the results from the circular array suited my needs best.
推荐答案
您只需要一个包含 1000 个元素的循环数组(循环缓冲区),您可以在其中将元素添加到前一个元素并存储它.
You simply need a circular array (circular buffer) of 1000 elements, where you add the element to the previous element and store it.
它变成了一个递增的和,你总是可以得到任意两对元素之间的和,然后除以它们之间的元素数,得到平均值.
It becomes an increasing sum, where you can always get the sum between any two pairs of elements, and divide by the number of elements between them, to yield the average.
这篇关于在 C++ 中计算滚动/移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中计算滚动/移动平均值
基础教程推荐
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
