Infuse-IoT SDK API 0.0.1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
filter.h
Go to the documentation of this file.
1
9
10#ifndef INFUSE_SDK_INCLUDE_INFUSE_MATH_FILTER_H_
11#define INFUSE_SDK_INCLUDE_INFUSE_MATH_FILTER_H_
12
13#include <math.h>
14
15#include <zephyr/kernel.h>
16#include <zephyr/sys/__assert.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
28
36static inline uint32_t iir_filter_alpha_init(float alpha)
37{
38 return (uint32_t)(alpha * (1ULL << 32));
39}
40
49 int16_t y_prev;
51 uint16_t alpha;
53 uint16_t error;
54};
55
66 uint32_t alpha, int16_t initial)
67{
68 __ASSERT_NO_MSG(alpha != 0);
69 filter->y_prev = initial;
70 filter->alpha = alpha >> 16;
71 filter->error = 0;
72}
73
83 int16_t x)
84{
85 uint32_t one = (1U << 16);
86 uint32_t alpha32 = filter->alpha;
87 uint32_t alpha32_inv = one - alpha32;
88 int32_t y_scaled = (alpha32 * x) + (alpha32_inv * filter->y_prev) + filter->error;
89 int16_t y = (y_scaled >> 16);
90
91 filter->error = y_scaled & UINT16_MAX;
92 filter->y_prev = y;
93 return y;
94}
95
104 int32_t y_prev;
106 uint32_t alpha;
108 uint32_t error;
109};
110
121 uint32_t alpha, int32_t initial)
122{
123 __ASSERT_NO_MSG(alpha != 0);
124 filter->y_prev = initial;
125 filter->alpha = alpha;
126 filter->error = 0;
127}
128
138 int32_t x)
139{
140 uint64_t one = (1ULL << 32);
141 uint64_t alpha64 = filter->alpha;
142 uint64_t alpha64_inv = one - alpha64;
143 int64_t y_scaled = (alpha64 * x) + (alpha64_inv * filter->y_prev) + filter->error;
144 int32_t y = (y_scaled >> 32);
145
146 filter->error = y_scaled & UINT32_MAX;
147 filter->y_prev = y;
148 return y;
149}
150
162
171 float alpha, float initial)
172{
173 __ASSERT_NO_MSG((alpha > 0.0f) && (alpha < 1.0f));
174 filter->y_prev = initial;
175 filter->alpha = alpha;
176 filter->inv_alpha = 1.0f - alpha;
177}
178
188 float x)
189{
190 float y = (filter->alpha * x) + (filter->inv_alpha * filter->y_prev);
191
192 filter->y_prev = y;
193 return y;
194}
195
199
200#ifdef __cplusplus
201}
202#endif
203
204#endif /* INFUSE_SDK_INCLUDE_INFUSE_MATH_FILTER_H_ */
static uint32_t iir_filter_alpha_init(float alpha)
Convert a filter alpha to a form compatible with IIR filters.
Definition filter.h:36
static void iir_filter_single_pole_s32_init(struct iir_filter_single_pole_s32 *filter, uint32_t alpha, int32_t initial)
Initialise the integer IIR single-pole filter.
Definition filter.h:120
static int16_t iir_filter_single_pole_s16_step(struct iir_filter_single_pole_s16 *filter, int16_t x)
Run the filter for one step.
Definition filter.h:82
static int32_t iir_filter_single_pole_s32_step(struct iir_filter_single_pole_s32 *filter, int32_t x)
Run the filter for one step.
Definition filter.h:137
static void iir_filter_single_pole_f32_init(struct iir_filter_single_pole_f32 *filter, float alpha, float initial)
Initialise the float IIR single-pole filter.
Definition filter.h:170
static float iir_filter_single_pole_f32_step(struct iir_filter_single_pole_f32 *filter, float x)
Run the filter for one step.
Definition filter.h:187
static void iir_filter_single_pole_s16_init(struct iir_filter_single_pole_s16 *filter, uint32_t alpha, int16_t initial)
Initialise the integer IIR single-pole filter.
Definition filter.h:65
Floating point IIR single-pole filter.
Definition filter.h:154
float inv_alpha
Precomputed (1 - alpha)
Definition filter.h:160
float alpha
Filter time constant.
Definition filter.h:158
float y_prev
Previous filter output.
Definition filter.h:156
Signed 16-bit IIR single-pole filter with fraction saving.
Definition filter.h:47
uint16_t error
Accumulated fractional error from the previous step.
Definition filter.h:53
uint16_t alpha
Filter time constant, 0-1 scaled by (1 << 16)
Definition filter.h:51
int16_t y_prev
Previous filter output.
Definition filter.h:49
Signed 32-bit IIR single-pole filter with fraction saving.
Definition filter.h:102
uint32_t error
Accumulated fractional error from the previous step.
Definition filter.h:108
int32_t y_prev
Previous filter output.
Definition filter.h:104
uint32_t alpha
Filter time constant, 0-1 scaled by (1 << 32)
Definition filter.h:106