HarmonicNotchFilter.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <AP_Param/AP_Param.h>
  17. #include "NotchFilter.h"
  18. /*
  19. a filter that manages a set of notch filters targetted at a fundamental center frequency
  20. and multiples of that fundamental frequency
  21. */
  22. template <class T>
  23. class HarmonicNotchFilter {
  24. public:
  25. ~HarmonicNotchFilter();
  26. // allocate a bank of notch filters for this harmonic notch filter
  27. void allocate_filters(uint8_t harmonics);
  28. // initialize the underlying filters using the provided filter parameters
  29. void init(float sample_freq_hz, float center_freq_hz, float bandwidth_hz, float attenuation_dB);
  30. // update the underlying filters' center frequencies using center_freq_hz as the fundamental
  31. void update(float center_freq_hz);
  32. // apply a sample to each of the underlying filters in turn
  33. T apply(const T &sample);
  34. // reset each of the underlying filters
  35. void reset();
  36. private:
  37. // underlying bank of notch filters
  38. NotchFilter<T>* _filters;
  39. // sample frequency for each filter
  40. float _sample_freq_hz;
  41. // attenuation for each filter
  42. float _A;
  43. // quality factor of each filter
  44. float _Q;
  45. // a bitmask of the harmonics to use
  46. uint8_t _harmonics;
  47. // number of allocated filters
  48. uint8_t _num_filters;
  49. // number of enabled filters
  50. uint8_t _num_enabled_filters;
  51. bool _initialised;
  52. };
  53. /*
  54. harmonic notch filter configuration parameters
  55. */
  56. class HarmonicNotchFilterParams : public NotchFilterParams {
  57. public:
  58. HarmonicNotchFilterParams(void);
  59. // set the fundamental center frequency of the harmonic notch
  60. void set_center_freq_hz(float center_freq) { _center_freq_hz.set(center_freq); }
  61. // harmonics enabled on the harmonic notch
  62. uint8_t harmonics(void) const { return _harmonics; }
  63. // reference value of the harmonic notch
  64. float reference(void) const { return _reference; }
  65. static const struct AP_Param::GroupInfo var_info[];
  66. private:
  67. // notch harmonics
  68. AP_Int8 _harmonics;
  69. // notch reference value
  70. AP_Float _reference;
  71. };
  72. typedef HarmonicNotchFilter<Vector3f> HarmonicNotchFilterVector3f;