FilterWithBuffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 FilterWithBuffer.h
  15. /// @brief A filter with a buffer.
  16. /// This is implemented separately to the base Filter class to get around
  17. /// restrictions caused by the use of templates which makes different sizes essentially
  18. /// completely different classes
  19. #pragma once
  20. #include "FilterClass.h"
  21. template <class T, uint8_t FILTER_SIZE>
  22. class FilterWithBuffer : public Filter<T>
  23. {
  24. public:
  25. // constructor
  26. FilterWithBuffer();
  27. // apply - Add a new raw value to the filter, retrieve the filtered result
  28. virtual T apply(T sample) override;
  29. // reset - clear the filter
  30. virtual void reset() override;
  31. // get filter size
  32. uint8_t get_filter_size() const {
  33. return FILTER_SIZE;
  34. };
  35. T get_sample(uint8_t i) const {
  36. return samples[i];
  37. }
  38. protected:
  39. T samples[FILTER_SIZE]; // buffer of samples
  40. uint8_t sample_index; // pointer to the next empty slot in the buffer
  41. };
  42. // Typedef for convenience
  43. typedef FilterWithBuffer<int16_t,2> FilterWithBufferInt16_Size2;
  44. typedef FilterWithBuffer<int16_t,3> FilterWithBufferInt16_Size3;
  45. typedef FilterWithBuffer<int16_t,4> FilterWithBufferInt16_Size4;
  46. typedef FilterWithBuffer<int16_t,5> FilterWithBufferInt16_Size5;
  47. typedef FilterWithBuffer<int16_t,6> FilterWithBufferInt16_Size6;
  48. typedef FilterWithBuffer<int16_t,7> FilterWithBufferInt16_Size7;
  49. typedef FilterWithBuffer<uint16_t,2> FilterWithBufferUInt16_Size2;
  50. typedef FilterWithBuffer<uint16_t,3> FilterWithBufferUInt16_Size3;
  51. typedef FilterWithBuffer<uint16_t,4> FilterWithBufferUInt16_Size4;
  52. typedef FilterWithBuffer<uint16_t,5> FilterWithBufferUInt16_Size5;
  53. typedef FilterWithBuffer<uint16_t,6> FilterWithBufferUInt16_Size6;
  54. typedef FilterWithBuffer<uint16_t,7> FilterWithBufferUInt16_Size7;
  55. typedef FilterWithBuffer<int32_t,2> FilterWithBufferInt32_Size2;
  56. typedef FilterWithBuffer<int32_t,3> FilterWithBufferInt32_Size3;
  57. typedef FilterWithBuffer<int32_t,4> FilterWithBufferInt32_Size4;
  58. typedef FilterWithBuffer<int32_t,5> FilterWithBufferInt32_Size5;
  59. typedef FilterWithBuffer<int32_t,6> FilterWithBufferInt32_Size6;
  60. typedef FilterWithBuffer<int32_t,7> FilterWithBufferInt32_Size7;
  61. typedef FilterWithBuffer<uint32_t,2> FilterWithBufferUInt32_Size2;
  62. typedef FilterWithBuffer<uint32_t,3> FilterWithBufferUInt32_Size3;
  63. typedef FilterWithBuffer<uint32_t,4> FilterWithBufferUInt32_Size4;
  64. typedef FilterWithBuffer<uint32_t,5> FilterWithBufferUInt32_Size5;
  65. typedef FilterWithBuffer<uint32_t,6> FilterWithBufferUInt32_Size6;
  66. typedef FilterWithBuffer<uint32_t,7> FilterWithBufferUInt32_Size7;
  67. // Constructor
  68. template <class T, uint8_t FILTER_SIZE>
  69. FilterWithBuffer<T,FILTER_SIZE>::FilterWithBuffer() :
  70. sample_index(0)
  71. {
  72. // clear sample buffer
  73. reset();
  74. }
  75. // reset - clear all samples from the buffer
  76. template <class T, uint8_t FILTER_SIZE>
  77. void FilterWithBuffer<T,FILTER_SIZE>::reset()
  78. {
  79. // clear samples buffer
  80. for( int8_t i=0; i<FILTER_SIZE; i++ ) {
  81. samples[i] = 0;
  82. }
  83. // reset index back to beginning of the array
  84. sample_index = 0;
  85. }
  86. // apply - take in a new raw sample, and return the filtered results
  87. template <class T, uint8_t FILTER_SIZE>
  88. T FilterWithBuffer<T,FILTER_SIZE>:: apply(T sample)
  89. {
  90. // add sample to array
  91. samples[sample_index++] = sample;
  92. // wrap index if necessary
  93. if( sample_index >= FILTER_SIZE )
  94. sample_index = 0;
  95. // base class doesn't know what filtering to do so we just return the raw sample
  96. return sample;
  97. }