Trent "ripred" Wyatt's Smooth Library Aims to Make Moving Averages More Performant on Arduinos

Designed to tame "noisy" inputs, this moving average implementation gets to work immediately and requires no memory-eating arrays.

Gareth Halfacree
11 months ago β€’ Sensors

Developer and microcontroller enthusiast Trent "ripred" Wyatt has published a new smoothing library designed to offer performant running average calculation to any Arduino-compatible microcontroller.

"You create the object and tell it what the running sample window size is," Wyatt explains of his library's operation. "Normally you would implement this using an array of past sample values and divide the sum by the number of samples. This object takes up eight bytes no matter the window size. And there's no looping over past values. And no arrays."

The calculation of a moving average is designed to smooth out sudden changes over time, ideal for inputs which are used for controlling motors, fading lights, and other systems whereby a sudden change in value is undesirable. Dubbed Smooth, the library uses coefficients to keep the running average, rather than an array as with rival implementations β€” something which, its creator says, saves a considerably amount of processing time and memory.

"Another big advantage is that the calculation is fast and constant time with no looping regardless of the sample window size," Wyatt adds. "Another advantage is that the way it is written you still get a valid average even before N samples have been added. The window size of N is completely configurable when the Smooth object is created as well as at runtime using the set_window(int const size) method. The object works natively with double values so it can accommodate pretty much every use case."

The Smooth library is now available from the Arduino IDE in the Libraries list, with full source code and examples published to GitHub under the permissive MIT license. Additional discussion is available on Wyatt's Reddit post.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles