liba 0.1.15
An algorithm library based on C/C++
 
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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_real fc, a_real ts)
44 {
45 alpha = ts / (A_REAL_1_TAU / fc + ts);
46 }
47 A_INLINE a_real operator()(a_real 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_real_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_real_c(ts) / (A_REAL_1_TAU / a_real_c(fc) + a_real_c(ts)))
67
81A_INTERN a_real a_lpf_gen(a_real fc, a_real ts)
82{
83 return ts / (A_REAL_1_TAU / fc + ts);
84}
85
91A_INTERN void a_lpf_init(a_lpf *ctx, a_real alpha)
92{
93 ctx->alpha = alpha;
94 ctx->output = 0;
95}
96
106A_INTERN a_real a_lpf_iter(a_lpf *ctx, a_real x)
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 */
struct a_lpf a_lpf
instance structure for Low Pass Filter
a_real a_lpf_gen(a_real fc, a_real ts)
generate for Low Pass Filter
Definition lpf.h:81
a_real a_lpf_iter(a_lpf *ctx, a_real x)
compute for Low Pass Filter
Definition lpf.h:106
void a_lpf_zero(a_lpf *ctx)
zeroing for Low Pass Filter
Definition lpf.h:117
void a_lpf_init(a_lpf *ctx, a_real alpha)
initialize for Low Pass Filter
Definition lpf.h:91
#define A_REAL_1_TAU
Definition math.h:271
double a_real
compiler built-in floating-point number type
Definition a.h:1006
mathematical algorithm library
instance structure for Low Pass Filter
Definition lpf.h:39
a_real output
Definition lpf.h:41
a_real alpha
Definition lpf.h:40