added fixed point exponential average filters

This commit is contained in:
Alessandro Mauri 2026-04-19 18:54:44 +02:00
parent 2c82d8d42e
commit a1590577d2
2 changed files with 23 additions and 4 deletions

15
fw/filter.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef _FILTER_H
#define _FILTER_H
// Fixed-Point Exponential Moving Average
// alpha = 1/2^k
// x: output value
// s: current sample
#define U16_FP_EMA_K2(x, s) (u16)((((u32)(x)<<2) - (x) + (s)) >> 2)
#define U16_FP_EMA_K4(x, s) (u16)((((u32)(x)<<4) - (x) + (s)) >> 4)
#define U16_FP_EMA_K8(x, s) (u16)((((u32)(x)<<8) - (x) + (s)) >> 8)
#define U16_FP_EMA_K16(x, s) (u16)((((u32)(x)<<16) - (x) + (s)) >> 16)
#endif // _FILTER_H

View File

@ -5,6 +5,7 @@
#include "lib_i2c.h" #include "lib_i2c.h"
#include "display.h" #include "display.h"
#include "filter.h"
// Pin definitions // Pin definitions
@ -199,13 +200,16 @@ __attribute__((noreturn)) int main(void)
u8g2 = display_init(); u8g2 = display_init();
for (;;) { for (;;) {
static uint16_t tip_mv, vbus_mv, current_ma;
static int16_t temp_k;
poll_input(); // usb poll_input(); // usb
u32 start = funSysTick32(); u32 start = funSysTick32();
uint16_t vbus_mv = ((u32)funAnalogRead(VBUS_ADC_CHANNEL)*VCC_MV*11)/4096; vbus_mv = U16_FP_EMA_K2(vbus_mv, ((u32)funAnalogRead(VBUS_ADC_CHANNEL)*VCC_MV*11)/4096);
uint16_t current_ma = get_current_ma(funAnalogRead(CURRENT_ADC_CHANNEL)); current_ma = U16_FP_EMA_K2(current_ma, get_current_ma(funAnalogRead(CURRENT_ADC_CHANNEL)));
int16_t temp_k = get_temp_k(funAnalogRead(NTC_ADC_CHANNEL)); temp_k = U16_FP_EMA_K2(temp_k, get_temp_k(funAnalogRead(NTC_ADC_CHANNEL)));
uint16_t tip_mv = ((u32)funAnalogRead(TEMP_ADC_CHANNEL)*VCC_MV)/4096; tip_mv = U16_FP_EMA_K2(tip_mv, (u32)(funAnalogRead(TEMP_ADC_CHANNEL)*VCC_MV)/4096);
u8g2_ClearBuffer(u8g2); u8g2_ClearBuffer(u8g2);
u8g2_SetBitmapMode(u8g2, 1); u8g2_SetBitmapMode(u8g2, 1);