# Matlab FIlter coefficients to CMSIS api

I am trying to implement iir Band Pass filter design in STM32F429i discovery kit. I konw that it needs coefficients to be generated from matlab and feed them into cmsis api to achivi filtered data.

My SOS Matrix look like shown below:

1,  0,  -1,  1, -1.9974,   0.99743,
1,  0,  -1,  1, -1.9995,  0.99947,
1,  0,  -1,  1,  -1.9937,  0.99371,
1,  0,  -1,  1, -1.9982,  0.99817,
1,  0,  -1,  1, -1.995,  0.99499,


above matrix i put into my coeff array removing column 4th which contains all 1's since its A0 in coeff.

static float32_t filter_coeffs[] = {
1,  0,  -1,  -1.9974,   0.99743,
1,  0,  -1,  -1.9995,  0.99947,
1,  0,  -1,  -1.9937,  0.99371,
1,  0,  -1,  -1.9982,  0.99817,
1,  0,  -1,  -1.995,  0.99499,
};


here is the code snippet am using :

#define NUMSTAGES 5
static float32_t firStateF32[2*NUMSTAGES];


when I am using matlab my signal are filtered perfect, but in microcontroller output is initially oscillating and than "1.#QNAN" am i missing any step?? please correct me.

Regards, Lokesh Bhatt

I'm not sure how you achieve a useful result in Matlab, because your filter is actually unstable. If you compute the pole radii of the second order sections you get:

max(abs(roots(sos(1,4:6))))    % 0.99871
max(abs(roots(sos(2,4:6))))    % 1.0052
max(abs(roots(sos(3,4:6))))    % 0.99685
max(abs(roots(sos(4,4:6))))    % 1.0047
max(abs(roots(sos(5,4:6))))    % 1.0015


So all poles are virtually on the unit circle, which will give you trouble when trying to implement such a filter. Something went wrong in the design process. If you have doubts concerning the filter design process you should probably formulate a new question.

• Thanks Matt for quick reply. I am new to matlab and dsp. can u plz guide me to the new link where i can learn about filter stablilty and unstability using method you described. what should be ideal values in above calculation? any quick help... Mar 30, 2015 at 7:31
• @lokesh: The poles of the filter transfer function must be inside the unit circle of the complex $z$-plane, i.e. the pole radii must be smaller than $1$. The poles are the roots of the polynomial described by the $a$ (denominator) coefficients. Also check this link. Mar 30, 2015 at 7:49
• @lokesh: How did you design that filter? Mar 30, 2015 at 8:41
• from fdatool Filter Structure : Direct-Form II, Second-Order Sections Number of Sections : 5; Bandpass IIR butterworth filter , Filter Order = 10; fs = 1000; Fc1=0.2 Hz Fc2 = 1Hz. Using this specs i created a filter and used on my data samples of 60K. I got the pulse i was interested in. Mar 30, 2015 at 9:42
• @lokesh: OK, but what were the specifications (cut-off frequencies, etc.)? Your cut-off frequencies are extremely small and close to each other, given the sampling frequency. Mar 30, 2015 at 9:45

I know this is an old question, but maybe this is useful to someone. In CMSIS documentation, in Biquad Cascade IIR Filters Using Direct Form I Structure, says that the used equation is:

$$y[n] = b_0 x[n] + b_1 x[n-1] + b_2 x[n-2] - a_1 y[n-1] - a_2 y[n-2]$$

You need to look closely to coefficients $a_1$ and $a_2$. You need to change the sign.

So in your case, if matlab gave you this result (SOS Matrix)

1,  0,  -1,  1, -1.9974,   0.99743,
1,  0,  -1,  1, -1.9995,  0.99947,
1,  0,  -1,  1,  -1.9937,  0.99371,
1,  0,  -1,  1, -1.9982,  0.99817,
1,  0,  -1,  1, -1.995,  0.99499,


then you should change it to:

static float32_t filter_coeffs[] = {

1,  0,  -1,  1.9974, -0.99743,
1,  0,  -1,  1.9995, -0.99947,
1,  0,  -1,  1.9937, -0.99371,
1,  0,  -1,  1.9982, -0.99817,
1,  0,  -1,  1.995,  -0.99499,
};