rename __call__ to mf in cython.mf
[liba.git] / include / a / hpf.h
blob51954e5373f4db57a687281b0508374c9d6e43bd
1 /*!
2 @file hpf.h
3 @brief High Pass Filter
4 @details
5 high frequencies are passed, low frequencies are attenuated.
6 \f[
7 RC\frac{\text{d}(V_{\mathrm i}-V_{\mathrm o})}{\text{dt}}=V_{\mathrm o}
8 \f]
9 \f[
10 RC\frac{[V_{\mathrm i}(n)-V_{\mathrm i}(n-1)]-[V_{\mathrm o}(n)-V_{\mathrm o}(n-1)]}{T_s}=V_{\mathrm o}(n)
11 \f]
12 \f[
13 V_{\mathrm o}(n)=\frac{RC}{RC+T_s}[V_{\mathrm o}(n-1)+V_{\mathrm i}(n)-V_{\mathrm i}(n-1)]
14 \f]
15 \f[
16 \alpha=\frac{RC}{RC+T_s}=\frac{1}{2\pi f_c T_s + 1}
17 \f]
18 \f[
19 V_{\mathrm o}(n)=\alpha[V_{\mathrm o}(n-1)+V_{\mathrm i}(n)-V_{\mathrm i}(n-1)]
20 \f]
21 https://en.wikipedia.org/wiki/High-pass_filter
24 #ifndef LIBA_HPF_H
25 #define LIBA_HPF_H
27 #include "math.h"
29 /*!
30 @ingroup A
31 @addtogroup A_HPF High Pass Filter
35 /*!
36 @brief instance structure for High Pass Filter
38 typedef struct a_hpf
40 a_float alpha; //!< filter coefficient [0,1]
41 a_float output; //!< filter output
42 a_float input; //!< filter input
43 #if defined(__cplusplus)
44 A_INLINE void gen(a_float fc, a_float ts)
46 alpha = 1 / (A_FLOAT_TAU * fc * ts + 1);
48 A_INLINE a_float operator()(a_float x)
50 output = alpha * (output + x - input);
51 return (void)(input = x), output;
53 A_INLINE void zero()
55 output = 0;
56 input = 0;
58 #endif /* __cplusplus */
59 } a_hpf;
60 #if defined(__cplusplus)
61 namespace a
63 typedef struct a_hpf hpf;
64 } /* namespace a */
65 #endif /* __cplusplus */
66 // clang-format off
67 #if defined(__cplusplus)
68 #define A_HPF_INIT(alpha) {a_float_c(alpha), 0, 0}
69 #define A_HPF_INIT2(fc, ts) {A_HPF_GEN(fc, ts), 0, 0}
70 #else /* !__cplusplus */
71 #define A_HPF_INIT(alpha) (a_hpf){a_float_c(alpha), 0, 0}
72 #define A_HPF_INIT2(fc, ts) (a_hpf){A_HPF_GEN(fc, ts), 0, 0}
73 #endif /* __cplusplus */
74 // clang-format on
75 #define A_HPF_GEN(fc, ts) (1 / (A_FLOAT_TAU * a_float_c(fc) * a_float_c(ts) + 1))
77 /*!
78 @brief generate for High Pass Filter
79 \f{cases}{
80 \alpha=\frac{RC}{RC+T_s},&\alpha\in[0,1]\\
81 RC=\frac{1}{2\pi f_c}.
82 \f}
83 \f[
84 \alpha=\frac{1}{2\pi f_c T_s+1}
85 \f]
86 @param[in] fc cut-off frequency unit(hz)
87 @param[in] ts sampling time unit(s)
88 @return filter coefficient [0,1]
90 A_INTERN a_float a_hpf_gen(a_float fc, a_float ts)
92 return 1 / (A_FLOAT_TAU * fc * ts + 1);
95 /*!
96 @brief initialize for High Pass Filter
97 @param[in,out] ctx points to an instance of High Pass Filter
98 @param[in] alpha filter coefficient [0,1]
100 A_INTERN void a_hpf_init(a_hpf *ctx, a_float alpha)
102 ctx->alpha = alpha;
103 ctx->output = 0;
104 ctx->input = 0;
108 @brief calculate for High Pass Filter
110 V_{\mathrm o}(n)=\alpha[V_{\mathrm o}(n-1)+V_{\mathrm i}(n)-V_{\mathrm i}(n-1)]
112 @param[in,out] ctx points to an instance of High Pass Filter
113 @param[in] x input value
114 @return output value
116 A_INTERN a_float a_hpf_iter(a_hpf *ctx, a_float x)
118 ctx->output = ctx->alpha * (ctx->output + x - ctx->input);
119 return (void)(ctx->input = x), ctx->output;
123 @brief zeroing for High Pass Filter
124 @param[in,out] ctx points to an instance of High Pass Filter
126 A_INTERN void a_hpf_zero(a_hpf *ctx)
128 ctx->output = 0;
129 ctx->input = 0;
132 /*! @} A_HPF */
134 #endif /* a/hpf.h */