5 low frequencies are passed, high frequencies are attenuated.
7 RC\frac{\text{d}V_{\mathrm o}}{\text{dt}}+V_{\mathrm o}=V_{\mathrm i}
10 RC\frac{V_{\mathrm o}(n)-V_{\mathrm o}(n-1)}{T_s}+V_{\mathrm o}(n)=V_{\mathrm i}(n)
13 V_{\mathrm o}(n)=\frac{RC}{RC+T_s}V_{\mathrm o}(n-1)+\frac{T_s}{RC+T_s}V_{\mathrm i}(n)
16 V_{\mathrm o}(n)=(1-\alpha)V_{\mathrm o}(n-1)+\alpha V_{\mathrm i}(n)
19 \alpha=\frac{T_s}{RC+T_s}=\frac{T_s}{{\frac{1}{2\pi f_c}}+T_s}
21 https://en.wikipedia.org/wiki/Low-pass_filter
31 @addtogroup a_lpf Low Pass Filter
36 @brief instance structure for Low Pass Filter
40 a_float alpha
; /*!< filter coefficient [0,1] */
41 a_float output
; /*!< filter output */
42 #if defined(__cplusplus)
43 A_INLINE
void gen(a_float fc
, a_float ts
)
45 alpha
= ts
/ (A_FLOAT_1_TAU
/ fc
+ ts
);
47 A_INLINE a_float
operator()(a_float x
)
53 A_INLINE
void zero() { output
= 0; }
54 #endif /* __cplusplus */
56 #if defined(__cplusplus)
59 typedef struct a_lpf lpf
;
61 #endif /* __cplusplus */
62 /* clang-format off */
63 #define A_LPF_1(alpha) {a_float_c(alpha), 0}
64 #define A_LPF_2(fc, ts) {A_LPF_GEN(fc, ts), 0}
66 #define A_LPF_GEN(fc, ts) (a_float_c(ts) / (A_FLOAT_1_TAU / a_float_c(fc) + a_float_c(ts)))
69 @brief generate for Low Pass Filter
71 \alpha=\frac{T_s}{RC+T_s},&\alpha\in[0,1]\\
72 RC=\frac{1}{2\pi f_c}.
75 \alpha=\frac{T_s}{\frac{1}{2\pi f_c}+T_s}
77 @param[in] fc cut-off frequency unit(hz)
78 @param[in] ts sampling time unit(s)
79 @return filter coefficient [0,1]
81 A_INTERN a_float
a_lpf_gen(a_float fc
, a_float ts
)
83 return ts
/ (A_FLOAT_1_TAU
/ fc
+ ts
);
87 @brief initialize for Low Pass Filter
88 @param[in,out] ctx points to an instance of Low Pass Filter
89 @param[in] alpha filter coefficient [0,1]
91 A_INTERN
void a_lpf_init(a_lpf
*ctx
, a_float alpha
)
98 @brief calculate for Low Pass Filter
100 V_{\mathrm o}(n)=(1-\alpha)V_{\mathrm o}(n-1)+\alpha V_{\mathrm i}(n)
102 @param[in,out] ctx points to an instance of Low Pass Filter
103 @param[in] x input value
106 A_INTERN a_float
a_lpf_iter(a_lpf
*ctx
, a_float x
)
108 ctx
->output
*= 1 - ctx
->alpha
;
109 ctx
->output
+= x
* ctx
->alpha
;
114 @brief zeroing for Low Pass Filter
115 @param[in,out] ctx points to an instance of Low Pass Filter
117 A_INTERN
void a_lpf_zero(a_lpf
*ctx
) { ctx
->output
= 0; }