Skip to content

Commit a65821c

Browse files
committed
feat: low pass filter constructor and update implementation
1 parent 68bc753 commit a65821c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "low_pass_fir_filter.hpp"
2+
#include <stdexcept>
3+
#include <string>
4+
#include <iostream>
5+
6+
LowPassFIRFilter::LowPassFIRFilter() : LowPassFIRFilter(1) {}
7+
8+
LowPassFIRFilter::LowPassFIRFilter(unsigned int order)
9+
: buffer_is_empty(true), buffer(), buffer_index(0), order(order),
10+
coefficient(1.0f / (order + 1)), output(0) {
11+
// Valid input check
12+
if (order < 1 || order > MAX_LP_FIR_ORDER) {
13+
throw std::invalid_argument("order must be at least 1 and less than " +
14+
std::to_string(MAX_LP_FIR_ORDER));
15+
}
16+
}
17+
18+
unsigned int LowPassFIRFilter::update(unsigned int input) {
19+
if (this->buffer_is_empty) {
20+
// fill entire buffer with the first input
21+
for (int i = 0; i < this->order + 1; i++) {
22+
this->buffer[i] = input;
23+
}
24+
this->buffer_is_empty = false;
25+
return this->output = input;
26+
}
27+
28+
this->output = this->output + input * this->coefficient - this->buffer[this->buffer_index] * this->coefficient;
29+
this->buffer[this->buffer_index] = input;
30+
this->buffer_index = (this->buffer_index + 1) % (this->order + 1);
31+
32+
return this->output;
33+
}

0 commit comments

Comments
 (0)