I use the FDAtool package to work with digital filters. I have always worked with coefficients in float format. I noticed that FDAtool always makes the b coefficients as integers. It also always adds gain before each filter section.
Here is an example of coefficients generated by FDAtool for a 4th order low pass filter, sampling frequency 1 kHz, cutoff frequency 100 Hz:
Section 1:
B0=1
B1=2
B2=1
A1= -1.320913434
A2= 0.6327387691
Gain= 0.07795634121
Section 2:
B0=1
B1=2
B2=1
A1= -1.048599601
A2= 0. 296140343
Gain= 0.06188519672
Next, this is how I implemented this filter on the microcontroller:
float lp100_sek1_x[3],lp100_sek1_y[3],lp100_sek1_a[2]={-1.320913434, 0.6327387691},
lp100_sek2_x[3],lp100_sek2_y[3],lp100_sek2_a[2]={-1.048599601, 0. 296140343};
//function call
lp_filter(lp100_sek1_x,lp100_sek1_y,lp100_sek1_a,0.07795634121,((float)res_adc));
lp_filter(lp100_sek2_x,lp100_sek2_y,lp100_sek2_a,0.06188519672,lp100_sek1_y[0]);
void lp_filter_one_section (float x ,float y ,float aa ,float g ,float in)
{
x[2]=x[1];
x[1]=x[0];
x[0]=ing;
y[2]=y[1];
y[1]=y[0];
y[0]=(x[0] + x[1]+x[1] + x[2] - aa[0]y[1] - aa[1]y[2]); //b0=1 b1=2 b2=1
}
Now I need to work with integer coefficients, and I don’t understand how to make the FDAtool package generate the integer coefficients I need, as well as generate coefficients without gain. The FDAtool package has a “Filter Arithmetic” tab, where you can configure parameters for generating integer coefficients, but I can’t figure out all the parameters that can be configured there (it’s not clear to me what the Input/Output and Filter Internals tabs are responsible for). I also don’t understand how to configure this package so that it does not generate gain before each filter section. With an integer implementation of the filter, this is very important for me, since when I multiply the input signal by gain and then shift it, I will lose the values after the decimal point every time.
lp_filter_one_section()function call, you are drawing the output sample fromy[0]and putting it where it's supposed to go? – robert bristow-johnson Dec 11 '23 at 22:55[b0, b1, b2, a1, a2]and gaingis the same as a filter with coefficients[b0*g, b1*g, b2*g, a1, a2]. Is that what you are looking for? – ZR Han Dec 12 '23 at 06:31