# Microcontroller statistics with a small SRAM footprint

You may know your way around the registers of that favorite microcontroller, but at some point you’ll also need to wield some ninja-level math skills to manage arrays of data on a small device. [Scott Daniels] has some help for you in this arena. He explains how to manage statistical calculations on your collected data without eating up all the RAM. The library which he made available is targeted for the Arduino. But the concepts, which he explains quite well, should be easy to port to your preferred hardware.

The situation he outlines in the beginning his post is data collected from a sensor, but acted upon by the collection device (as opposed to a data logger where you dump the saved numbers and use a computer for the heavy lifting). This can take the form of a touch sensor, which are known for having a lot of noise when looking at individual readings. But since [Scott] is using the Mean and Standard Deviation to keep running totals of collected data over time it is also very useful for applications like building your own home heating thermostat.

1. AS says:

I haven’t grabbed the code, but the description in the linked page seems to show the author making every sort of numerical stability and loss of precision mistake possible. Even wikipedia has a good tutorial Kunth’s algorithms for keeping these calculations stable and precise.

2. Michal says:

It seems that the integer version is using “sqrt”. That is the float implementation for sqrt. Probably should have implemented own isqrt function.

3. Justin says:

The first rule of efficient programming for AVR is to ignore that inefficient software clusterfuck called arduino and all its affiliates. Making efficient libraries for arduino is like mounting a V8 on your dolly to pull a lexus. It’ll be a nice video on youtube but anyone who uses it doesn’t get how things work.

• SavannahLion says:

At least someone gets it. I was a little mystefied as to why the author was trading off 200 bytes of SRAM for a floating library when that same space could have easily be used to buffer his 10 values for calculating his average and mean. It took a little fiddling but once you wrap your head around the pointer acrobatics of a Round Robin table, the rest of the work is just figuring out what kind of precision you want. And I did mine in assembly…

• John says:

Do I detect sarcasm or are you just very proud? If it’s the former, I think we should be celebrating the fact that some young people actually care about the low-level details. If it’s the latter, you’re definitely proud of something alright.

• SavannahLion says:

Let me see if I can put it another way. Arduino now is like what VB was ten years ago. When you start jumping through more and more hoops to accomplish something that a “lower” language can easily do, it’s time to rethink the strategy.

• dext0rb says: