mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
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 
16 namespace 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 
43  MedianFilter();
44 
53  MedianFilter(const MedianFilter& other);
54 
63  MedianFilter(MedianFilter&& other);
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
mrs_lib::MedianFilter::setBufferLength
void setBufferLength(const size_t buffer_length)
Set a new size of the buffer.
Definition: median_filter.cpp:166
mrs_lib::MedianFilter::full
bool full() const
Check whether the buffer is filled with values.
Definition: median_filter.cpp:112
mrs_lib::MedianFilter::addCheck
bool addCheck(const double value)
Add a new value to the buffer and check if it complies with the constraints.
Definition: median_filter.cpp:94
mrs_lib::MedianFilter::add
void add(const double value)
Add a new value to the buffer.
Definition: median_filter.cpp:73
mrs_lib::MedianFilter::setMaxValue
void setMaxValue(const double max_value)
Set a new maximal threshold for new values.
Definition: median_filter.cpp:187
mrs_lib
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition: attitude_converter.h:29
mrs_lib::MedianFilter::setMaxDifference
void setMaxDifference(const double max_diff)
Set a new maximal difference from median for new values.
Definition: median_filter.cpp:195
mrs_lib::MedianFilter::check
bool check(const double value)
Check whether a value complies with the constraints.
Definition: median_filter.cpp:84
mrs_lib::MedianFilter::initialized
bool initialized() const
Check whether the filter was initialized with a valid buffer length.
Definition: median_filter.cpp:158
mrs_lib::MedianFilter::median
double median() const
Obtain the median.
Definition: median_filter.cpp:120
mrs_lib::MedianFilter::operator=
MedianFilter & operator=(const MedianFilter &other)
A convenience copy assignment operator.
Definition: median_filter.cpp:39
mrs_lib::MedianFilter
Implementation of a median filter with a fixed-length buffer.
Definition: median_filter.h:22
mrs_lib::MedianFilter::MedianFilter
MedianFilter()
A convenience empty constructor that will construct an invalid filter.
Definition: median_filter.cpp:17
mrs_lib::MedianFilter::setMinValue
void setMinValue(const double min_value)
Set a new minimal threshold for new values.
Definition: median_filter.cpp:179
mrs_lib::MedianFilter::clear
void clear()
Clear the buffer of all values.
Definition: median_filter.cpp:103