a_linalg_{Tnn,Tmn}->{T1,T2}
[liba.git] / include / a / hpf.h
blob70b0ec57fe2caee4602ed8be17dbec58524dc71c
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 liba
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 #define A_HPF_1(alpha) {a_float_c(alpha), 0, 0}
68 #define A_HPF_2(fc, ts) {A_HPF_GEN(fc, ts), 0, 0}
69 /* clang-format on */
70 #define A_HPF_GEN(fc, ts) (1 / (A_FLOAT_TAU * a_float_c(fc) * a_float_c(ts) + 1))
72 /*!
73 @brief generate for High Pass Filter
74 \f{cases}{
75 \alpha=\frac{RC}{RC+T_s},&\alpha\in[0,1]\\
76 RC=\frac{1}{2\pi f_c}.
77 \f}
78 \f[
79 \alpha=\frac{1}{2\pi f_c T_s+1}
80 \f]
81 @param[in] fc cut-off frequency unit(hz)
82 @param[in] ts sampling time unit(s)
83 @return filter coefficient [0,1]
85 A_INTERN a_float a_hpf_gen(a_float fc, a_float ts)
87 return 1 / (A_FLOAT_TAU * fc * ts + 1);
90 /*!
91 @brief initialize for High Pass Filter
92 @param[in,out] ctx points to an instance of High Pass Filter
93 @param[in] alpha filter coefficient [0,1]
95 A_INTERN void a_hpf_init(a_hpf *ctx, a_float alpha)
97 ctx->alpha = alpha;
98 ctx->output = 0;
99 ctx->input = 0;
103 @brief calculate for High Pass Filter
105 V_{\mathrm o}(n)=\alpha[V_{\mathrm o}(n-1)+V_{\mathrm i}(n)-V_{\mathrm i}(n-1)]
107 @param[in,out] ctx points to an instance of High Pass Filter
108 @param[in] x input value
109 @return output value
111 A_INTERN a_float a_hpf_iter(a_hpf *ctx, a_float x)
113 ctx->output = ctx->alpha * (ctx->output + x - ctx->input);
114 return (void)(ctx->input = x), ctx->output;
118 @brief zeroing for High Pass Filter
119 @param[in,out] ctx points to an instance of High Pass Filter
121 A_INTERN void a_hpf_zero(a_hpf *ctx)
123 ctx->output = 0;
124 ctx->input = 0;
127 /*! @} a_hpf */
129 #endif /* a/hpf.h */