State-space representations of filters come up frequently in the topic of control systems but do not seem to be a focus when learning-about or implementing regular filter structures. Instead, the focus seems to be on transfer functions. I think this is quite a shame because state space representations both: map conveniently to implementations and provide a much more natural way to see and exploit the mathematics behind a filter. Their representation in terms of matrices opens up a gigantic amount of mathematical tools for manipulating them. Their usefulness is demonstrated in my previous post (Arbitrary polynomial-segment signal generation) and I figured would expand on some of the details in this post… although, this is really just scratching the surface.

I did some googling to see if I could figure out why state space is not completely ubiquitous and stumbled upon this DSP related blog post which appears to ponder the same thing. The author argues that “the reason is how electrical engineers think” – which is probably true – but surely the way we think has been conditioned by our education (and/or work).

I want this page to provide a bit of an overview of state space and hopefully provide some compelling reasons to use it. Let’s see how it goes…

## A quick introduction.

At the core, state space representations consist of two matrix equations:

The choice of (as opposed to or another variable) is irrelevant – but intentional in this post as we are going to focus on discrete systems. The and matrices define how the previous states (stored in the column vector ) and input combine to create the next set of states. and show how the previous states and input are mixed to create the output. The above is equivalent to the difference equation below:

Where , and are column vectors at time .

When we design and implement IIRs using transfer functions and difference equations, the states are the previous output samples. In a state space system, we *can* define the states as previous output samples, but there are many ways we can assign the states. I’m going to suggest that choosing previous output samples as states is actually quite a bad choice: it’s convenient and easy to understand in an implementation, but must imply that the “true” states need to be derived from previous output samples. Consider modelling an analogue electronic system with a capacitor and an inductor: somehow our model is predicting the voltage across the capacitor and current through the inductor using previous output samples. In theory, this is no problem – but on a computer with fixed precision, it can explode into instability depending on the system. This has already been widely discussed with high-order difference equations and is one of the main reasons for using cascades of biquad sections to implement high-order filters. With state space, we can quantify just how badly a system will perform by looking at the condition number of the matrix. The condition number is a reflection of how much small changes in state can be amplified when passed through the matrix which is a metric for defining the loss of precision in a process.

## Going from a transfer function to state space

While the transfer function of a system is unique, the state space representation is not. There are an infinite number of ways to choose , and that will give exactly the same output.

Choosing good ways to map a transfer function into state space is outside the scope of this post, but we can create a system which is equivalent to the 2nd order direct-form system below:

As:

The above is implemeneted in ss_siso_init() on GitHub. As stated in the comments, it’s not a particularly good mapping.

Writing a program to execute the filter is only needs to evaluate the matrix mutliplies. Notice that for a single-input single-output system (SISO), is a column vector, is a row vector and is a scalar – so we are exclusively doing vector-matrix multiplies. If the system happened to be 4th order (i.e. the matrix is 4×4) and we were targetting x86 with SSE, we could do the whole state update operation using SSE operations.

## Going from state space to a transfer function

Going back to a transfer function from state space is more complicated – although for a 2nd order system (or a block diagonal system of 2×2 and 1×1 blocks), we can work out a closed-form solution by hand. The transfer function of a state space system is given as:

I’m going to argue that going in this direction is almost never necessary, so we aren’t going to think much about it. If we need to figure out the frequency responce, we can do exactly what we do with transfer functions and make the substitution – but now we have a matrix inverse to perform.

## Changing the states

Let’s make the substitution where is some NxN invertible matrix. To remove the from the LHS of the state update, we will left-multiply both sides by to obtain a new system in state space form.

We now have a system which still produces the same output, but the values of the internal states will take on different values (provided ). This can be particularly useful because:

- We mentioned before that if the matrix has a high condition number, small changes in state variables can be amplified substantially during the state update. On a computer with limited precision types, this can lead to errors which propagate to the output through the matrix. By making this transformation, we might be able to reduce the condition number of the new state update matrix.
- Executing the vector-matrix multiply for the state update increases in complexity as the order of the system increases with . If we can find some way to diagonalise or block diagonalise the state update matrix, we could reduce this to a complexity increase in .

