liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
lpf.h
Go to the documentation of this file.
1
24#ifndef LIBA_LPF_H
25#define LIBA_LPF_H
26
27#include "math.h"
28
38typedef struct a_lpf
39{
42#if defined(__cplusplus)
43 A_INLINE void gen(a_float fc, a_float ts)
44 {
45 alpha = ts / (A_FLOAT_1_TAU / fc + ts);
46 }
47 A_INLINE a_float operator()(a_float x)
48 {
49 output *= 1 - alpha;
50 output += x * alpha;
51 return output;
52 }
53 A_INLINE void zero() { output = 0; }
54#endif /* __cplusplus */
56#if defined(__cplusplus)
57namespace a
58{
59typedef 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)))
72
87{
88 return ts / (A_FLOAT_1_TAU / fc + ts);
89}
90
96A_INTERN void a_lpf_init(a_lpf *ctx, a_float alpha)
97{
98 ctx->alpha = alpha;
99 ctx->output = 0;
100}
101
112{
113 ctx->output *= 1 - ctx->alpha;
114 ctx->output += x * ctx->alpha;
115 return ctx->output;
116}
117
122A_INTERN void a_lpf_zero(a_lpf *ctx) { ctx->output = 0; }
123
126#endif /* a/lpf.h */
#define A_FLOAT_1_TAU
Definition math.h:249
#define a_float
Definition a.h:785
struct a_lpf a_lpf
instance structure for Low Pass Filter
double a_lpf_iter(a_lpf *ctx, double x)
calculate for Low Pass Filter
Definition lpf.h:111
void a_lpf_init(a_lpf *ctx, double alpha)
initialize for Low Pass Filter
Definition lpf.h:96
void a_lpf_zero(a_lpf *ctx)
zeroing for Low Pass Filter
Definition lpf.h:122
double a_lpf_gen(double fc, double ts)
generate for Low Pass Filter
Definition lpf.h:86
mathematical algorithm library
instance structure for Low Pass Filter
Definition lpf.h:39
double alpha
filter coefficient [0,1]
Definition lpf.h:40
double output
filter output
Definition lpf.h:41