Smoothing values over time: moving average or something better?

后端 未结 5 1546
误落风尘
误落风尘 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:18

    Moving average I can get down with ... but it strikes me that it's probably quite inefficient.

    There's really no reason a moving average should be inefficient. You keep the number of data points you want in some buffer (like a circular queue). On each new data point, you pop the oldest value and subtract it from a sum, and push the newest and add it to the sum. So every new data point really only entails a pop/push, an addition and a subtraction. Your moving average is always this shifting sum divided by the number of values in your buffer.

    It gets a little trickier if you're receiving data concurrently from multiple threads, but since your data is coming from a hardware device that seems highly doubtful to me.

    Oh and also: awful self-taught programmers unite! ;)

提交回复
热议问题