mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
|
Implementation of a median filter with a fixed-length buffer. More...
#include <median_filter.h>
Public Member Functions | |
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()) | |
The main constructor. More... | |
MedianFilter () | |
A convenience empty constructor that will construct an invalid filter. More... | |
MedianFilter (const MedianFilter &other) | |
A convenience copy constructor. More... | |
MedianFilter (MedianFilter &&other) | |
A convenience move constructor. More... | |
MedianFilter & | operator= (const MedianFilter &other) |
A convenience copy assignment operator. More... | |
MedianFilter & | operator= (MedianFilter &&other) |
A convenience move assignment operator. More... | |
void | add (const double value) |
Add a new value to the buffer. More... | |
bool | check (const double value) |
Check whether a value complies with the constraints. More... | |
bool | addCheck (const double value) |
Add a new value to the buffer and check if it complies with the constraints. More... | |
void | clear () |
Clear the buffer of all values. More... | |
bool | full () const |
Check whether the buffer is filled with values. More... | |
double | median () const |
Obtain the median. More... | |
bool | initialized () const |
Check whether the filter was initialized with a valid buffer length. More... | |
void | setBufferLength (const size_t buffer_length) |
Set a new size of the buffer. More... | |
void | setMinValue (const double min_value) |
Set a new minimal threshold for new values. More... | |
void | setMaxValue (const double max_value) |
Set a new maximal threshold for new values. More... | |
void | setMaxDifference (const double max_diff) |
Set a new maximal difference from median for new values. More... | |
Implementation of a median filter with a fixed-length buffer.
mrs_lib::MedianFilter::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() |
||
) |
The main constructor.
buffer_length | the number of last values to be kept in the buffer. |
min_value | values below this threshold will be discarded (won't be added to the buffer). |
max_value | values above this threshold will be discarded. |
max_diff | values that differ from the current mean by more than this threshold will be discarded. |
mrs_lib::MedianFilter::MedianFilter | ( | ) |
A convenience empty constructor that will construct an invalid filter.
mrs_lib::MedianFilter::MedianFilter | ( | const MedianFilter & | other | ) |
A convenience copy constructor.
This constructor copies all data from the object that is being assigned from in a thread-safe manner.
other | the object to assign from. |
mrs_lib::MedianFilter::MedianFilter | ( | MedianFilter && | other | ) |
A convenience move constructor.
This constructor moves all data from the object that is being assigned from in a thread-safe manner, invalidating it.
other | the object to assign from. It will be invalid after this method returns. |
void mrs_lib::MedianFilter::add | ( | const double | value | ) |
Add a new value to the buffer.
value | the new value to be added to the buffer. |
bool mrs_lib::MedianFilter::addCheck | ( | const double | value | ) |
Add a new value to the buffer and check if it complies with the constraints.
The value is compliant if it's above the min_value
, below the max_value
and its (absolute) difference from the current mean is below max_diff
.
value | the new value to be added to the buffer and checked. |
bool mrs_lib::MedianFilter::check | ( | const double | value | ) |
Check whether a value complies with the constraints.
The value is compliant if it's above the min_value
, below the max_value
and its (absolute) difference from the current mean is below max_diff
.
value | the value to be checked. |
void mrs_lib::MedianFilter::clear | ( | ) |
Clear the buffer of all values.
Doesn't change the buffer's length set in the constructor or any other parameters, only clears all stored values.
bool mrs_lib::MedianFilter::full | ( | ) | const |
Check whether the buffer is filled with values.
If true, adding a new value will remove the oldest value in the buffer.
buffer_length
values. bool mrs_lib::MedianFilter::initialized | ( | ) | const |
Check whether the filter was initialized with a valid buffer length.
double mrs_lib::MedianFilter::median | ( | ) | const |
Obtain the median.
If an up-to-date median value is available, it's not recalculated. Otherwise, the new median value is calculated and then returned (lazy evaluation).
nan
if the input buffer is empty). MedianFilter & mrs_lib::MedianFilter::operator= | ( | const MedianFilter & | other | ) |
A convenience copy assignment operator.
This operator copies all data from the object that is being assigned from in a thread-safe manner.
other | the object to assign from. |
MedianFilter & mrs_lib::MedianFilter::operator= | ( | MedianFilter && | other | ) |
A convenience move assignment operator.
This operator moves all data from the object that is being assigned from in a thread-safe manner, invalidating it.
other | the object to assign from. It will be invalid after this method returns. |
void mrs_lib::MedianFilter::setBufferLength | ( | const size_t | buffer_length | ) |
Set a new size of the buffer.
buffer_length | the new size of the buffer. |
void mrs_lib::MedianFilter::setMaxDifference | ( | const double | max_diff | ) |
Set a new maximal difference from median for new values.
max_diff | the new maximal difference of new buffer elements from the current median. |
void mrs_lib::MedianFilter::setMaxValue | ( | const double | max_value | ) |
Set a new maximal threshold for new values.
max_value | the new maximal value of new buffer elements. |
void mrs_lib::MedianFilter::setMinValue | ( | const double | min_value | ) |
Set a new minimal threshold for new values.
min_value | the new minimal value of new buffer elements. |