# Circular buffers

Let's take a closer look at how the temperature samples were collected in our calibrated thermistor project.

In many scientific projects, keeping good statistics helps to understand when data are real (or significant) and when they are the result of noise (random variations in the measurement). We created the class Circular to collect samples and do simple statistics on them so we could find out how accurate our thermometer reading were. Here is that class once again:

class Circular

{

double samples[ 200 ];

long int count;

double mean_value;

double standard_deviation;

double variance;

enum { COUNT = (sizeof samples / sizeof *samples) };

public:

Circular( void ) { count = 0; }

void store( double value ) { samples[ count++ % (sizeof samples / sizeof *samples) ] = value; }

void

calculate_statistics( void )

{

double sum = 0;

int cnt = min( count, COUNT );

for( int i = 0; i < cnt; i++ )

sum += samples[ i ];

mean_value = sum / cnt;

variance = 0;

for( int i = 0; i < cnt; i++ )

{

double deviation = samples[ i ] - mean_value;

variance += deviation * deviation;

}

variance /= ( cnt - 1 );

standard_deviation = sqrt( variance );

}

double mean( void ) { return mean_value; }

double std_dev( void ) { return standard_deviation; }

double var( void ) { return variance; }

int num_samples( void ) { return min( count, COUNT ); }

};

The class gets its name from a simple concept. We want to collect up to 200 samples, and no more. But we always want to analyze the last samples that came in. We put each sample into its own slot in an array that can hold 200 samples. But when we get the 201st sample, we copy it into the first slot, over-writing the first sample. The next sample goes into slot 2, and so on.

Let's look at how that happens. The array of samples is declared by the line

The line

store( double value )

{

samples[ count++ % (sizeof samples / sizeof *samples) ] = value;

}

**count**, which keeps track of how many samples we have collected. The two plus signs after it mean that it gets incremented (we add one to it) after it is used. The percent sign means to take the value of

**count**(before it is incremented), and divide it by 200 (our sizeof trick), and use the remainder as our index into the array. Then the variable

**value**is stored in the

**samples**array at that index.

**samples[0]**.

**count**is incremented, so its value is 1.

**public**is a funny declaration:

*constructor*. Whenever we create an instance of our class, this method will be called, just once.

**count**variable to zero.

**calculate_statistics**().

**count**variable will be less than 200, and sometimes it will be much greater. We want to know how many samples we have collected. That number is the smaller of

**count**or 200. That is what

**min**() gives us.

**for**() statement to do that.

sum += samples[ i ];

**i**and set it to zero. Then as long as

**i**is less than cnt, do the next line, and then increment

**i**.

**sum**.

**for**() statement calculates the sum of the squared differences from the mean.

{

double deviation = samples[ i ] - mean_value;

variance += deviation * deviation;

}