Browse Source

Refactor filters

fastled
Xose Pérez 6 years ago
parent
commit
a5ea1eb027
4 changed files with 44 additions and 18 deletions
  1. +1
    -1
      code/espurna/filters/BaseFilter.h
  2. +1
    -1
      code/espurna/filters/MaxFilter.h
  3. +11
    -3
      code/espurna/filters/MedianFilter.h
  4. +31
    -13
      code/espurna/filters/MovingAverageFilter.h

+ 1
- 1
code/espurna/filters/BaseFilter.h View File

@ -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;
}


+ 1
- 1
code/espurna/filters/MaxFilter.h View File

@ -12,7 +12,7 @@ class MaxFilter : public BaseFilter {
public:
virtual double result() {
double result() {
return max();
}


+ 11
- 3
code/espurna/filters/MedianFilter.h View File

@ -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);


+ 31
- 13
code/espurna/filters/MovingAverageFilter.h View File

@ -12,30 +12,48 @@ class MovingAverageFilter : public BaseFilter {
public:
MovingAverageFilter(unsigned char size) {
_size = size;
for (unsigned char i=0; i<size; i++) {
_data.push_back(0);
void add(double value) {
// If we are at the end of the vector we add a new element
if (_pointer >= _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;


Loading…
Cancel
Save