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 Derivative.h 18 : /// @brief A class to implement a derivative (slope) filter 19 : /// See http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/ 20 : #pragma once 21 : 22 : #include "FilterClass.h" 23 : #include "FilterWithBuffer.h" 24 : 25 : // 1st parameter <T> is the type of data being filtered. 26 : // 2nd parameter <FILTER_SIZE> is the number of elements in the filter 27 : template <class T, uint8_t FILTER_SIZE> 28 : class DerivativeFilter : public FilterWithBuffer<T,FILTER_SIZE> 29 : { 30 : public: 31 : // constructor 32 3387 : DerivativeFilter() : FilterWithBuffer<T,FILTER_SIZE>() { 33 3387 : }; 34 : 35 : // update - Add a new raw value to the filter, but don't recalculate 36 : void update(T sample, uint32_t timestamp); 37 : 38 : // return the derivative value 39 : float slope(void); 40 : 41 : // reset - clear the filter 42 : virtual void reset() override; 43 : 44 : private: 45 : bool _new_data; 46 : float _last_slope; 47 : 48 : // microsecond timestamps for samples. This is needed 49 : // to cope with non-uniform time spacing of the data 50 : uint32_t _timestamps[FILTER_SIZE]; 51 : }; 52 : 53 : typedef DerivativeFilter<float,5> DerivativeFilterFloat_Size5; 54 : typedef DerivativeFilter<float,7> DerivativeFilterFloat_Size7; 55 : typedef DerivativeFilter<float,9> DerivativeFilterFloat_Size9;