Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每2秒取一次平均值_C#_Moving Average - Fatal编程技术网

C# 每2秒取一次平均值

C# 每2秒取一次平均值,c#,moving-average,C#,Moving Average,我对数组有问题,或者在这些文本中遗漏了什么。。 我的程序每500毫秒运行一次,我想读取前4个双倍值,取这些值的平均值,然后得到下4个双倍值,依此类推。。。我写了一些关于这个的东西,你能看看这个吗 if (u_dcbus_pv_act[i] > 0 && i != 0) { u_dcbus_pv = u_dcbus_pv_act[i]; p_dcbus_pv = p_dcbus_pv_act[i]; } if (i >= 3) { for (in

我对数组有问题,或者在这些文本中遗漏了什么。。 我的程序每500毫秒运行一次,我想读取前4个双倍值,取这些值的平均值,然后得到下4个双倍值,依此类推。。。我写了一些关于这个的东西,你能看看这个吗

if (u_dcbus_pv_act[i] > 0 && i != 0)
{
    u_dcbus_pv = u_dcbus_pv_act[i];
    p_dcbus_pv = p_dcbus_pv_act[i];
}
if (i >= 3)
{
    for (int j = 0; j < 4; j++)
    {
        total_u += u_dcbus_pv;
        total_p += p_dcbus_pv;
    }

    average_u = total_u / 4;
    average_p = total_p / 4;
    u_dcbus_target = average_u;
    p_dcbus_pv_avg = average_p;
}

根据我对你描述的理解,我会这样做:

 /* add current samples to totals */
 total_u += u_dcbus_pv_act[i];
 total_p += p_dcbus_pv_act[i];

 /* every fourth tick, calc average and reset totals */
 if (i % 4 == 0)
 {
     average_u = total_u / 4;
     average_p = total_p / 4;
     total_u = 0;
     total_p = 0;
 }
 u_dcbus_target = average_u;
 p_dcbus_pv_avg = average_p;
 i++;

是的,我们可以看看这个。现在你的问题是什么?