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
23
24#ifndef LIBA_LPF_H
25#define LIBA_LPF_H
26
27#include "math.h"
28
34
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#define A_LPF_1(alpha) {a_float_c(alpha), 0}
64#define A_LPF_2(fc, ts) {A_LPF_GEN(fc, ts), 0}
65/* clang-format on */
66#define A_LPF_GEN(fc, ts) (a_float_c(ts) / (A_FLOAT_1_TAU / a_float_c(fc) + a_float_c(ts)))
67
82{
83 return ts / (A_FLOAT_1_TAU / fc + ts);
84}
85
91A_INTERN void a_lpf_init(a_lpf *ctx, a_float alpha)
92{
93 ctx->alpha = alpha;
94 ctx->output = 0;
95}
96
107{
108 ctx->output *= 1 - ctx->alpha;
109 ctx->output += x * ctx->alpha;
110 return ctx->output;
111}
112
117A_INTERN void a_lpf_zero(a_lpf *ctx) { ctx->output = 0; }
118
120
121#endif /* a/lpf.h */
double a_float
compiler built-in floating-point number type
Definition a.h:1003
#define A_FLOAT_1_TAU
Definition math.h:249
struct a_lpf a_lpf
instance structure for Low Pass Filter
a_float a_lpf_gen(a_float fc, a_float ts)
generate for Low Pass Filter
Definition lpf.h:81
void a_lpf_init(a_lpf *ctx, a_float alpha)
initialize for Low Pass Filter
Definition lpf.h:91
void a_lpf_zero(a_lpf *ctx)
zeroing for Low Pass Filter
Definition lpf.h:117
a_float a_lpf_iter(a_lpf *ctx, a_float x)
calculate for Low Pass Filter
Definition lpf.h:106
mathematical algorithm library
instance structure for Low Pass Filter
Definition lpf.h:39
a_float output
Definition lpf.h:41
a_float alpha
Definition lpf.h:40