State-space filters in fixed-point?

I am having difficulties implementing state-space filters in (32-bit) fixed-point. This is because the coefficients have a huge dynamic range, for example (MATLAB code):

A = [0.989923894 -116924.375; 4.34027786e-10 1];
B = [1; 0];
C = [0.010076086 1];
D = 0;


[sos, gain] = ss2sos(A,B,C,D);


the coeffs are pretty normal:

sos = 0    1.0000   -1.0000    1.0000   -1.9899    0.9900

b0 =  0.0000
b1 =  1.0000
b2 = -1.0000
a0 =  1.0000
a1 = -1.9899
a2 =  0.9900

gain = 0.0101


Is there anything I can do to make my life easier? Any advice most welcome.

• Do you need to implement the filters in space-state form ? Or would an SOS form be ok? – Ben Oct 30 '20 at 11:30
• Should be state-space because it's time-varying. – Danijel Oct 30 '20 at 14:09
• State space models are not a unique system representation due to using any nonsingular similarity transformation. So you could use such similarity transformation which balances this. Does the build in matlab function balreal yield desirable results? – fibonatic Oct 30 '20 at 14:33
• is this written in C or C++? do you have a fixed type? or will you be implementing this using int or long? – robert bristow-johnson Oct 30 '20 at 16:25
• and all of those forms, whether they be Direct Form I or Direct Form II or transposed Direct forms or the Chamberlin form or the Lattice form or the Gold-Rader form or something else, all of those forms can be expressed in state-space model. the state-space model is a generalization of all of the other structures. there are a zillion different ways of implementing the same filter (from an I/O perspective) with the state-space model. – robert bristow-johnson Oct 30 '20 at 16:31

Do a similarity transform on the state-space equation*.

I'm assuming your original system is $$\begin{matrix} x_n = A x_{n-1} + B u_n \\ y_n = C x_n \end{matrix}$$

Let $$T = \begin{bmatrix} 1 & 0 \\ 0 & 5 \cdot 10^{-8}\end{bmatrix}$$ (Note that I arbitrarily chose this by educated eyeball and some messing around -- any $$T$$ that is invertible will work in theory, so you're free to chose one that makes your problem pretty.)

Then define $$\chi_n = T x_n$$. The system $$\begin{matrix} \chi_n = T^{-1} A T \chi_{n-1} + T^{-1} B u_n \\ y_n = C\,T\,\chi_n \end{matrix}$$

has exactly the same input-output dynamics as your original system. The difference is that $$T^{-1} A T \simeq \begin{bmatrix}0.98992389 & -0.00584622 \\0.00868056 & 1\end{bmatrix}$$ is much better conditioned numerically. You'll still have the issue that your C matrix is "poorly conditioned", but that pretty much means that the second element of $$\chi$$ doesn't have much effect on your final answer.

* "Linear Systems" by Thomas Kailath, pp 53-54, Prentice-Hall, 1980

• And if all you're interested in doing is to implement a SISO filter, then just use a biquad. – TimWescott Oct 30 '20 at 19:05
• and, if it's fixed-point, OP should use Direct Form 1 (with a double-wide accumulator) or, perhaps, a Lattice form. – robert bristow-johnson Oct 31 '20 at 2:25