gpt4 book ai didi

algorithm - C代码中最后1000个模拟步骤算法的平均值

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:47 26 4
gpt4 key购买 nike

我想用 C 来计算我正在使用的软件中最后 1000 个模拟步骤的平均值。求解器的步长为 1 毫秒,其中一个信号在振荡,因此我想将该信号过滤得更平滑。我们的软件使我们能够使用 C 代码作为接口(interface),这意味着我将信号(振荡的信号)作为 [0] 变量驱动到 C 代码中,而我从 C 代码接收到的是 y[0],代表过滤后的信号。 C 代码和软件之间的信息在每个时间步交换。

现在我想知道这段代码应该是什么样子。代码的想法是计算最后 n 个步骤的每个时间步长的平均值,其中 n 值决定了我要考虑多少个历史时间步长。我将从 n=1000 开始,然后根据情况减少/增加该值。

最佳答案

您可以使用 Exponential Moving Average过滤器。

实际应用中,可以使用如下代码:

static double ema_val = 0.0;        // Initial value
static double ema_k = 1.0 / 1000; // Weight of single event in sum

// Code for update the average value ema_val with the new_val
ema_val += ema_k * (new_val - ema_val);

关于algorithm - C代码中最后1000个模拟步骤算法的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com