LowPassFilter2p.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #pragma once
  14. #include <AP_Math/AP_Math.h>
  15. #include <cmath>
  16. #include <inttypes.h>
  17. /// @file LowPassFilter2p.h
  18. /// @brief A class to implement a second order low pass filter
  19. /// @authors: Leonard Hall <LeonardTHall@gmail.com>, template implmentation: Daniel Frenzel <dgdanielf@gmail.com>
  20. template <class T>
  21. class DigitalBiquadFilter {
  22. public:
  23. struct biquad_params {
  24. float cutoff_freq;
  25. float sample_freq;
  26. float a1;
  27. float a2;
  28. float b0;
  29. float b1;
  30. float b2;
  31. };
  32. DigitalBiquadFilter();
  33. T apply(const T &sample, const struct biquad_params &params);
  34. void reset();
  35. static void compute_params(float sample_freq, float cutoff_freq, biquad_params &ret);
  36. private:
  37. T _delay_element_1;
  38. T _delay_element_2;
  39. };
  40. template <class T>
  41. class LowPassFilter2p {
  42. public:
  43. LowPassFilter2p();
  44. // constructor
  45. LowPassFilter2p(float sample_freq, float cutoff_freq);
  46. // change parameters
  47. void set_cutoff_frequency(float sample_freq, float cutoff_freq);
  48. // return the cutoff frequency
  49. float get_cutoff_freq(void) const;
  50. float get_sample_freq(void) const;
  51. T apply(const T &sample);
  52. void reset(void);
  53. protected:
  54. struct DigitalBiquadFilter<T>::biquad_params _params;
  55. private:
  56. DigitalBiquadFilter<T> _filter;
  57. };
  58. // Uncomment this, if you decide to remove the instantiations in the implementation file
  59. /*
  60. template <class T>
  61. LowPassFilter2p<T>::LowPassFilter2p() {
  62. memset(&_params, 0, sizeof(_params) );
  63. }
  64. // constructor
  65. template <class T>
  66. LowPassFilter2p<T>::LowPassFilter2p(float sample_freq, float cutoff_freq) {
  67. // set initial parameters
  68. set_cutoff_frequency(sample_freq, cutoff_freq);
  69. }
  70. */
  71. typedef LowPassFilter2p<int> LowPassFilter2pInt;
  72. typedef LowPassFilter2p<long> LowPassFilter2pLong;
  73. typedef LowPassFilter2p<float> LowPassFilter2pFloat;
  74. typedef LowPassFilter2p<Vector2f> LowPassFilter2pVector2f;
  75. typedef LowPassFilter2p<Vector3f> LowPassFilter2pVector3f;