flt_butter.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /******************************************************************************
  2. * Copyright 2020 The Firmament Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *****************************************************************************/
  16. #ifndef BUTTER_H__
  17. #define BUTTER_H__
  18. #ifdef __cplusplus
  19. extern "C"
  20. {
  21. #endif
  22. /* 30Hz cut-off frequency, 1000Hz sampling frequency */
  23. #define BUTTER3_B_1000S_30C \
  24. { \
  25. 0.0007, 0.0021, 0.0021, 0.0007 \
  26. }
  27. #define BUTTER3_A_1000S_30C \
  28. { \
  29. 1.0, -2.6236, 2.3147, -0.6855 \
  30. }
  31. typedef struct
  32. {
  33. float A[4];
  34. float B[4];
  35. float X[4];
  36. float Y[4];
  37. } LpfButter3;
  38. typedef struct
  39. {
  40. // 历史采样数据
  41. float _delay_element1;
  42. float _delay_element2;
  43. // 权重系数
  44. float _a1;
  45. float _a2;
  46. float _b0;
  47. float _b1;
  48. float _b2;
  49. // 截至频率
  50. float _cutoff_freq;
  51. // 采样频率
  52. float _sample_freq;
  53. } LpfButter2;
  54. int butter3_filter_init(LpfButter3 *butter, const float b[4], const float a[4]);
  55. int butter3_filter_reset(float sample, LpfButter3 *butter);
  56. float butter3_filter_apply(float in, LpfButter3 *butter);
  57. int butter2_filter_init(LpfButter2 *butter, float sample_freq,
  58. float cuttoff_freq);
  59. float butter2_filter_reset(LpfButter2 *lp, float sample);
  60. float butter2_filter_apply(LpfButter2 *lp, float sample);
  61. #ifdef __cplusplus
  62. }
  63. #endif
  64. #endif