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(), const double max_value = std::numeric_limits<double>::infinity(), const double max_diff = std::numeric_limits<double>::infinity());
34
44
53 MedianFilter(const MedianFilter& other);
54
64
73 MedianFilter& operator=(const MedianFilter& other);
74
84
92 void add(const double value);
93
103 bool check(const double value);
104
116 bool addCheck(const double value);
117
123 void clear();
124
132 bool full() const;
133
142 double median() const;
143
149 bool initialized() const;
150
158 void setBufferLength(const size_t buffer_length);
159
167 void setMinValue(const double min_value);
168
176 void setMaxValue(const double max_value);
177
185 void setMaxDifference(const double max_diff);
186
187 private:
188 // for thread-safety
189 mutable std::recursive_mutex m_mtx;
190 // the input buffer
191 boost::circular_buffer<double> m_buffer;
192 // a helper buffer for sorting the input buffer
193 mutable std::vector<double> m_buffer_sorted;
194 // the last median value for lazy evaluation
195 mutable std::optional<double> m_median;
196
197 // parameters specified by the user
198 double m_min_valid;
199 double m_max_valid;
200 double m_max_diff;
201 };
202
203} // namespace mrs_lib
204
205#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:79
bool check(const double value)
Check whether a value complies with the constraints.
Definition median_filter.cpp:70
void clear()
Clear the buffer of all values.
Definition median_filter.cpp:87
double median() const
Obtain the median.
Definition median_filter.cpp:102
void setMinValue(const double min_value)
Set a new minimal threshold for new values.
Definition median_filter.cpp:157
void setMaxValue(const double max_value)
Set a new maximal threshold for new values.
Definition median_filter.cpp:164
void setMaxDifference(const double max_diff)
Set a new maximal difference from median for new values.
Definition median_filter.cpp:171
bool initialized() const
Check whether the filter was initialized with a valid buffer length.
Definition median_filter.cpp:138
void add(const double value)
Add a new value to the buffer.
Definition median_filter.cpp:60
MedianFilter & operator=(const MedianFilter &other)
A convenience copy assignment operator.
Definition median_filter.cpp:28
bool full() const
Check whether the buffer is filled with values.
Definition median_filter.cpp:95
MedianFilter()
A convenience empty constructor that will construct an invalid filter.
Definition median_filter.cpp:13
void setBufferLength(const size_t buffer_length)
Set a new size of the buffer.
Definition median_filter.cpp:145
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24