-
Notifications
You must be signed in to change notification settings - Fork 115
Exponantial filter refactoring #493
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
base: master
Are you sure you want to change the base?
Changes from 18 commits
5ba7d18
5580c6a
6cfc93d
722232f
b684032
004c9a7
c8b1586
3a35890
e685aed
d66ab5c
55531b8
2e47eed
61cd168
35b7f36
1605c20
6da8ff8
f49fa6f
51afd55
4f72bd9
09b2b40
79f4b3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||||||||
| // Copyright (c) 2023, Stogl Robotics Consulting UG (haftungsbeschränkt) | ||||||||||||
| // | ||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||
| // You may obtain a copy of the License at | ||||||||||||
| // | ||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||
| // | ||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||
| // limitations under the License. | ||||||||||||
|
|
||||||||||||
| #ifndef CONTROL_TOOLBOX__EXPONENTIAL_FILTER_HPP_ | ||||||||||||
| #define CONTROL_TOOLBOX__EXPONENTIAL_FILTER_HPP_ | ||||||||||||
|
|
||||||||||||
| #include <cmath> | ||||||||||||
| #include <limits> | ||||||||||||
| #include <memory> | ||||||||||||
| #include <stdexcept> | ||||||||||||
| #include <string> | ||||||||||||
| #include <type_traits> | ||||||||||||
| #include <vector> | ||||||||||||
|
|
||||||||||||
| #include "control_toolbox/filter_traits.hpp" | ||||||||||||
|
|
||||||||||||
| namespace control_toolbox | ||||||||||||
| { | ||||||||||||
| template <typename T> | ||||||||||||
| class ExponentialFilter | ||||||||||||
| { | ||||||||||||
| public: | ||||||||||||
| // Default constructor | ||||||||||||
| ExponentialFilter(); | ||||||||||||
| explicit ExponentialFilter(double alpha) { set_params(alpha); } | ||||||||||||
|
|
||||||||||||
| ~ExponentialFilter(); | ||||||||||||
|
|
||||||||||||
| bool configure(); | ||||||||||||
|
|
||||||||||||
| bool update(const T & data_in, T & data_out); | ||||||||||||
|
|
||||||||||||
| bool is_configured() const { return configured_; } | ||||||||||||
|
|
||||||||||||
| void set_params(double alpha) { alpha_ = alpha; } | ||||||||||||
|
|
||||||||||||
| private: | ||||||||||||
| double alpha_; | ||||||||||||
|
|
||||||||||||
| // Define the storage type based on T | ||||||||||||
| using Traits = FilterTraits<T>; | ||||||||||||
| using StorageType = typename Traits::StorageType; | ||||||||||||
|
|
||||||||||||
| StorageType old_value_; | ||||||||||||
|
|
||||||||||||
| bool configured_ = false; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| template <typename T> | ||||||||||||
| ExponentialFilter<T>::ExponentialFilter() : alpha_(0.5) | ||||||||||||
| { | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| template <typename T> | ||||||||||||
| ExponentialFilter<T>::~ExponentialFilter() | ||||||||||||
| { | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| template <typename T> | ||||||||||||
| bool ExponentialFilter<T>::configure() | ||||||||||||
| { | ||||||||||||
| Traits::initialize(old_value_); | ||||||||||||
|
|
||||||||||||
| return configured_ = true; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| template <typename T> | ||||||||||||
| bool ExponentialFilter<T>::update(const T & data_in, T & data_out) | ||||||||||||
| { | ||||||||||||
| if (!configured_) | ||||||||||||
| { | ||||||||||||
| throw std::runtime_error("Filter is not configured"); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // First call: initialize filter state | ||||||||||||
| if (Traits::is_nan(old_value_) || Traits::is_empty(old_value_)) | ||||||||||||
| { | ||||||||||||
| if (!Traits::is_finite(data_in)) | ||||||||||||
| { | ||||||||||||
| return false; | ||||||||||||
| } | ||||||||||||
| Traits::assign(old_value_, data_in); | ||||||||||||
| } | ||||||||||||
| else | ||||||||||||
| { | ||||||||||||
| Traits::validate_input(data_in, old_value_, data_out); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Convert data_in to StorageType for arithmetic | ||||||||||||
| StorageType storage_in; | ||||||||||||
| Traits::assign(storage_in, data_in); | ||||||||||||
|
|
||||||||||||
| // Exponential filter update: y[n] = α * x[n] + (1-α) * y[n-1] | ||||||||||||
| old_value_ = storage_in * alpha_ + old_value_ * (1.0 - alpha_); | ||||||||||||
christophfroehlich marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but here we could use control_toolbox/control_toolbox/include/control_toolbox/filters.hpp Lines 43 to 47 in d12a587
we would increase code coverage for free then ;) |
||||||||||||
|
|
||||||||||||
| Traits::assign(data_out, old_value_); | ||||||||||||
| Traits::add_metadata(data_out, data_in); | ||||||||||||
|
|
||||||||||||
| return true; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| } // namespace control_toolbox | ||||||||||||
|
|
||||||||||||
| #endif // CONTROL_TOOLBOX__EXPONENTIAL_FILTER_HPP_ | ||||||||||||
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.
This is not needed any more here, right?