mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
mrs_lib::MedianFilter Class Reference

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...
 
MedianFilteroperator= (const MedianFilter &other)
 A convenience copy assignment operator. More...
 
MedianFilteroperator= (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...
 

Detailed Description

Implementation of a median filter with a fixed-length buffer.

Constructor & Destructor Documentation

◆ MedianFilter() [1/4]

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.

Parameters
buffer_lengththe number of last values to be kept in the buffer.
min_valuevalues below this threshold will be discarded (won't be added to the buffer).
max_valuevalues above this threshold will be discarded.
max_diffvalues that differ from the current mean by more than this threshold will be discarded.

◆ MedianFilter() [2/4]

mrs_lib::MedianFilter::MedianFilter ( )

A convenience empty constructor that will construct an invalid filter.

Warning
This constructor will construct an unusable filter with a zero-length buffer. To actually initialize this object, use the main constructor. You can use the initialized() method to check whether the object is valid.

◆ MedianFilter() [3/4]

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.

Parameters
otherthe object to assign from.

◆ MedianFilter() [4/4]

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.

Parameters
otherthe object to assign from. It will be invalid after this method returns.

Member Function Documentation

◆ add()

void mrs_lib::MedianFilter::add ( const double  value)

Add a new value to the buffer.

Note
The median value will not be updated until the median() method is called (lazy evaluation).
Parameters
valuethe new value to be added to the buffer.

◆ addCheck()

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.

Note
The median value will not be updated until the median() method is called (lazy evaluation).
Parameters
valuethe new value to be added to the buffer and checked.
Returns
true if the value is compliant, false otherwise.

◆ check()

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.

Parameters
valuethe value to be checked.
Returns
true if the value is compliant, false otherwise.

◆ clear()

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.

◆ full()

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.

Returns
true if the buffer contains buffer_length values.

◆ initialized()

bool mrs_lib::MedianFilter::initialized ( ) const

Check whether the filter was initialized with a valid buffer length.

Returns
true if the buffer length is larger than zero.

◆ median()

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).

Returns
the current median value (returns nan if the input buffer is empty).

◆ operator=() [1/2]

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.

Parameters
otherthe object to assign from.
Returns
a reference to the object being assigned to.

◆ operator=() [2/2]

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.

Parameters
otherthe object to assign from. It will be invalid after this method returns.
Returns
a reference to the object being assigned to.

◆ setBufferLength()

void mrs_lib::MedianFilter::setBufferLength ( const size_t  buffer_length)

Set a new size of the buffer.

Note
The median value may change.
Parameters
buffer_lengththe new size of the buffer.

◆ setMaxDifference()

void mrs_lib::MedianFilter::setMaxDifference ( const double  max_diff)

Set a new maximal difference from median for new values.

Note
The current buffer is not changed - the change only applies to new values.
Parameters
max_diffthe new maximal difference of new buffer elements from the current median.

◆ setMaxValue()

void mrs_lib::MedianFilter::setMaxValue ( const double  max_value)

Set a new maximal threshold for new values.

Note
The current buffer is not changed - the change only applies to new values.
Parameters
max_valuethe new maximal value of new buffer elements.

◆ setMinValue()

void mrs_lib::MedianFilter::setMinValue ( const double  min_value)

Set a new minimal threshold for new values.

Note
The current buffer is not changed - the change only applies to new values.
Parameters
min_valuethe new minimal value of new buffer elements.

The documentation for this class was generated from the following files: