123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #pragma once
- #include <inttypes.h>
- #include "FilterClass.h"
- #include "FilterWithBuffer.h"
- template <class T, uint8_t FILTER_SIZE>
- class ModeFilter : public FilterWithBuffer<T,FILTER_SIZE>
- {
- public:
- ModeFilter(uint8_t return_element);
-
- virtual T apply(T sample) override;
-
- virtual T get() const {
- return _output;
- }
- private:
-
- uint8_t _return_element;
- T _output;
- void isort(T sample, bool drop_high_sample);
- bool drop_high_sample;
- };
- typedef ModeFilter<int8_t,3> ModeFilterInt8_Size3;
- typedef ModeFilter<int8_t,4> ModeFilterInt8_Size4;
- typedef ModeFilter<int8_t,5> ModeFilterInt8_Size5;
- typedef ModeFilter<int8_t,6> ModeFilterInt8_Size6;
- typedef ModeFilter<int8_t,7> ModeFilterInt8_Size7;
- typedef ModeFilter<uint8_t,3> ModeFilterUInt8_Size3;
- typedef ModeFilter<uint8_t,4> ModeFilterUInt8_Size4;
- typedef ModeFilter<uint8_t,5> ModeFilterUInt8_Size5;
- typedef ModeFilter<uint8_t,6> ModeFilterUInt8_Size6;
- typedef ModeFilter<uint8_t,7> ModeFilterUInt8_Size7;
- typedef ModeFilter<int16_t,3> ModeFilterInt16_Size3;
- typedef ModeFilter<int16_t,4> ModeFilterInt16_Size4;
- typedef ModeFilter<int16_t,5> ModeFilterInt16_Size5;
- typedef ModeFilter<int16_t,6> ModeFilterInt16_Size6;
- typedef ModeFilter<int16_t,7> ModeFilterInt16_Size7;
- typedef ModeFilter<uint16_t,3> ModeFilterUInt16_Size3;
- typedef ModeFilter<uint16_t,4> ModeFilterUInt16_Size4;
- typedef ModeFilter<uint16_t,5> ModeFilterUInt16_Size5;
- typedef ModeFilter<uint16_t,6> ModeFilterUInt16_Size6;
- typedef ModeFilter<uint16_t,7> ModeFilterUInt16_Size7;
- typedef ModeFilter<float,3> ModeFilterFloat_Size3;
- typedef ModeFilter<float,4> ModeFilterFloat_Size4;
- typedef ModeFilter<float,5> ModeFilterFloat_Size5;
- typedef ModeFilter<float,6> ModeFilterFloat_Size6;
- typedef ModeFilter<float,7> ModeFilterFloat_Size7;
- template <class T, uint8_t FILTER_SIZE>
- ModeFilter<T,FILTER_SIZE>::ModeFilter(uint8_t return_element) :
- FilterWithBuffer<T,FILTER_SIZE>(),
- _return_element(return_element),
- drop_high_sample(true)
- {
-
- if( _return_element >= FILTER_SIZE )
- _return_element = FILTER_SIZE / 2;
- };
- template <class T, uint8_t FILTER_SIZE>
- T ModeFilter<T,FILTER_SIZE>:: apply(T sample)
- {
-
- isort(sample, drop_high_sample);
-
- drop_high_sample = !drop_high_sample;
-
- if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
-
- return _output = FilterWithBuffer<T,FILTER_SIZE>::samples[(FilterWithBuffer<T,FILTER_SIZE>::sample_index / 2)];
- }else{
-
- return _output = FilterWithBuffer<T,FILTER_SIZE>::samples[_return_element];
- }
- }
- template <class T, uint8_t FILTER_SIZE>
- void ModeFilter<T,FILTER_SIZE>:: isort(T new_sample, bool drop_high)
- {
- int8_t i;
-
-
- if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
- FilterWithBuffer<T,FILTER_SIZE>::sample_index++;
- drop_high = true;
- }
- if( drop_high ) {
-
- i = FilterWithBuffer<T,FILTER_SIZE>::sample_index-1;
-
- while(i > 0 && FilterWithBuffer<T,FILTER_SIZE>::samples[i-1] > new_sample) {
- FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i-1];
- i--;
- }
-
- FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
- }else{
-
- i = 0;
-
- while( FilterWithBuffer<T,FILTER_SIZE>::samples[i+1] < new_sample && i < FilterWithBuffer<T,FILTER_SIZE>::sample_index-1 ) {
- FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i+1];
- i++;
- }
-
- FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
- }
- }
|