From a5ea1eb0270b4902d04a783484a6b2c704553bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 21 Dec 2017 17:09:38 +0100 Subject: [PATCH] Refactor filters --- code/espurna/filters/BaseFilter.h | 2 +- code/espurna/filters/MaxFilter.h | 2 +- code/espurna/filters/MedianFilter.h | 14 +++++-- code/espurna/filters/MovingAverageFilter.h | 44 +++++++++++++++------- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/code/espurna/filters/BaseFilter.h b/code/espurna/filters/BaseFilter.h index bef36c65..fb422c32 100644 --- a/code/espurna/filters/BaseFilter.h +++ b/code/espurna/filters/BaseFilter.h @@ -32,7 +32,7 @@ class BaseFilter { virtual double max() { double max = 0; for (unsigned char i = 1; i < _data.size(); i++) { - if (max < _data.at(i)) max = _data.at(i); + if (max < _data[i]) max = _data[i]; } return max; } diff --git a/code/espurna/filters/MaxFilter.h b/code/espurna/filters/MaxFilter.h index 58f6ec69..683e58f6 100644 --- a/code/espurna/filters/MaxFilter.h +++ b/code/espurna/filters/MaxFilter.h @@ -12,7 +12,7 @@ class MaxFilter : public BaseFilter { public: - virtual double result() { + double result() { return max(); } diff --git a/code/espurna/filters/MedianFilter.h b/code/espurna/filters/MedianFilter.h index 005788fd..531252a4 100644 --- a/code/espurna/filters/MedianFilter.h +++ b/code/espurna/filters/MedianFilter.h @@ -12,9 +12,13 @@ class MedianFilter : public BaseFilter { public: virtual void reset() { + + // When resetting we get the last value + // and make it the first of the new series double last = _data.empty() ? 0 : _data.back(); _data.clear(); add(last); + } virtual double result() { @@ -25,9 +29,13 @@ class MedianFilter : public BaseFilter { for (unsigned char i = 1; i <= _data.size() - 2; i++) { - double previous = _data.at(i-1); - double current = _data.at(i); - double next = _data.at(i+1); + // For each position, + // we find the median with the previous and next value + // and use that for the sum + + double previous = _data[i-1]; + double current = _data[i]; + double next = _data[i+1]; if (previous > current) std::swap(previous, current); if (current > next) std::swap(current, next); diff --git a/code/espurna/filters/MovingAverageFilter.h b/code/espurna/filters/MovingAverageFilter.h index 7855ff22..0c48eff9 100644 --- a/code/espurna/filters/MovingAverageFilter.h +++ b/code/espurna/filters/MovingAverageFilter.h @@ -12,30 +12,48 @@ class MovingAverageFilter : public BaseFilter { public: - MovingAverageFilter(unsigned char size) { - _size = size; - for (unsigned char i=0; i= _data.size()) { + _sum = _sum + value; + _data.push_back(value); + + // Else we substract the old value at the current poisiton and overwrite it + } else { + _sum = _sum + value - _data[_pointer]; + _data[_pointer] = value; } - } - virtual void add(double value) { - _sum = _sum + value - _data.at(_pointer); - _data.at(_pointer) = value; - _pointer = (_pointer + 1) % _size; + _pointer++; + } - virtual void reset() { - // Nothing to do + void reset() { + + // I assume series length to be the number of data points since last reset, + // so I zero-ed old data points from this point on + for (unsigned char i=_pointer; i<_data.size(); i++) { + _data[i] = 0; + } + + _pointer = 0; + } - virtual double result() { + double result() { + + // At this point we want to return the sum since last request + for (unsigned char i=_pointer; i<_data.size(); i++) { + _sum = _sum - _data[i]; + } + return _sum; + } protected: - unsigned char _size = 0; unsigned char _pointer = 0; double _sum = 0;