Simplified low pass filter

This commit is contained in:
Martin Kompf 2019-02-10 17:00:06 +01:00
parent 8485b87bbf
commit 964011e832
1 changed files with 9 additions and 15 deletions

View File

@ -33,10 +33,9 @@ int sensorValue = 0; // difference sensorValueOn - sensorValueOff
float filteredValue; // filtered sensor value float filteredValue; // filtered sensor value
// definitions for low pass filter // definitions for low pass filter
#define FILTER_LEN 6 float filterAlpha = 0.1f;
float buffer[FILTER_LEN]; float filterOut = 0;
int startBuf = 0; boolean filterLoad = true;
int lenBuf = 0;
// command line // command line
#define MAX_CMD_LEN 80 #define MAX_CMD_LEN 80
@ -175,20 +174,15 @@ void doCommand() {
* Low pass filter to eleminate spikes * Low pass filter to eleminate spikes
*/ */
float lowpass(int value) { float lowpass(int value) {
if (lenBuf < FILTER_LEN) { if (filterLoad) {
lenBuf++; filterOut = value;
filterLoad = false;
} }
buffer[startBuf++] = (float) value; filterOut = filterAlpha * value + (1.f - filterAlpha) * filterOut;
if (startBuf >= FILTER_LEN) { return filterOut;
startBuf = 0;
}
float sum = 0;
for (int i = 0; i < lenBuf; ++i) {
sum += buffer[i];
}
return sum / lenBuf;
} }
/** /**
* Setup. * Setup.
*/ */