#ifndef LIBA_LPF_H
#define LIBA_LPF_H
#include "math.h"
typedef struct a_lpf
{
a_float alpha; a_float output; #if defined(__cplusplus)
A_INLINE void gen(a_float fc, a_float ts)
{
alpha = ts / (A_FLOAT_1_TAU / fc + ts);
}
A_INLINE a_float operator()(a_float x)
{
output *= 1 - alpha;
output += x * alpha;
return output;
}
A_INLINE void zero() { output = 0; }
#endif
} a_lpf;
#if defined(__cplusplus)
namespace a
{
typedef struct a_lpf lpf;
}
#endif
#if defined(__cplusplus)
#define A_LPF_INIT(alpha) {a_float_c(alpha), 0}
#define A_LPF_INIT2(fc, ts) {A_LPF_GEN(fc, ts), 0}
#else
#define A_LPF_INIT(alpha) (a_lpf){a_float_c(alpha), 0}
#define A_LPF_INIT2(fc, ts) (a_lpf){A_LPF_GEN(fc, ts), 0}
#endif
#define A_LPF_GEN(fc, ts) (a_float_c(ts) / (A_FLOAT_1_TAU / a_float_c(fc) + a_float_c(ts)))
A_INTERN a_float a_lpf_gen(a_float fc, a_float ts)
{
return ts / (A_FLOAT_1_TAU / fc + ts);
}
A_INTERN void a_lpf_init(a_lpf *ctx, a_float alpha)
{
ctx->alpha = alpha;
ctx->output = 0;
}
A_INTERN a_float a_lpf_iter(a_lpf *ctx, a_float x)
{
ctx->output *= 1 - ctx->alpha;
ctx->output += x * ctx->alpha;
return ctx->output;
}
A_INTERN void a_lpf_zero(a_lpf *ctx) { ctx->output = 0; }
#endif