diff --git a/server/include/hooks.h b/server/include/hooks.h index b245cc108..b150649df 100644 --- a/server/include/hooks.h +++ b/server/include/hooks.h @@ -65,7 +65,7 @@ int hook_tofixed(struct msg *m, struct path *p); /** Example hook: add timestamp to message. */ int hook_ts(struct msg *m, struct path *p); -#define HOOK_TS_INDEX -1 // last message +#define HOOK_TS_INDEX -1 /* last message */ /** Example hook: Finite-Impulse-Response (FIR) filter. */ int hook_fir(struct msg *m, struct path *p); diff --git a/server/include/utils.h b/server/include/utils.h index bfe635099..dd25dd55e 100644 --- a/server/include/utils.h +++ b/server/include/utils.h @@ -60,6 +60,16 @@ b = tmp; \ } while(0) +/* Return the bigger value */ +#define MAX(a, b) ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a > _b ? _a : _b; }) + +/* Return the smaller value */ +#define MIN(a, b) ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; }) + /* Forward declarations */ struct settings; struct timespec; diff --git a/server/src/hooks.c b/server/src/hooks.c index 8502a960f..571389381 100644 --- a/server/src/hooks.c +++ b/server/src/hooks.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include "msg.h" @@ -102,39 +101,28 @@ int hook_ts(struct msg *m, struct path *p) return 0; } -/** Simple FIR-LP: F_s = 1kHz, F_pass = 100 Hz, F_block = 300 - * Tip: Use MATLAB's filter design tool and export coefficients - * with the integrated C-Header export */ -static const double hook_fir_coeffs[] = { -0.003658148158728, -0.008882653268281, 0.008001024183003, - 0.08090485991761, 0.2035239551043, 0.3040703593515, - 0.3040703593515, 0.2035239551043, 0.08090485991761, - 0.008001024183003, -0.008882653268281,-0.003658148158728 }; - -/** @todo: test */ int hook_fir(struct msg *m, struct path *p) { - static pthread_key_t pkey; - float *history = pthread_getspecific(pkey); + /** Simple FIR-LP: F_s = 1kHz, F_pass = 100 Hz, F_block = 300 + * Tip: Use MATLAB's filter design tool and export coefficients + * with the integrated C-Header export */ + static const double coeffs[] = { + -0.003658148158728, -0.008882653268281, 0.008001024183003, + 0.08090485991761, 0.2035239551043, 0.3040703593515, + 0.3040703593515, 0.2035239551043, 0.08090485991761, + 0.008001024183003, -0.008882653268281,-0.003658148158728 }; - /** Length of impulse response */ - int len = ARRAY_LEN(hook_fir_coeffs); - /** Current index in circular history buffer */ - int cur = m->sequence % len; /* Accumulator */ double sum = 0; + + /** Trim FIR length to length of history buffer */ + int len = MIN(ARRAY_LEN(coeffs), POOL_SIZE); + + for (int i=0; ihistory[(POOL_SIZE+p->received-i) % POOL_SIZE]; - /* Create thread local storage for circular history buffer */ - if (!history) { - history = alloc(len * sizeof(float)); - pthread_key_create(&pkey, free); - pthread_setspecific(pkey, history); + sum += coeffs[i] * old->data[HOOK_FIR_INDEX].f; } - - /* Update circular buffer */ - history[cur] = m->data[HOOK_FIR_INDEX].f; - - for (int i=0; idata[HOOK_FIR_INDEX].f = sum;