DerivativeFilter.h 1.9 KB

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