NotchFilter.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /*
  15. notch filter with settable sample rate, center frequency, bandwidth and attenuation
  16. Design by Leonard Hall
  17. */
  18. #include <AP_Math/AP_Math.h>
  19. #include <cmath>
  20. #include <inttypes.h>
  21. #include <AP_Param/AP_Param.h>
  22. template <class T>
  23. class NotchFilter {
  24. public:
  25. // set parameters
  26. void init(float sample_freq_hz, float center_freq_hz, float bandwidth_hz, float attenuation_dB);
  27. void init_with_A_and_Q(float sample_freq_hz, float center_freq_hz, float A, float Q);
  28. T apply(const T &sample);
  29. void reset();
  30. // calculate attenuation and quality from provided center frequency and bandwidth
  31. static void calculate_A_and_Q(float center_freq_hz, float bandwidth_hz, float attenuation_dB, float& A, float& Q);
  32. private:
  33. bool initialised;
  34. float b0, b1, b2, a1, a2, a0_inv;
  35. T ntchsig, ntchsig1, ntchsig2, signal2, signal1;
  36. };
  37. /*
  38. notch filter enable and filter parameters
  39. */
  40. class NotchFilterParams {
  41. public:
  42. NotchFilterParams(void);
  43. static const struct AP_Param::GroupInfo var_info[];
  44. float center_freq_hz(void) const { return _center_freq_hz; }
  45. float bandwidth_hz(void) const { return _bandwidth_hz; }
  46. float attenuation_dB(void) const { return _attenuation_dB; }
  47. uint8_t enabled(void) const { return _enable; }
  48. protected:
  49. AP_Int8 _enable;
  50. AP_Float _center_freq_hz;
  51. AP_Float _bandwidth_hz;
  52. AP_Float _attenuation_dB;
  53. };
  54. typedef NotchFilter<float> NotchFilterFloat;
  55. typedef NotchFilter<Vector3f> NotchFilterVector3f;