When you use Arduino to sense with the digital inputs there are no doubts: it is either on or off. When it comes to analog input instead, the noise and/or other reasons make the reading oscillate. This project allows the calculation of a basic statistics summary on sampled data (average, standard deviation, min, max), without saving the whole bunch of data! So it is very useful on low resources hardware as Arduino. This can be applied to any analog quantity measured by an Arduino.

As an example I made a thermometer and connected Arduino to an LCD. This project does not include particular difficulties. To build it you may also refer to other projects/tutorials.

The software part is the main contribution of this project. Here how to use it:

```
#include "AvgStd.h"
AvgStd mySamples;
void setup(){
mySamples = AvgStd();
mySamples.setRejectionSigma(7.0);
}
void loop(){
float value = someArduinoSample(); // here you get a value of some
// kind from your arduino board.
mySamples.checkAndAddReading(value); // add a value with rejection rule
// get the average and standard deviation, min, max and number of samplings;
float average = mySamples.getMean();
float std = mySamples.getStd();
float min = mySamples.getMin();
float max = mySamples.getMax();
int N = mySamples.getN();
}
```

But here is how it all started...

Some time ago I was sick with some fever. The only thing I could do was to lay in bed and measure my body temperature. The thermometer took so little time to measure that I tried again and again. Each time the temperature was different.

I thought to measure a few times and then to calculate the average, to get a more stable result.

Doing it without a piece of paper starts to be a bit difficult when you have to deal with more than 5 samples (at least for me). I started thinking that there must be a way to update the last calculated average with the next sample.

I easily worked out the formula:

<T>_(N+1) = N/(N+1) * <T>_N + T_(N+1)/(N+1)

where <T>_(N+1) is the average calculated with N+1 samples and N is the number of samples. This formula says that we can calculate the next average by keeping in mind only 3 numbers: the previous average, the number of samplings and the last sample. That's nice!

I worked out also the rule for the standard deviation, or better for the variance.

var_(N+1) = var_N * ((N-1)/N) + (T_(N+1) - <T>_N)^2

Here, you need to remember 4 numbers, the previous average and variance, the last sample and the number of samplings.

This rules are very useful especially on low resource hardware because they do not require a lot of memory. Only 4 variables (3 floats and an int) have to be stored. I put them together in a simple project, because I think they can be useful for many.

I later found that this formulas were already known (guess what??) but it was nice to find them by myself!

## Comments