Fernando Cosentino / MovingAverage
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MovingAverage.h Source File

MovingAverage.h

Go to the documentation of this file.
00001 /** @file MovingAverage.h
00002  *  A moving average filter class for analog-to-digital converters.
00003  *  This version uses static memmory allocation. Not RAM optimized
00004  *  but increases device compatibility avoiding malloc calls.
00005  *
00006  *  @author Fernando Cosentino
00007  */
00008 
00009 #ifndef __MOVINGAVERAGE
00010 #define __MOVINGAVERAGE
00011 
00012 #define _MA_FIFO_SIZE 65 // must be sample size + 1
00013 
00014 /**
00015  * Helper class implementing a float FIFO.
00016  */
00017 class _ma_fifo
00018 {
00019     float buffer[_MA_FIFO_SIZE];
00020     unsigned int head, tail;
00021 
00022 public:
00023     _ma_fifo();
00024     int put(float data);// returns 0 on success
00025     int get(float * data);
00026     int available();
00027     int free();
00028 };
00029 
00030 /**
00031  * Receives a constant stream of samples and outputs the last available
00032  * average. The value is in the past by half the number of averaged samples.
00033  */
00034 class MovingAverage {
00035 public:
00036     /**
00037      * Creates an instance of the MovingAverage class.
00038      *
00039      * @param number_of_samples Number of samples to average per output value.
00040      * Maximum value is 64.
00041      */
00042     MovingAverage(int number_of_samples);
00043     
00044     /**
00045      * Inserts a new value into the filter, and returns the updated average.
00046      *
00047      * @param value The new value to be inserted.
00048      * @returns The updated average including the new value.
00049      */
00050     float filter(float value);
00051 
00052 private:
00053     _ma_fifo _samples;
00054     int _started;
00055     int _num_samples;
00056     float _average;
00057     
00058 
00059 };
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 #endif