简单移动平均总和/偏移问题总和、平均、简单、问题

2023-09-11 07:07:01 作者:野性の小男人

我一个简单移动平均线与读出一个介于0和10V电压温度的移动窗口中写道。

I wrote a simple moving average with a moving window of Temperatures read as a voltage between 0 and 10V.

该算法似乎正常工作,然而,它具有在那里,这依赖于温度的第一填充所述窗口中的问题,移动平均将有一个偏移量不接近该值的任何值。例如,运行此程序的温度。传感器插在一个房间里临时产量4.4V或21.3℃。不过,如果我拔掉温度。传感器的电压下降到1.4V尚未移动平均停留在1.6V。此偏移变小予增加窗口的大小。如何消除这种偏差甚至对小窗口尺寸,如。 20?

The algorithm appears to work correctly, however, it has a problem where depending upon which Temperatures filled the window first, the moving average will have an offset for any values not near this value. For example, running this program with the temp. sensor plugged in a room temp yields 4.4V or 21.3 C. Though, if I unplug the temp. sensor the voltage drops to 1.4V yet the moving average stays at 1.6V. This offset becomes smaller as I increase the size of the window. How remove this offset even for small window sizes eg. 20 ?

REM SMA Num Must be greater than 1
#DEFINE SMANUM 20
PROGRAM
'Program 3 - Simple Moving Average Test
CLEAR
DIM SA(1)
DIM SA0(SMANUM) : REM Moving Average Window as Array
DIM LV1
DIM SV2
LV0 = 0 : REM Counter
SV0 = 0 : REM Average
SV1 = 0 : REM Sum
WHILE(1)
    SA0(LV0 MOD SMANUM) = PLPROBETEMP : REM add Temperature to head of window
    SV1 = SV1 + SA0(LV0 MOD SMANUM) : REM add new value to sum
    IF(LV0 >= (SMANUM)) : REM check if we have min num of values
        SV1 = SV1 - SA0((LV0+1) MOD SMANUM) : REM remove oldest value from sum
        SV0 = SV1 / SMANUM : REM calc moving average
        PRINT "Avg: " ; SV0 , " Converted: " ; SV0 * 21.875 - 75
    ENDIF
    LV0 = LV0 + 1 : REM increment counter
WEND
ENDP

(注意,这是写在个ACROBASIC为ACR9000由Parker)

(Note this is written in ACROBASIC for the ACR9000 by Parker)

输出 - 连接温度传感器

Output - Temp Sensor attached

Raw: 4.43115    Avg: 4.41926     Converted: 21.6713125
Raw: 4.43115    Avg: 4.41938     Converted: 21.6739375
Raw: 4.43359    Avg: 4.41963     Converted: 21.67940625
Raw: 4.43359    Avg: 4.41987     Converted: 21.68465625
Raw: 4.43359    Avg: 4.42012     Converted: 21.690125
Raw: 4.43359    Avg: 4.42036     Converted: 21.695375
Raw: 4.43359    Avg: 4.42061     Converted: 21.70084375

...删除温度传感器,而程序运行

...remove temp sensor while program is running

Raw: 1.40625    Avg: 1.55712     Converted: -40.938
Raw: 1.40381    Avg: 1.55700     Converted: -40.940625
Raw: 1.40625    Avg: 1.55699     Converted: -40.94084375
Raw: 1.40625    Avg: 1.55699     Converted: -40.94084375
Raw: 1.40381    Avg: 1.55686     Converted: -40.9436875
Raw: 1.40381    Avg: 1.55674     Converted: -40.9463125
Raw: 1.40625    Avg: 1.55661     Converted: -40.94915625

有一个明显的偏移去除传感器后仍出现原料和均线之间。

A noticeable offset appears between the raw and moving average after removing the sensor.

偏移也发生在相反的顺序:

The offset also occurs in the reverse order:

输出 - 开始计划瓦特/温度传感器删除

Output - Begin program w/ Temp Sensor removed

Raw: 1.40381    Avg: 1.40550     Converted: -44.2546875
Raw: 1.40625    Avg: 1.40550     Converted: -44.2546875
Raw: 1.40625    Avg: 1.40549     Converted: -44.25490625
Raw: 1.40625    Avg: 1.40549     Converted: -44.25490625
Raw: 1.40625    Avg: 1.40548     Converted: -44.255125
Raw: 1.40625    Avg: 1.40548     Converted: -44.255125

...附加温度传感器,而程序运行

... attach temp sensor while program is running

Raw: 4.43848    Avg: 4.28554     Converted: 18.7461875
Raw: 4.43848    Avg: 4.28554     Converted: 18.7461875
Raw: 4.43848    Avg: 4.28554     Converted: 18.7461875
Raw: 4.43848    Avg: 4.28554     Converted: 18.7461875
Raw: 4.43848    Avg: 4.28554     Converted: 18.7461875
Raw: 4.43359    Avg: 4.28530     Converted: 18.7409375

安装传感器后

同样引人注目的偏移出现的原均线之间。

Again noticeable offset appears between the raw and moving average after attaching the sensor.

推荐答案

这个问题似乎是,当时正在从和中减去的价值实际上并没有在数组中最古老的价值 - 最古老的价值,事实上由新值在 WHILE 循环的第一线覆盖。这是当时正在从总和减去第二古老的值。

The problem seems to be that the value that was being subtracted from the sum was not actually the oldest value in the array -- the oldest value was, in fact, overwritten by the new value in the first line of the WHILE loop. It was the second-oldest value that was being subtracted from the sum.

编辑更改平均和总和变量到64位浮点处理precision损失随着时间的推移,在OP的建议。

EDIT Changed Average and Sum variable to 64-bit floating point to address precision loss over time, on the OP's advice.

确保最旧值先减去(一旦数组已满)给出了预期的答案:

Ensuring that the oldest value is subtracted first (once the array is full) gives the expected answer:

PROGRAM
'Program 3 - Simple Moving Average Test
CLEAR
DIM SA(1)
DIM SA0(SMANUM) : REM Moving Average Window as Array
DIM LV1
DIM DV2
LV0 = 0 : REM Counter
DV0 = 0 : REM Average
DV1 = 0 : REM Sum
WHILE(1)
    IF(LV0 >= (SMANUM)) : REM check if we have min num of values
        DV1 = DV1 - SA0(LV0 MOD SMANUM) : REM remove oldest value from sum
    ENDIF
    SA0(LV0 MOD SMANUM) = PLPROBETEMP : REM add Temperature to head of window
    DV1 = DV1 + SA0(LV0 MOD SMANUM) : REM add new value to sum
    IF(LV0 >= (SMANUM)) : REM check if we have min num of values
        DV0 = DV1 / SMANUM : REM calc moving average
        PRINT "Avg: " ; DV0 , " Converted: " ; DV0 * 21.875 - 75
    ENDIF
    LV0 = LV0 + 1 : REM increment counter
WEND

我没有一个正在运行的基本环境,但我在Python测试了这一点,并得到了同样的不正确输出code等同于您的版本和code相当于预期的输出,我已经版本上方插入。

I don't have a running BASIC environment but I tested this in Python and got the same incorrect output for code equivalent to your version and the expected output for code equivalent to the version that I've inserted above.