Browse Source

Improve memory footprint in filter classes

fastled
Xose Pérez 7 years ago
parent
commit
b2e0984214
3 changed files with 19 additions and 21 deletions
  1. +6
    -8
      code/espurna/filters/BaseFilter.h
  2. +10
    -10
      code/espurna/filters/MedianFilter.h
  3. +3
    -3
      code/espurna/filters/MovingAverageFilter.h

+ 6
- 8
code/espurna/filters/BaseFilter.h View File

@ -11,29 +11,27 @@ class BaseFilter {
public: public:
BaseFilter() { BaseFilter() {
_data = new std::vector<double>();
} }
~BaseFilter() { ~BaseFilter() {
if (_data) delete _data;
} }
virtual void add(double value) { virtual void add(double value) {
_data->push_back(value);
_data.push_back(value);
} }
virtual unsigned char count() { virtual unsigned char count() {
return _data->size();
return _data.size();
} }
virtual void reset() { virtual void reset() {
_data->clear();
_data.clear();
} }
virtual double max() { virtual double max() {
double max = 0; double max = 0;
for (unsigned char i = 1; i < _data->size(); i++) {
if (max < _data->at(i)) max = _data->at(i);
for (unsigned char i = 1; i < _data.size(); i++) {
if (max < _data.at(i)) max = _data.at(i);
} }
return max; return max;
} }
@ -44,6 +42,6 @@ class BaseFilter {
protected: protected:
std::vector<double> *_data;
std::vector<double> _data;
}; };

+ 10
- 10
code/espurna/filters/MedianFilter.h View File

@ -11,8 +11,8 @@ class MedianFilter : public BaseFilter {
public: public:
virtual void reset() { virtual void reset() {
double last = _data->empty() ? 0 : _data->back();
_data->clear();
double last = _data.empty() ? 0 : _data.back();
_data.clear();
add(last); add(last);
} }
@ -20,13 +20,13 @@ class MedianFilter : public BaseFilter {
double sum = 0; double sum = 0;
if (_data->size() > 2) {
if (_data.size() > 2) {
for (unsigned char i = 1; i <= _data->size() - 2; i++) {
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);
double previous = _data.at(i-1);
double current = _data.at(i);
double next = _data.at(i+1);
if (previous > current) std::swap(previous, current); if (previous > current) std::swap(previous, current);
if (current > next) std::swap(current, next); if (current > next) std::swap(current, next);
@ -36,11 +36,11 @@ class MedianFilter : public BaseFilter {
} }
sum /= (_data->size() - 2);
sum /= (_data.size() - 2);
} else if (_data->size() > 0) {
} else if (_data.size() > 0) {
sum = _data->front();
sum = _data.front();
} }


+ 3
- 3
code/espurna/filters/MovingAverageFilter.h View File

@ -14,13 +14,13 @@ class MovingAverageFilter : public BaseFilter {
MovingAverageFilter(unsigned char size) { MovingAverageFilter(unsigned char size) {
_size = size; _size = size;
for (unsigned char i=0; i<size; i++) { for (unsigned char i=0; i<size; i++) {
_data->push_back(0);
_data.push_back(0);
} }
} }
virtual void add(double value) { virtual void add(double value) {
_sum = _sum + value - _data->at(_pointer);
_data->at(_pointer) = value;
_sum = _sum + value - _data.at(_pointer);
_data.at(_pointer) = value;
_pointer = (_pointer + 1) % _size; _pointer = (_pointer + 1) % _size;
} }


Loading…
Cancel
Save