Smoothing values over time: moving average or something better?

后端 未结 5 1541
误落风尘
误落风尘 2020-12-22 19:01

I\'m coding something at the moment where I\'m taking a bunch of values over time from a hardware compass. This compass is very accurate and updates very often, with the res

5条回答
  •  一整个雨季
    2020-12-22 19:26

    If you are trying to remove the occasional odd value, a low-pass filter is the best of the three options that you have identified. Low-pass filters allow low-speed changes such as the ones caused by rotating a compass by hand, while rejecting high-speed changes such as the ones caused by bumps on the road, for example.

    A moving average will probably not be sufficient, since the effects of a single "blip" in your data will affect several subsequent values, depending on the size of your moving average window.

    If the odd values are easily detected, you may even be better off with a glitch-removal algorithm that completely ignores them:

    if (abs(thisValue - averageOfLast10Values) > someThreshold)
    {
        thisValue = averageOfLast10Values;
    }
    

    Here is a guick graph to illustrate:

    graph comparison

    The first graph is the input signal, with one unpleasant glitch. The second graph shows the effect of a 10-sample moving average. The final graph is a combination of the 10-sample average and the simple glitch detection algorithm shown above. When the glitch is detected, the 10-sample average is used instead of the actual value.

提交回复
热议问题