LCOV - code coverage report
Current view: top level - Filter - FilterWithBuffer.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 18 18 100.0 %
Date: 2026-09-09 11:02:08 Functions: 26 36 72.2 %

          Line data    Source code
       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             : 
       7             :    This program is distributed in the hope that it will be useful,
       8             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
       9             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      10             :    GNU General Public License for more details.
      11             : 
      12             :    You should have received a copy of the GNU General Public License
      13             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      14             :  */
      15             : 
      16             : //
      17             : /// @file       FilterWithBuffer.h
      18             : /// @brief      A filter with a buffer.
      19             : ///         This is implemented separately to the base Filter class to get around
      20             : ///         restrictions caused by the use of templates which makes different sizes essentially
      21             : ///         completely different classes
      22             : #pragma once
      23             : 
      24             : #include "FilterClass.h"
      25             : 
      26             : template <class T, uint8_t FILTER_SIZE>
      27             : class FilterWithBuffer : public Filter<T>
      28             : {
      29             : public:
      30             :     // constructor
      31             :     FilterWithBuffer();
      32             : 
      33             :     // apply - Add a new raw value to the filter, retrieve the filtered result
      34             :     virtual T apply(T sample) override;
      35             : 
      36             :     // reset - clear the filter
      37             :     virtual void reset() override;
      38             : 
      39             :     // get filter size
      40          12 :     uint8_t get_filter_size() const {
      41          12 :         return FILTER_SIZE;
      42             :     };
      43             : 
      44     6795494 :     T get_sample(uint8_t i) const {
      45     6795494 :         return samples[i];
      46             :     }
      47             : 
      48             : protected:
      49             :     T               samples[FILTER_SIZE];       // buffer of samples
      50             :     uint8_t         sample_index;               // pointer to the next empty slot in the buffer
      51             : };
      52             : 
      53             : // Typedef for convenience
      54             : typedef FilterWithBuffer<int16_t,2> FilterWithBufferInt16_Size2;
      55             : typedef FilterWithBuffer<int16_t,3> FilterWithBufferInt16_Size3;
      56             : typedef FilterWithBuffer<int16_t,4> FilterWithBufferInt16_Size4;
      57             : typedef FilterWithBuffer<int16_t,5> FilterWithBufferInt16_Size5;
      58             : typedef FilterWithBuffer<int16_t,6> FilterWithBufferInt16_Size6;
      59             : typedef FilterWithBuffer<int16_t,7> FilterWithBufferInt16_Size7;
      60             : typedef FilterWithBuffer<uint16_t,2> FilterWithBufferUInt16_Size2;
      61             : typedef FilterWithBuffer<uint16_t,3> FilterWithBufferUInt16_Size3;
      62             : typedef FilterWithBuffer<uint16_t,4> FilterWithBufferUInt16_Size4;
      63             : typedef FilterWithBuffer<uint16_t,5> FilterWithBufferUInt16_Size5;
      64             : typedef FilterWithBuffer<uint16_t,6> FilterWithBufferUInt16_Size6;
      65             : typedef FilterWithBuffer<uint16_t,7> FilterWithBufferUInt16_Size7;
      66             : 
      67             : typedef FilterWithBuffer<int32_t,2> FilterWithBufferInt32_Size2;
      68             : typedef FilterWithBuffer<int32_t,3> FilterWithBufferInt32_Size3;
      69             : typedef FilterWithBuffer<int32_t,4> FilterWithBufferInt32_Size4;
      70             : typedef FilterWithBuffer<int32_t,5> FilterWithBufferInt32_Size5;
      71             : typedef FilterWithBuffer<int32_t,6> FilterWithBufferInt32_Size6;
      72             : typedef FilterWithBuffer<int32_t,7> FilterWithBufferInt32_Size7;
      73             : typedef FilterWithBuffer<uint32_t,2> FilterWithBufferUInt32_Size2;
      74             : typedef FilterWithBuffer<uint32_t,3> FilterWithBufferUInt32_Size3;
      75             : typedef FilterWithBuffer<uint32_t,4> FilterWithBufferUInt32_Size4;
      76             : typedef FilterWithBuffer<uint32_t,5> FilterWithBufferUInt32_Size5;
      77             : typedef FilterWithBuffer<uint32_t,6> FilterWithBufferUInt32_Size6;
      78             : typedef FilterWithBuffer<uint32_t,7> FilterWithBufferUInt32_Size7;
      79             : 
      80             : // Constructor
      81             : template <class T, uint8_t FILTER_SIZE>
      82       62077 : FilterWithBuffer<T,FILTER_SIZE>::FilterWithBuffer() :
      83       62077 :     sample_index(0)
      84             : {
      85             :     // clear sample buffer
      86       62077 :     reset();
      87       62077 : }
      88             : 
      89             : // reset - clear all samples from the buffer
      90             : template <class T, uint8_t FILTER_SIZE>
      91       64110 : void FilterWithBuffer<T,FILTER_SIZE>::reset()
      92             : {
      93             :     // clear samples buffer
      94      280812 :     for( int8_t i=0; i<FILTER_SIZE; i++ ) {
      95      216702 :         samples[i] = 0;
      96             :     }
      97             : 
      98             :     // reset index back to beginning of the array
      99       64110 :     sample_index = 0;
     100       64110 : }
     101             : 
     102             : // apply - take in a new raw sample, and return the filtered results
     103             : template <class T, uint8_t FILTER_SIZE>
     104    24735182 : T FilterWithBuffer<T,FILTER_SIZE>::        apply(T sample)
     105             : {
     106             :     // add sample to array
     107    24735182 :     samples[sample_index++] = sample;
     108             : 
     109             :     // wrap index if necessary
     110    24735182 :     if( sample_index >= FILTER_SIZE )
     111     4066169 :         sample_index = 0;
     112             : 
     113             :     // base class doesn't know what filtering to do so we just return the raw sample
     114    24735182 :     return sample;
     115             : }

Generated by: LCOV version 1.14