Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MovingAverage.h
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
Generated on Mon Jul 18 2022 08:52:40 by
1.7.2