Butter.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include <AP_HAL/AP_HAL.h>
  3. template <typename Coefficients>
  4. class Butter2
  5. {
  6. public:
  7. float filter(float input)
  8. {
  9. float newhist = input + Coefficients::A1*hist[1] + Coefficients::A2*hist[0];
  10. float ret = (newhist + 2*hist[1] + hist[0])/Coefficients::GAIN;
  11. hist[0] = hist[1]; hist[1] = newhist;
  12. return ret;
  13. }
  14. private:
  15. float hist[2];
  16. };
  17. struct butter100_025_coeffs
  18. {
  19. static constexpr float A1 = 1.9777864838f;
  20. static constexpr float A2 = -0.9780305085f;
  21. static constexpr float GAIN = 1.639178228e+04f;
  22. };
  23. typedef Butter2<butter100_025_coeffs> butter100hz0_25; //100hz sample, 0.25hz fcut
  24. typedef Butter2<butter100_025_coeffs> butter50hz0_125; //50hz sample, 0.125hz fcut
  25. typedef Butter2<butter100_025_coeffs> butter10hz0_025; //10hz sample, 0.025hz fcut
  26. struct butter100_05_coeffs
  27. {
  28. static constexpr float A1 = 1.9555782403f;
  29. static constexpr float A2 = -0.9565436765f;
  30. static constexpr float GAIN = 4.143204922e+03f;
  31. };
  32. typedef Butter2<butter100_05_coeffs> butter100hz0_5; //100hz sample, 0.5hz fcut
  33. typedef Butter2<butter100_05_coeffs> butter50hz0_25; //50hz sample, 0.25hz fcut
  34. typedef Butter2<butter100_05_coeffs> butter10hz0_05; //10hz sample, 0.05hz fcut
  35. struct butter100_1_coeffs
  36. {
  37. static constexpr float A1 = 1.9111970674f;
  38. static constexpr float A2 = -0.9149758348f;
  39. static constexpr float GAIN = 1.058546241e+03f;
  40. };
  41. typedef Butter2<butter100_1_coeffs> butter100hz1_0; //100hz sample, 1hz fcut
  42. typedef Butter2<butter100_1_coeffs> butter50hz0_5; //50hz sample, 0.5hz fcut
  43. typedef Butter2<butter100_1_coeffs> butter10hz0_1; //10hz sample, 0.1hz fcut
  44. struct butter100_1_5_coeffs
  45. {
  46. static constexpr float A1 = 1.8668922797f;
  47. static constexpr float A2 = -0.8752145483f;
  48. static constexpr float GAIN = 4.806381793e+02f;
  49. };
  50. typedef Butter2<butter100_1_5_coeffs> butter100hz1_5; //100hz sample, 1.5hz fcut
  51. typedef Butter2<butter100_1_5_coeffs> butter50hz0_75; //50hz sample, 0.75hz fcut
  52. typedef Butter2<butter100_1_5_coeffs> butter10hz0_15; //10hz sample, 0.15hz fcut
  53. struct butter100_2_coeffs
  54. {
  55. static constexpr float A1 = 1.8226949252f;
  56. static constexpr float A2 = -0.8371816513f;
  57. static constexpr float GAIN = 2.761148367e+02f;
  58. };
  59. typedef Butter2<butter100_2_coeffs> butter100hz2_0; //100hz sample, 2hz fcut
  60. typedef Butter2<butter100_2_coeffs> butter50hz1_0; //50hz sample, 1hz fcut
  61. typedef Butter2<butter100_2_coeffs> butter10hz0_2; //10hz sample, 0.2hz fcut
  62. struct butter100_3_coeffs
  63. {
  64. static constexpr float A1 = 1.7347257688f;
  65. static constexpr float A2 = -0.7660066009f;
  66. static constexpr float GAIN = 1.278738361e+02f;
  67. };
  68. typedef Butter2<butter100_3_coeffs> butter100hz3_0; //100hz sample, 3hz fcut
  69. typedef Butter2<butter100_3_coeffs> butter50hz1_5; //50hz sample, 1.5hz fcut
  70. typedef Butter2<butter100_3_coeffs> butter10hz0_3; //10hz sample, 0.3hz fcut
  71. struct butter100_4_coeffs
  72. {
  73. static constexpr float A1 = 1.6474599811f;
  74. static constexpr float A2 = -0.7008967812f;
  75. static constexpr float GAIN = 7.485478157e+01f;
  76. };
  77. typedef Butter2<butter100_4_coeffs> butter100hz4_0; //100hz sample, 4hz fcut
  78. typedef Butter2<butter100_4_coeffs> butter50hz2_0; //50hz sample, 2hz fcut
  79. typedef Butter2<butter100_4_coeffs> butter10hz0_4; //10hz sample, .4hz fcut
  80. struct butter100_8_coeffs
  81. {
  82. static constexpr float A1 = 1.3072850288f;
  83. static constexpr float A2 = -0.4918122372f;
  84. static constexpr float GAIN = 2.167702007e+01f;
  85. };
  86. typedef Butter2<butter100_8_coeffs> butter100hz8_0; //100hz sample, 8hz fcut
  87. typedef Butter2<butter100_8_coeffs> butter50hz4_0; //50hz sample, 4hz fcut
  88. typedef Butter2<butter100_8_coeffs> butter10hz0_8; //10hz sample, .8hz fcut
  89. struct butter50_8_coeffs
  90. {
  91. static constexpr float A1 = 0.6710290908f;
  92. static constexpr float A2 = -0.2523246263f;
  93. static constexpr float GAIN = 6.881181354e+00f;
  94. };
  95. typedef Butter2<butter50_8_coeffs> butter50hz8_0; //50hz sample, 8hz fcut
  96. typedef Butter2<butter50_8_coeffs> butter10hz1_6; //10hz sample, 1.6hz fcut