uncrustify libraries/Filter/AverageFilter.h
This commit is contained in:
parent
10acadef62
commit
dbba304af8
@ -16,24 +16,25 @@
|
|||||||
#include <Filter.h>
|
#include <Filter.h>
|
||||||
#include <FilterWithBuffer.h>
|
#include <FilterWithBuffer.h>
|
||||||
|
|
||||||
// 1st parameter <T> is the type of data being filtered.
|
// 1st parameter <T> is the type of data being filtered.
|
||||||
// 2nd parameter <U> is a larger data type used during summation to prevent overflows
|
// 2nd parameter <U> is a larger data type used during summation to prevent overflows
|
||||||
// 3rd parameter <FILTER_SIZE> is the number of elements in the filter
|
// 3rd parameter <FILTER_SIZE> is the number of elements in the filter
|
||||||
template <class T, class U, uint8_t FILTER_SIZE>
|
template <class T, class U, uint8_t FILTER_SIZE>
|
||||||
class AverageFilter : public FilterWithBuffer<T,FILTER_SIZE>
|
class AverageFilter : public FilterWithBuffer<T,FILTER_SIZE>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructor
|
// constructor
|
||||||
AverageFilter() : FilterWithBuffer<T,FILTER_SIZE>(), _num_samples(0) {};
|
AverageFilter() : FilterWithBuffer<T,FILTER_SIZE>(), _num_samples(0) {
|
||||||
|
};
|
||||||
|
|
||||||
// apply - Add a new raw value to the filter, retrieve the filtered result
|
// apply - Add a new raw value to the filter, retrieve the filtered result
|
||||||
virtual T apply(T sample);
|
virtual T apply(T sample);
|
||||||
|
|
||||||
// reset - clear the filter
|
// reset - clear the filter
|
||||||
virtual void reset();
|
virtual void reset();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _num_samples; // the number of samples in the filter, maxes out at size of the filter
|
uint8_t _num_samples; // the number of samples in the filter, maxes out at size of the filter
|
||||||
};
|
};
|
||||||
|
|
||||||
// Typedef for convenience (1st argument is the data type, 2nd is a larger datatype to handle overflows, 3rd is buffer size)
|
// Typedef for convenience (1st argument is the data type, 2nd is a larger datatype to handle overflows, 3rd is buffer size)
|
||||||
@ -69,34 +70,34 @@ typedef AverageFilter<float,float,5> AverageFilterFloat_Size5;
|
|||||||
// Public Methods //////////////////////////////////////////////////////////////
|
// Public Methods //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template <class T, class U, uint8_t FILTER_SIZE>
|
template <class T, class U, uint8_t FILTER_SIZE>
|
||||||
T AverageFilter<T,U,FILTER_SIZE>::apply(T sample)
|
T AverageFilter<T,U,FILTER_SIZE>:: apply(T sample)
|
||||||
{
|
{
|
||||||
U result = 0;
|
U result = 0;
|
||||||
|
|
||||||
// call parent's apply function to get the sample into the array
|
// call parent's apply function to get the sample into the array
|
||||||
FilterWithBuffer<T,FILTER_SIZE>::apply(sample);
|
FilterWithBuffer<T,FILTER_SIZE>::apply(sample);
|
||||||
|
|
||||||
// increment the number of samples so far
|
// increment the number of samples so far
|
||||||
_num_samples++;
|
_num_samples++;
|
||||||
if( _num_samples > FILTER_SIZE || _num_samples == 0 )
|
if( _num_samples > FILTER_SIZE || _num_samples == 0 )
|
||||||
_num_samples = FILTER_SIZE;
|
_num_samples = FILTER_SIZE;
|
||||||
|
|
||||||
// get sum of all values - there is a risk of overflow here that we ignore
|
// get sum of all values - there is a risk of overflow here that we ignore
|
||||||
for(uint8_t i=0; i<FILTER_SIZE; i++)
|
for(uint8_t i=0; i<FILTER_SIZE; i++)
|
||||||
result += FilterWithBuffer<T,FILTER_SIZE>::samples[i];
|
result += FilterWithBuffer<T,FILTER_SIZE>::samples[i];
|
||||||
|
|
||||||
return (T)(result / _num_samples);
|
return (T)(result / _num_samples);
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset - clear all samples
|
// reset - clear all samples
|
||||||
template <class T, class U, uint8_t FILTER_SIZE>
|
template <class T, class U, uint8_t FILTER_SIZE>
|
||||||
void AverageFilter<T,U,FILTER_SIZE>::reset()
|
void AverageFilter<T,U,FILTER_SIZE>:: reset()
|
||||||
{
|
{
|
||||||
// call parent's apply function to get the sample into the array
|
// call parent's apply function to get the sample into the array
|
||||||
FilterWithBuffer<T,FILTER_SIZE>::reset();
|
FilterWithBuffer<T,FILTER_SIZE>::reset();
|
||||||
|
|
||||||
// clear our variable
|
// clear our variable
|
||||||
_num_samples = 0;
|
_num_samples = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user