During reading article about filtering, I've found some strange using of .h
file - use it for filling array of coefficients:
#define N 100 // filter order
float h[N] = { #include "f1.h" }; //insert coefficients of filter
float x[N];
float y[N];
short my_FIR(short sample_data)
{
float result = 0;
for ( int i = N - 2 ; i >= 0 ; i-- )
{
x[i + 1] = x[i];
y[i + 1] = y[i];
}
x[0] = (float)sample_data;
for (int k = 0; k < N; k++)
{
result = result + x[k]*h[k];
}
y[0] = result;
return ((short)result);
}
So, is it normal practice to use float h[N] = { #include "f1.h" };
this way?
Preprocessor directives like #include
are just doing some textual substitution (see the documentation of GNU cpp inside GCC). It can occur at any place (outside of comments and string literals).
However, a #include
should have its #
as the first non-blank character of its line. So you'll code
float h[N] = {
#include "f1.h"
};
The original question did not have #include
on its own line, so had wrong code.
It is not normal practice, but it is permitted practice. In that case, I would suggest using some other extension than .h
e.g. use #include "f1.def"
or #include "f1.data"
...
Ask your compiler to show you the preprocessed form. With GCC compile with gcc -C -E -Wall yoursource.c > yoursource.i
and look with an editor or a pager into the generated yoursource.i
I actually prefer to have such data in its own source file. So I would instead suggest to generate a self-contained h-data.c
file using e.g. some tool like GNU awk (so file h-data.c
would start with const float h[345] = {
and end with };
...) And if it is a constant data, better declare it const float h[]
(so it could sit in read-only segment like .rodata
on Linux). Also, if the embedded data is big, the compiler might take time to (uselessly) optimize it (then you could compile your h-data.c
quickly without optimizations).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments