I have many (~100 or so) filter coefficients calculated with the aid of some Matlab and Excel that I want to dump into a C header file for general use, but I'm not sure what the best way to do this would be. I was starting out as so:
#define BUTTER 1
#define BESSEL 2
#define CHEBY 3
#if FILT_TYPE == BUTTER
#if FILT_ROLLOFF == 0.010
#define B0 256
#define B1 512
#define B2 256
#define A1 467
#define A2 -214
#elif FILT_ROLLOFF == 0.015
#define B0 256
#define B1 512
// and so on...
However, if I do that and shove them all into a header, I need to set the conditionals (FILT_TYPE
, FILT_ROLLOFF
) in my source before including it, which seems kinda nasty. What's more, if I have 2+ different filters that want different roll-offs/filter types it won't work. I could #undef
my 5 coefficients (A1-2, B0-2) in that coefficients file, but it still seems wrong to have to insert an #include
buried in code.
Edit: This is for an embedded 8-bit processor with very small (2-4K) code space. I cannot seem to accomplish this by storing them into an array of structs because the space it consumes is unacceptable. Even declaring them all constant, my compiler will not 'optimize them away' so I'm left with a shade over 1.2K of extra binary data.
The below does not work.
typedef struct {
int16_t b0, b1, b2, a1, a2;
} filtCoeff;
const filtCoeff butter[41] = {
{256,512,256,467,-214},
{256,512,256,444,-196},
{255,512,255,422,-179},
// ...
};
const filtCoeff bessel[41] // ...