IO Variants: 5
Variant: 1
bool32.rising Clock input
int32 Input
int32 Moving average output
combo length
#ifdef FIFO_EXP
#undef FIFO_EXP
#endif
#ifdef FIFO_LEN
#undef FIFO_LEN
#endif
#ifdef FIFO_MASK
#undef FIFO_MASK
#endif
#define FIFO_EXP attr_length
#define FIFO_LEN (1 << FIFO_EXP)
#define FIFO_MASK (FIFO_LEN - 1)
typedef struct _AveragerContext {
int32_t fifo[FIFO_LEN];
int32_t fifo_i;
int32_t acc;
} AveragerContext;
AveragerContext avg;
__attribute__((always_inline)) __STATIC_INLINE int32_t
moving_average(AveragerContext *avg, int32_t new_value) {
avg->fifo_i = (avg->fifo_i + 1) & FIFO_MASK;
avg->acc -= avg->fifo[avg->fifo_i];
avg->acc += new_value;
avg->fifo[avg->fifo_i] = new_value;
return avg->acc >> FIFO_EXP;
}
int32_t last_out;
int32_t old_inlet_clock;
for (avg.fifo_i = 0; avg.fifo_i < FIFO_LEN; avg.fifo_i++) {
avg.fifo[avg.fifo_i] = 0;
}
avg.fifo_i = 0;
avg.acc = 0;
last_out = 0;
old_inlet_clock = 0;
if (inlet_clock & !old_inlet_clock) {
last_out = moving_average(&avg, inlet_in);
}
old_inlet_clock = inlet_clock;
outlet_out = last_out;
Variant: 2
frac32buffer Input
frac32buffer Moving average output
combo length
#ifdef FIFO_EXP
#undef FIFO_EXP
#endif
#ifdef FIFO_LEN
#undef FIFO_LEN
#endif
#ifdef FIFO_MASK
#undef FIFO_MASK
#endif
#define FIFO_EXP attr_length
#define FIFO_LEN (1 << FIFO_EXP)
#define FIFO_MASK (FIFO_LEN - 1)
typedef struct _AveragerContext {
int32_t fifo[FIFO_LEN];
int32_t fifo_i;
int32_t acc;
} AveragerContext;
AveragerContext avg;
__attribute__((always_inline)) __STATIC_INLINE int32_t
moving_average(AveragerContext *avg, int32_t new_value) {
avg->fifo_i = (avg->fifo_i + 1) & FIFO_MASK;
avg->acc -= avg->fifo[avg->fifo_i];
avg->acc += new_value;
avg->fifo[avg->fifo_i] = new_value;
return avg->acc >> FIFO_EXP;
}
for (avg.fifo_i = 0; avg.fifo_i < FIFO_LEN; avg.fifo_i++) {
avg.fifo[avg.fifo_i] = 0;
}
avg.fifo_i = 0;
avg.acc = 0;
outlet_out = moving_average(&avg, inlet_in >> FIFO_EXP) << FIFO_EXP;
Variant: 3
int32 Input
int32 Moving average output
combo length
#ifdef FIFO_EXP
#undef FIFO_EXP
#endif
#ifdef FIFO_LEN
#undef FIFO_LEN
#endif
#ifdef FIFO_MASK
#undef FIFO_MASK
#endif
#define FIFO_EXP attr_length
#define FIFO_LEN (1 << FIFO_EXP)
#define FIFO_MASK (FIFO_LEN - 1)
typedef struct _AveragerContext {
int32_t fifo[FIFO_LEN];
int32_t fifo_i;
int32_t acc;
} AveragerContext;
AveragerContext avg;
__attribute__((always_inline)) __STATIC_INLINE int32_t
moving_average(AveragerContext *avg, int32_t new_value) {
avg->fifo_i = (avg->fifo_i + 1) & FIFO_MASK;
avg->acc -= avg->fifo[avg->fifo_i];
avg->acc += new_value;
avg->fifo[avg->fifo_i] = new_value;
return avg->acc >> FIFO_EXP;
}
for (avg.fifo_i = 0; avg.fifo_i < FIFO_LEN; avg.fifo_i++) {
avg.fifo[avg.fifo_i] = 0;
}
avg.fifo_i = 0;
avg.acc = 0;
outlet_out = moving_average(&avg, inlet_in);
Variant: 4
frac32 Input
frac32 Moving average output
combo length
#ifdef FIFO_EXP
#undef FIFO_EXP
#endif
#ifdef FIFO_LEN
#undef FIFO_LEN
#endif
#ifdef FIFO_MASK
#undef FIFO_MASK
#endif
#define FIFO_EXP attr_length
#define FIFO_LEN (1 << FIFO_EXP)
#define FIFO_MASK (FIFO_LEN - 1)
typedef struct _AveragerContext {
int32_t fifo[FIFO_LEN];
int32_t fifo_i;
int32_t acc;
} AveragerContext;
AveragerContext avg;
__attribute__((always_inline)) __STATIC_INLINE int32_t
moving_average(AveragerContext *avg, int32_t new_value) {
avg->fifo_i = (avg->fifo_i + 1) & FIFO_MASK;
avg->acc -= avg->fifo[avg->fifo_i];
avg->acc += new_value;
avg->fifo[avg->fifo_i] = new_value;
return avg->acc >> FIFO_EXP;
}
for (avg.fifo_i = 0; avg.fifo_i < FIFO_LEN; avg.fifo_i++) {
avg.fifo[avg.fifo_i] = 0;
}
avg.fifo_i = 0;
avg.acc = 0;
outlet_out = moving_average(&avg, inlet_in >> FIFO_EXP) << FIFO_EXP;
Variant: 5
bool32.rising Clock input
frac32 Input
frac32 Moving average output
combo length
#ifdef FIFO_EXP
#undef FIFO_EXP
#endif
#ifdef FIFO_LEN
#undef FIFO_LEN
#endif
#ifdef FIFO_MASK
#undef FIFO_MASK
#endif
#define FIFO_EXP attr_length
#define FIFO_LEN (1 << FIFO_EXP)
#define FIFO_MASK (FIFO_LEN - 1)
typedef struct _AveragerContext {
int32_t fifo[FIFO_LEN];
int32_t fifo_i;
int32_t acc;
} AveragerContext;
AveragerContext avg;
__attribute__((always_inline)) __STATIC_INLINE int32_t
moving_average(AveragerContext *avg, int32_t new_value) {
avg->fifo_i = (avg->fifo_i + 1) & FIFO_MASK;
avg->acc -= avg->fifo[avg->fifo_i];
avg->acc += new_value;
avg->fifo[avg->fifo_i] = new_value;
return avg->acc >> FIFO_EXP;
}
int32_t last_out;
int32_t old_inlet_clock;
for (avg.fifo_i = 0; avg.fifo_i < FIFO_LEN; avg.fifo_i++) {
avg.fifo[avg.fifo_i] = 0;
}
avg.fifo_i = 0;
avg.acc = 0;
last_out = 0;
old_inlet_clock = 0;
if (inlet_clock & !old_inlet_clock) {
last_out = moving_average(&avg, inlet_in >> FIFO_EXP) << FIFO_EXP;
}
old_inlet_clock = inlet_clock;
outlet_out = last_out;