move python/src/a/*.pxi to python/src/a.pyx
[liba.git] / include / a / hpf.h
blob518a1f414795562d1371ddc1f598814397ed40b9
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 "a.h"
28 #include "math.h"
30 /*!
31 @ingroup A
32 @addtogroup A_HPF High Pass Filter
36 /*!
37 @brief instance structure for High Pass Filter
39 typedef struct a_hpf
41 a_float alpha; //!< filter coefficient [0,1]
42 a_float output; //!< filter output
43 a_float input; //!< filter input
44 #if defined(__cplusplus)
45 A_INLINE void gen(a_float fc, a_float ts)
47 alpha = 1 / (A_FLOAT_TAU * fc * ts + 1);
49 A_INLINE a_float operator()(a_float x)
51 output = alpha * (output + x - input);
52 return (void)(input = x), output;
54 A_INLINE void zero()
56 output = 0;
57 input = 0;
59 #endif /* __cplusplus */
60 } a_hpf;
61 #if defined(__cplusplus)
62 namespace a
64 typedef struct a_hpf hpf;
65 } /* namespace a */
66 #endif /* __cplusplus */
67 // clang-format off
68 #if defined(__cplusplus)
69 #define A_HPF_INIT(alpha) {a_float_c(alpha), 0, 0}
70 #define A_HPF_INIT2(fc, ts) {A_HPF_GEN(fc, ts), 0, 0}
71 #else /* !__cplusplus */
72 #define A_HPF_INIT(alpha) (a_hpf){a_float_c(alpha), 0, 0}
73 #define A_HPF_INIT2(fc, ts) (a_hpf){A_HPF_GEN(fc, ts), 0, 0}
74 #endif /* __cplusplus */
75 // clang-format on
76 #define A_HPF_GEN(fc, ts) (1 / (A_FLOAT_TAU * a_float_c(fc) * a_float_c(ts) + 1))
78 /*!
79 @brief generate for High Pass Filter
80 \f{cases}{
81 \alpha=\frac{RC}{RC+T_s},&\alpha\in[0,1]\\
82 RC=\frac{1}{2\pi f_c}.
83 \f}
84 \f[
85 \alpha=\frac{1}{2\pi f_c T_s+1}
86 \f]
87 @param[in] fc cut-off frequency unit(hz)
88 @param[in] ts sampling time unit(s)
89 @return filter coefficient [0,1]
91 A_INTERN a_float a_hpf_gen(a_float fc, a_float ts)
93 return 1 / (A_FLOAT_TAU * fc * ts + 1);
96 /*!
97 @brief initialize for High Pass Filter
98 @param[in,out] ctx points to an instance of High Pass Filter
99 @param[in] alpha filter coefficient [0,1]
101 A_INTERN void a_hpf_init(a_hpf *ctx, a_float alpha)
103 ctx->alpha = alpha;
104 ctx->output = 0;
105 ctx->input = 0;
109 @brief calculate for High Pass Filter
111 V_{\mathrm o}(n)=\alpha[V_{\mathrm o}(n-1)+V_{\mathrm i}(n)-V_{\mathrm i}(n-1)]
113 @param[in,out] ctx points to an instance of High Pass Filter
114 @param[in] x input value
115 @return output value
117 A_INTERN a_float a_hpf_iter(a_hpf *ctx, a_float x)
119 ctx->output = ctx->alpha * (ctx->output + x - ctx->input);
120 return (void)(ctx->input = x), ctx->output;
124 @brief zeroing for High Pass Filter
125 @param[in,out] ctx points to an instance of High Pass Filter
127 A_INTERN void a_hpf_zero(a_hpf *ctx)
129 ctx->output = 0;
130 ctx->input = 0;
133 /*! @} A_HPF */
135 #endif /* a/hpf.h */