ModeFilter.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. //
  14. /// @file ModeFilter.h
  15. /// @brief A class to apply a mode filter which is basically picking the median value from the last x samples
  16. /// the filter size (i.e buffer size) should always be an odd number
  17. #pragma once
  18. #include <inttypes.h>
  19. #include "FilterClass.h"
  20. #include "FilterWithBuffer.h"
  21. template <class T, uint8_t FILTER_SIZE>
  22. class ModeFilter : public FilterWithBuffer<T,FILTER_SIZE>
  23. {
  24. public:
  25. ModeFilter(uint8_t return_element);
  26. // apply - Add a new raw value to the filter, retrieve the filtered result
  27. virtual T apply(T sample) override;
  28. // get - get latest filtered value from filter (equal to the value returned by latest call to apply method)
  29. virtual T get() const {
  30. return _output;
  31. }
  32. private:
  33. // private methods
  34. uint8_t _return_element;
  35. T _output;
  36. void isort(T sample, bool drop_high_sample);
  37. bool drop_high_sample; // switch to determine whether to drop the highest or lowest sample when new value arrives
  38. };
  39. // Typedef for convenience
  40. typedef ModeFilter<int8_t,3> ModeFilterInt8_Size3;
  41. typedef ModeFilter<int8_t,4> ModeFilterInt8_Size4;
  42. typedef ModeFilter<int8_t,5> ModeFilterInt8_Size5;
  43. typedef ModeFilter<int8_t,6> ModeFilterInt8_Size6;
  44. typedef ModeFilter<int8_t,7> ModeFilterInt8_Size7;
  45. typedef ModeFilter<uint8_t,3> ModeFilterUInt8_Size3;
  46. typedef ModeFilter<uint8_t,4> ModeFilterUInt8_Size4;
  47. typedef ModeFilter<uint8_t,5> ModeFilterUInt8_Size5;
  48. typedef ModeFilter<uint8_t,6> ModeFilterUInt8_Size6;
  49. typedef ModeFilter<uint8_t,7> ModeFilterUInt8_Size7;
  50. typedef ModeFilter<int16_t,3> ModeFilterInt16_Size3;
  51. typedef ModeFilter<int16_t,4> ModeFilterInt16_Size4;
  52. typedef ModeFilter<int16_t,5> ModeFilterInt16_Size5;
  53. typedef ModeFilter<int16_t,6> ModeFilterInt16_Size6;
  54. typedef ModeFilter<int16_t,7> ModeFilterInt16_Size7;
  55. typedef ModeFilter<uint16_t,3> ModeFilterUInt16_Size3;
  56. typedef ModeFilter<uint16_t,4> ModeFilterUInt16_Size4;
  57. typedef ModeFilter<uint16_t,5> ModeFilterUInt16_Size5;
  58. typedef ModeFilter<uint16_t,6> ModeFilterUInt16_Size6;
  59. typedef ModeFilter<uint16_t,7> ModeFilterUInt16_Size7;
  60. typedef ModeFilter<float,3> ModeFilterFloat_Size3;
  61. typedef ModeFilter<float,4> ModeFilterFloat_Size4;
  62. typedef ModeFilter<float,5> ModeFilterFloat_Size5;
  63. typedef ModeFilter<float,6> ModeFilterFloat_Size6;
  64. typedef ModeFilter<float,7> ModeFilterFloat_Size7;
  65. // Constructor //////////////////////////////////////////////////////////////
  66. template <class T, uint8_t FILTER_SIZE>
  67. ModeFilter<T,FILTER_SIZE>::ModeFilter(uint8_t return_element) :
  68. FilterWithBuffer<T,FILTER_SIZE>(),
  69. _return_element(return_element),
  70. drop_high_sample(true)
  71. {
  72. // ensure we have a valid return_nth_element value. if not, revert to median
  73. if( _return_element >= FILTER_SIZE )
  74. _return_element = FILTER_SIZE / 2;
  75. };
  76. // Public Methods //////////////////////////////////////////////////////////////
  77. template <class T, uint8_t FILTER_SIZE>
  78. T ModeFilter<T,FILTER_SIZE>:: apply(T sample)
  79. {
  80. // insert the new items into the samples buffer
  81. isort(sample, drop_high_sample);
  82. // next time drop from the other end of the sample buffer
  83. drop_high_sample = !drop_high_sample;
  84. // return results
  85. if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
  86. // middle sample if buffer is not yet full
  87. return _output = FilterWithBuffer<T,FILTER_SIZE>::samples[(FilterWithBuffer<T,FILTER_SIZE>::sample_index / 2)];
  88. }else{
  89. // return element specified by user in constructor
  90. return _output = FilterWithBuffer<T,FILTER_SIZE>::samples[_return_element];
  91. }
  92. }
  93. //
  94. // insertion sort - takes a new sample and pushes it into the sample array
  95. // drops either the highest or lowest sample depending on the 'drop_high_sample' parameter
  96. //
  97. template <class T, uint8_t FILTER_SIZE>
  98. void ModeFilter<T,FILTER_SIZE>:: isort(T new_sample, bool drop_high)
  99. {
  100. int8_t i;
  101. // if the buffer isn't full simply increase the #items in the buffer (i.e. sample_index)
  102. // the rest is the same as dropping the high sample
  103. if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
  104. FilterWithBuffer<T,FILTER_SIZE>::sample_index++;
  105. drop_high = true;
  106. }
  107. if( drop_high ) { // drop highest sample from the buffer to make room for our new sample
  108. // start from top. Note: sample_index always points to the next open space so we start from sample_index-1
  109. i = FilterWithBuffer<T,FILTER_SIZE>::sample_index-1;
  110. // if the next element is higher than our new sample, push it up one position
  111. while(i > 0 && FilterWithBuffer<T,FILTER_SIZE>::samples[i-1] > new_sample) {
  112. FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i-1];
  113. i--;
  114. }
  115. // add our new sample to the buffer
  116. FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
  117. }else{ // drop lowest sample from the buffer to make room for our new sample
  118. // start from the bottom
  119. i = 0;
  120. // if the element is lower than our new sample, push it down one position
  121. while( FilterWithBuffer<T,FILTER_SIZE>::samples[i+1] < new_sample && i < FilterWithBuffer<T,FILTER_SIZE>::sample_index-1 ) {
  122. FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i+1];
  123. i++;
  124. }
  125. // add our new sample to the buffer
  126. FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
  127. }
  128. }