mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
adjusted fir_hook() to use the new history buffer
This commit is contained in:
parent
9f4dd78ab3
commit
e1d63db91b
3 changed files with 26 additions and 28 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
#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; i<len; i++) {
|
||||
struct msg *old = &p->history[(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; i<len; i++)
|
||||
sum += hook_fir_coeffs[(cur+len-i)%len] * history[(cur+i)%len];
|
||||
|
||||
m->data[HOOK_FIR_INDEX].f = sum;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue