-
Notifications
You must be signed in to change notification settings - Fork 0
Template low pass filter #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
common_libraries/low_pass_fir_filter/low_pass_fir_filter.cpp
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
common_libraries/low_pass_fir_filter/low_pass_fir_filter.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #ifndef FIR_FILTER_HPP | ||
| #define FIR_FILTER_HPP | ||
|
|
||
| /* | ||
| This class is used to apply a low pass FIR filter to a stream of inputs with a specific order. This filter is simply taking S=[ current_input, previous_input_1, previous_input_2, ... , previous_input_{order} ], and returning the average. | ||
|
|
||
| The buffer is initially filled with the first input to simplfy the algorithm (this removes the need for taking the average of less than "order" + 1 number of inputs). | ||
|
|
||
| The buffer is of size "order" + 1 since we need to store the current input, and also remember the oldest input (to remove from the total output in a subsequent update). | ||
| */ | ||
|
|
||
| template <int order=1> | ||
| class LowPassFIRFilter { | ||
| private: | ||
| bool buffer_is_empty = true; | ||
| unsigned int buffer[order+1]; | ||
| unsigned int buffer_index = 0; | ||
| float coefficient = 1.0f / (order + 1); | ||
| float output = 0; | ||
|
|
||
| public: | ||
| LowPassFIRFilter() { | ||
| // Valid input check | ||
| if (order < 1) { | ||
| throw std::invalid_argument("order must be at least 1"); | ||
| } | ||
| }; | ||
|
|
||
| unsigned int getOrder() const { | ||
| return order; | ||
| } | ||
|
|
||
| // Update filter with new input and return filtered output | ||
| unsigned int update(unsigned int input) { | ||
| if (this->buffer_is_empty) { | ||
| // fill entire buffer with the first input | ||
| for (int i = 0; i < order + 1; i++) { | ||
| this->buffer[i] = input; | ||
| } | ||
| this->buffer_is_empty = false; | ||
| return this->output = input; | ||
| } | ||
|
|
||
| // `input` is the new data point that is begin added. `buffer[buffer_index]` is the old data point that must be removed. | ||
| this->output = this->output + input * this->coefficient - this->buffer[this->buffer_index] * this->coefficient; | ||
| this->buffer[this->buffer_index] = input; | ||
|
|
||
| // the index must wrap around once it reaches the right most side of the buffer. Note that buffer size is order + 1. | ||
| this->buffer_index = (this->buffer_index + 1) % (order + 1); | ||
|
|
||
| return this->output; | ||
| }; | ||
| }; | ||
|
|
||
| #endif |
34 changes: 0 additions & 34 deletions
34
common_libraries/low_pass_fir_filter/low_pass_fir_filter.hpp
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good use of catch2 macros