mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
median_filter.h
Go to the documentation of this file.
1#ifndef MEDIAN_FILTER
2#define MEDIAN_FILTER
3
10#include <boost/circular_buffer.hpp>
11#include <mutex>
12#include <cmath>
13#include <vector>
14#include <optional>
15
16namespace mrs_lib
17{
23 {
24 public:
33 MedianFilter(const size_t buffer_length, const double min_value = -std::numeric_limits<double>::infinity(),
34 const double max_value = std::numeric_limits<double>::infinity(), const double max_diff = std::numeric_limits<double>::infinity());
35
45
54 MedianFilter(const MedianFilter& other);
55
65
74 MedianFilter& operator=(const MedianFilter& other);
75
85
93 void add(const double value);
94
104 bool check(const double value);
105
117 bool addCheck(const double value);
118
124 void clear();
125
133 bool full() const;
134
143 double median() const;
144
150 bool initialized() const;
151
159 void setBufferLength(const size_t buffer_length);
160
168 void setMinValue(const double min_value);
169
177 void setMaxValue(const double max_value);
178
186 void setMaxDifference(const double max_diff);
187
188 private:
189 // for thread-safety
190 mutable std::recursive_mutex m_mtx;
191 // the input buffer
192 boost::circular_buffer<double> m_buffer;
193 // a helper buffer for sorting the input buffer
194 mutable std::vector<double> m_buffer_sorted;
195 // the last median value for lazy evaluation
196 mutable std::optional<double> m_median;
197
198 // parameters specified by the user
199 double m_min_valid;
200 double m_max_valid;
201 double m_max_diff;
202 };
203
204} // namespace mrs_lib
205
206#endif
Implementation of a median filter with a fixed-length buffer.
Definition median_filter.h:23
bool addCheck(const double value)
Add a new value to the buffer and check if it complies with the constraints.
Definition median_filter.cpp:87
bool check(const double value)
Check whether a value complies with the constraints.
Definition median_filter.cpp:77
void clear()
Clear the buffer of all values.
Definition median_filter.cpp:96
double median() const
Obtain the median.
Definition median_filter.cpp:113
void setMinValue(const double min_value)
Set a new minimal threshold for new values.
Definition median_filter.cpp:172
void setMaxValue(const double max_value)
Set a new maximal threshold for new values.
Definition median_filter.cpp:180
void setMaxDifference(const double max_diff)
Set a new maximal difference from median for new values.
Definition median_filter.cpp:188
bool initialized() const
Check whether the filter was initialized with a valid buffer length.
Definition median_filter.cpp:151
void add(const double value)
Add a new value to the buffer.
Definition median_filter.cpp:66
MedianFilter & operator=(const MedianFilter &other)
A convenience copy assignment operator.
Definition median_filter.cpp:32
bool full() const
Check whether the buffer is filled with values.
Definition median_filter.cpp:105
MedianFilter()
A convenience empty constructor that will construct an invalid filter.
Definition median_filter.cpp:14
void setBufferLength(const size_t buffer_length)
Set a new size of the buffer.
Definition median_filter.cpp:159
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24