rename __call__ to mf in cython.mf
[liba.git] / include / a / lpf.h
blob3266e94ece448dfc0e8a5cc684d5876866973fb9
1 /*!
2 @file lpf.h
3 @brief Low Pass Filter
4 @details
5 low frequencies are passed, high frequencies are attenuated.
6 \f[
7 RC\frac{\text{d}V_{\mathrm o}}{\text{dt}}+V_{\mathrm o}=V_{\mathrm i}
8 \f]
9 \f[
10 RC\frac{V_{\mathrm o}(n)-V_{\mathrm o}(n-1)}{T_s}+V_{\mathrm o}(n)=V_{\mathrm i}(n)
11 \f]
12 \f[
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)
14 \f]
15 \f[
16 V_{\mathrm o}(n)=(1-\alpha)V_{\mathrm o}(n-1)+\alpha V_{\mathrm i}(n)
17 \f]
18 \f[
19 \alpha=\frac{T_s}{RC+T_s}=\frac{T_s}{{\frac{1}{2\pi f_c}}+T_s}
20 \f]
21 https://en.wikipedia.org/wiki/Low-pass_filter
24 #ifndef LIBA_LPF_H
25 #define LIBA_LPF_H
27 #include "math.h"
29 /*!
30 @ingroup A
31 @addtogroup A_LPF Low Pass Filter
35 /*!
36 @brief instance structure for Low Pass Filter
38 typedef struct a_lpf
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)
49 output *= 1 - alpha;
50 output += x * alpha;
51 return output;
53 A_INLINE void zero() { output = 0; }
54 #endif /* __cplusplus */
55 } a_lpf;
56 #if defined(__cplusplus)
57 namespace a
59 typedef struct a_lpf lpf;
60 } /* namespace a */
61 #endif /* __cplusplus */
62 // clang-format off
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 */
70 // clang-format on
71 #define A_LPF_GEN(fc, ts) (a_float_c(ts) / (A_FLOAT_1_TAU / a_float_c(fc) + a_float_c(ts)))
73 /*!
74 @brief generate for Low Pass Filter
75 \f{cases}{
76 \alpha=\frac{T_s}{RC+T_s},&\alpha\in[0,1]\\
77 RC=\frac{1}{2\pi f_c}.
78 \f}
79 \f[
80 \alpha=\frac{T_s}{\frac{1}{2\pi f_c}+T_s}
81 \f]
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);
91 /*!
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)
98 ctx->alpha = alpha;
99 ctx->output = 0;
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
109 @return output 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;
115 return ctx->output;
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; }
124 /*! @} A_LPF */
126 #endif /* a/lpf.h */