## Concatenation of state space systems

When we have the transfer functions of two systems and want to concatenate them together (i.e. send the output of one to the input of another), we can multiply the transfer functions to get the combined system. With state space, there is a bit more process but we can do a similar thing. Denote two systems:

We can join these systems together to obtain:

If we now define a new column vector that is the concatenation of the two column vectors and , we can work out the matrices for the combined system. i.e.

As we mentioned before, growing the size of the matrix increases complexity in unless we can somehow figure out a way to block diagonalise the matrix. If we choose to do this when we concatenate systems together, it means that we need to find some similarity transform that satisfies:

As long as and share no common eigenvalues, this problem has a unique solution. Expanding out the above into distinct matrix equations, it turns out that:

The final equation is known as a Sylvester equation and there are closed-form methods for solving them.

I have an implementation which performs this entire diagonalising concatenation in the ss_siso_cat_diag() function on GitHub.

This is a really cool result because it means that if we construct a high-order filter using second order state space representations and none of those individual representations have common eigenvalues, we can completely block diagonalise the system i.e. the filters can be run completely in parallel! This is very similar to a partial fraction decomposition of transfer function. Furthermore, the state space representation of the block diagonalised system shows that there is an efficient implementation for running the system on a computer with vector support where the vectors are half the length of the filter order. Denote a 8th order system with a block diagonalised state transition matrix:

Each lower-case letter represents some coefficient vector which will exist in memory. With the system laid out like this, we can write some C to implement the filtering operation (this is written using my cop vector abstraction which is fairly opaque):

#include "cop/cop_vec.h" float filter(const float *coefs, float *state, float input) { /* load coefficients */ v4f a = v4f_ld(coefs + 0); v4f b = v4f_ld(coefs + 4); v4f c = v4f_ld(coefs + 8); v4f d = v4f_ld(coefs + 12); v4f e = v4f_ld(coefs + 16); v4f f = v4f_ld(coefs + 20); v4f g = v4f_ld(coefs + 24); v4f h = v4f_ld(coefs + 28); float i = coefs[32]; /* load existing state */ v4f q0 = v4f_ld(state + 0); v4f q1 = v4f_ld(state + 4); /* perform state update and compute output*/ v4f inb = v4f_broadcast(input); v4f new_q0 = v4f_mul(e, inb); v4f new_q1 = v4f_mul(f, inb); v4f out = v4f_mul(g, q0); new_q0 = v4f_add(new_q0, v4f_mul(a, q0)); new_q1 = v4f_add(new_q1, v4f_mul(c, q0)); out = v4f_add(out, v4f_mul(h, q1)); new_q0 = v4f_add(new_q0, v4f_mul(b, q1)); new_q1 = v4f_add(new_q1, v4f_mul(d, q1)); v4f_st(state + 0, new_q0); v4f_st(state + 4, new_q1); /* horizontal adddition required here but I don't support it */ return v4f_hadd_not_implemented(out) + i * input; } |

The point of including the code is to show how we can filter a sample through an 8th order system efficiently utilising vector parallelism with 8 multiply/MAC operations. Implementing this with cascaded biquads would:

- Probably lead to pipelining issues as each biquad is dependent on the output of the previous one.
- Probably not be implementable using vector support.

## Multiple input processing

As a final point, if we wanted to derive a way to process multiple input samples through a high-order difference equation, there is a substantial amount of tedious substitution to be done. If we stay in a state space representation, there is a single substitution to make. For example:

The final expressions above for and are only valid for .

This may or may not be useful depending on what you’re trying to do, but in my previous post I made heavy use of it to derive simple expressions for when is a polynomial in n. It would likely also be of use in building resampling filters where many output samples are discarded and most input samples are zero.