mov_avg_clk

Moving average, integer (no loss of precision), clocked, k-rate
Author: Are Leistad
License: BSD
Github: drj/math/moving_average.axo

IO Variants: 5


Variant: 1

Inlets

bool32.rising Clock input

int32 Input

Outlets

int32 Moving average output

Attributes

combo length

Declaration
#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;
Init
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;
Control Rate
if (inlet_clock & !old_inlet_clock) {
  last_out = moving_average(&avg, inlet_in);
}
old_inlet_clock = inlet_clock;
outlet_out = last_out;

Variant: 2

Inlets

frac32buffer Input

Outlets

frac32buffer Moving average output

Attributes

combo length

Declaration
#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;
}
Init
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;
Audio Rate
outlet_out = moving_average(&avg, inlet_in >> FIFO_EXP) << FIFO_EXP;

Variant: 3

Inlets

int32 Input

Outlets

int32 Moving average output

Attributes

combo length

Declaration
#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;
}
Init
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;
Control Rate
outlet_out = moving_average(&avg, inlet_in);

Variant: 4

Inlets

frac32 Input

Outlets

frac32 Moving average output

Attributes

combo length

Declaration
#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;
}
Init
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;
Control Rate
outlet_out = moving_average(&avg, inlet_in >> FIFO_EXP) << FIFO_EXP;

Variant: 5

Inlets

bool32.rising Clock input

frac32 Input

Outlets

frac32 Moving average output

Attributes

combo length

Declaration
#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;
Init
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;
Control Rate
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;

Privacy

© 2024 Zrna Research