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 */
63 #if defined(__cplusplus)
64 #define A_LPF_INIT(alpha) {a_float_c(alpha), 0}
65 #define A_LPF_INIT2(fc, ts) {A_LPF_GEN(fc, ts), 0}
66 #else /* !__cplusplus */
67 #define A_LPF_INIT(alpha) (a_lpf){a_float_c(alpha), 0}
68 #define A_LPF_INIT2(fc, ts) (a_lpf){A_LPF_GEN(fc, ts), 0}
69 #endif /* __cplusplus */
71 #define A_LPF_GEN(fc, ts) (a_float_c(ts) / (A_FLOAT_1_TAU / a_float_c(fc) + a_float_c(ts)))
74 @brief generate for Low Pass Filter
76 \alpha=\frac{T_s}{RC+T_s},&\alpha\in[0,1]\\
77 RC=\frac{1}{2\pi f_c}.
80 \alpha=\frac{T_s}{\frac{1}{2\pi f_c}+T_s}
82 @param[in] fc cut-off frequency unit(hz)
83 @param[in] ts sampling time unit(s)
84 @return filter coefficient [0,1]
86 A_INTERN a_float
a_lpf_gen(a_float fc
, a_float ts
)
88 return ts
/ (A_FLOAT_1_TAU
/ fc
+ ts
);
92 @brief initialize for Low Pass Filter
93 @param[in,out] ctx points to an instance of Low Pass Filter
94 @param[in] alpha filter coefficient [0,1]
96 A_INTERN
void a_lpf_init(a_lpf
*ctx
, a_float alpha
)
103 @brief calculate for Low Pass Filter
105 V_{\mathrm o}(n)=(1-\alpha)V_{\mathrm o}(n-1)+\alpha V_{\mathrm i}(n)
107 @param[in,out] ctx points to an instance of Low Pass Filter
108 @param[in] x input value
111 A_INTERN a_float
a_lpf_iter(a_lpf
*ctx
, a_float x
)
113 ctx
->output
*= 1 - ctx
->alpha
;
114 ctx
->output
+= x
* ctx
->alpha
;
119 @brief zeroing for Low Pass Filter
120 @param[in,out] ctx points to an instance of Low Pass Filter
122 A_INTERN
void a_lpf_zero(a_lpf
*ctx
) { ctx
->output
= 0; }