Class for automatic handling of moving averages.
MovingAverage.cpp@0:332104904b69, 2019-03-29 (annotated)
- Committer:
- fbcosentino
- Date:
- Fri Mar 29 15:44:58 2019 +0000
- Revision:
- 0:332104904b69
Initial version of a moving average class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fbcosentino | 0:332104904b69 | 1 | #include "MovingAverage.h" |
fbcosentino | 0:332104904b69 | 2 | |
fbcosentino | 0:332104904b69 | 3 | MovingAverage::MovingAverage(int number_of_samples) { |
fbcosentino | 0:332104904b69 | 4 | _started = 0; |
fbcosentino | 0:332104904b69 | 5 | _average = 0.0; |
fbcosentino | 0:332104904b69 | 6 | _num_samples = number_of_samples; |
fbcosentino | 0:332104904b69 | 7 | } |
fbcosentino | 0:332104904b69 | 8 | |
fbcosentino | 0:332104904b69 | 9 | float MovingAverage::filter(float value) { |
fbcosentino | 0:332104904b69 | 10 | // If this filter has already started, means we have other samples |
fbcosentino | 0:332104904b69 | 11 | if (_started) { |
fbcosentino | 0:332104904b69 | 12 | float dropping_value; |
fbcosentino | 0:332104904b69 | 13 | if (_samples.get(&dropping_value) == 0) { // if no errors |
fbcosentino | 0:332104904b69 | 14 | // if value == dropping_value, average is kept the same |
fbcosentino | 0:332104904b69 | 15 | _average = _average + ((value - dropping_value) / _num_samples); |
fbcosentino | 0:332104904b69 | 16 | _samples.put(value); |
fbcosentino | 0:332104904b69 | 17 | } |
fbcosentino | 0:332104904b69 | 18 | } |
fbcosentino | 0:332104904b69 | 19 | // If we are starting now, this sample is all we have |
fbcosentino | 0:332104904b69 | 20 | else { |
fbcosentino | 0:332104904b69 | 21 | // fills the buffer with the initial value |
fbcosentino | 0:332104904b69 | 22 | for (int i=0; i < _num_samples; i++) _samples.put(value); |
fbcosentino | 0:332104904b69 | 23 | _started = 1; |
fbcosentino | 0:332104904b69 | 24 | _average = value; // Average is the initial value. |
fbcosentino | 0:332104904b69 | 25 | } |
fbcosentino | 0:332104904b69 | 26 | return _average; |
fbcosentino | 0:332104904b69 | 27 | } |
fbcosentino | 0:332104904b69 | 28 | |
fbcosentino | 0:332104904b69 | 29 | |
fbcosentino | 0:332104904b69 | 30 | |
fbcosentino | 0:332104904b69 | 31 | // FIFO HELPER CLASS |
fbcosentino | 0:332104904b69 | 32 | |
fbcosentino | 0:332104904b69 | 33 | _ma_fifo::_ma_fifo() { |
fbcosentino | 0:332104904b69 | 34 | head = 0; |
fbcosentino | 0:332104904b69 | 35 | tail = 0; |
fbcosentino | 0:332104904b69 | 36 | } |
fbcosentino | 0:332104904b69 | 37 | int _ma_fifo::available() { |
fbcosentino | 0:332104904b69 | 38 | return (_MA_FIFO_SIZE + this->head - this->tail) % _MA_FIFO_SIZE; |
fbcosentino | 0:332104904b69 | 39 | } |
fbcosentino | 0:332104904b69 | 40 | int _ma_fifo::free() { |
fbcosentino | 0:332104904b69 | 41 | return (_MA_FIFO_SIZE - 1 - available()); |
fbcosentino | 0:332104904b69 | 42 | } |
fbcosentino | 0:332104904b69 | 43 | int _ma_fifo::put(float data) { |
fbcosentino | 0:332104904b69 | 44 | int next; |
fbcosentino | 0:332104904b69 | 45 | // check if FIFO has room |
fbcosentino | 0:332104904b69 | 46 | next = (head + 1) % _MA_FIFO_SIZE; |
fbcosentino | 0:332104904b69 | 47 | if (next == tail) return 1; // fifo full |
fbcosentino | 0:332104904b69 | 48 | |
fbcosentino | 0:332104904b69 | 49 | buffer[head] = data; |
fbcosentino | 0:332104904b69 | 50 | head = next; |
fbcosentino | 0:332104904b69 | 51 | return 0; |
fbcosentino | 0:332104904b69 | 52 | } |
fbcosentino | 0:332104904b69 | 53 | int _ma_fifo::get(float * data) { |
fbcosentino | 0:332104904b69 | 54 | int next; |
fbcosentino | 0:332104904b69 | 55 | // check if FIFO has data |
fbcosentino | 0:332104904b69 | 56 | if (head == tail) return 1; // FIFO empty |
fbcosentino | 0:332104904b69 | 57 | |
fbcosentino | 0:332104904b69 | 58 | next = (tail + 1) % _MA_FIFO_SIZE; |
fbcosentino | 0:332104904b69 | 59 | *data = buffer[tail]; |
fbcosentino | 0:332104904b69 | 60 | tail = next; |
fbcosentino | 0:332104904b69 | 61 | return 0; |
fbcosentino | 0:332104904b69 | 62 | } |