This commit is contained in:
2018-06-06 15:28:17 +02:00
commit aece7ca1b0
124 changed files with 39029 additions and 0 deletions

25
espurna/filters/BaseFilter.h Executable file
View File

@ -0,0 +1,25 @@
// -----------------------------------------------------------------------------
// Base Filter (other filters inherit from this)
// Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#if SENSOR_SUPPORT
#pragma once
class BaseFilter {
public:
virtual void add(double value);
virtual unsigned char count();
virtual void reset();
virtual double result();
virtual void resize(unsigned char size);
unsigned char size() { return _size; };
protected:
unsigned char _size;
};
#endif // SENSOR_SUPPORT

40
espurna/filters/MaxFilter.h Executable file
View File

@ -0,0 +1,40 @@
// -----------------------------------------------------------------------------
// Max Filter
// Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#if SENSOR_SUPPORT
#pragma once
#include "BaseFilter.h"
class MaxFilter : public BaseFilter {
public:
void add(double value) {
if (value > _max) _max = value;
}
unsigned char count() {
return 1;
}
void reset() {
_max = 0;
}
double result() {
return _max;
}
void resize(unsigned char size) {}
protected:
double _max = 0;
};
#endif // SENSOR_SUPPORT

92
espurna/filters/MedianFilter.h Executable file
View File

@ -0,0 +1,92 @@
// -----------------------------------------------------------------------------
// Median Filter
// Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#if SENSOR_SUPPORT
#pragma once
#include "BaseFilter.h"
class MedianFilter : public BaseFilter {
public:
~MedianFilter() {
if (_data) delete _data;
}
void add(double value) {
if (_pointer <= _size) {
_data[_pointer] = value;
_pointer++;
}
}
unsigned char count() {
return _pointer;
}
void reset() {
if (_pointer > 0) {
_data[0] = _data[_pointer-1];
_pointer = 1;
} else {
_pointer = 0;
}
}
double result() {
double sum = 0;
if (_pointer > 2) {
for (unsigned char i = 1; i <= _pointer - 2; i++) {
// 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);
if (previous > current) std::swap(previous, current);
sum += current;
}
sum /= (_pointer - 2);
} else if (_pointer > 0) {
sum = _data[0];
}
return sum;
}
void resize(unsigned char size) {
if (_size == size) return;
_size = size;
if (_data) delete _data;
_data = new double[_size+1];
for (unsigned char i=0; i<=_size; i++) _data[i] = 0;
_pointer = 0;
}
protected:
unsigned char _pointer = 0;
double * _data = NULL;
};
#endif // SENSOR_SUPPORT

View File

@ -0,0 +1,51 @@
// -----------------------------------------------------------------------------
// Moving Average Filter
// Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#if SENSOR_SUPPORT
#pragma once
#include <vector>
#include "BaseFilter.h"
class MovingAverageFilter : public BaseFilter {
public:
void add(double value) {
_sum = _sum + value - _data[_pointer];
_data[_pointer] = value;
_pointer = (_pointer + 1) % _size;
}
unsigned char count() {
return _pointer;
}
void reset() {}
double result() {
return _sum;
}
void resize(unsigned char size) {
if (_size == size) return;
_size = size;
if (_data) delete _data;
_data = new double[_size];
for (unsigned char i=0; i<_size; i++) _data[i] = 0;
_pointer = 0;
_sum = 0;
}
protected:
unsigned char _pointer = 0;
double _sum = 0;
double * _data = NULL;
};
#endif // SENSOR_SUPPORT