云南快乐十分前三直选this is the first blog in what will probably be a series on the benefits of using state-space representation to implement discrete infinite impulse response (iir) filters. before i write about state-space and some of the things you can do with it, i want to give an overview of the difference equation and explain (hopefully intuitively) why they can be a poor choice for implementing a filter.

## The discrete difference equation

云南快乐十分前三直选the discrete difference equation takes an input value, a list of previous input values and a list of previous output values and combines them linearly to produce the next output value. this is codified mathematically in the following equation which can be recognised as the sum of two convolutions:

云南快乐十分前三直选instead of the above, we could write the difference equation as two distinct difference equations chained together:

is the output of an all-zero filter which is used as input into an all-pole filter to produce 云南快乐十分前三直选. If we were to take the z-transforms of the two filters, we would see quite plainly that we can also swap the order of the filters as:

This time, is the output of an all-pole filter which is used as input into an all-zero filter to produce . If we set , the benefit of this representation is that the two filters share exactly the same state and so an implementation needs not have delay lines for both output and input. This formulation has the name and is a popular implementation choice.

云南快乐十分前三直选regardless of whether we implement the difference equation directly or use direct form ii, the implementation is easy to realise in a c program given a set of filter coefficients… so why would we want to do anything differently?

### When difference equations go bad…

let’s build a 6-th order elliptic filter and run the difference equation explicitly using the following gnu octave code. the filter is designed with a passband edge frequency of 240 hz at a 48000 hz sample rate (which isn’t particularly low) with 6 db of ripple in the passband and 80 db of stopband attenuation.

N = 6; IRLen = 8000; [b, a] = ellip(N, 6, 80.0, 240.0/24000); dp_y = zeros(IRLen, 1); q = zeros(N, 1); x = 1; for i = 1:IRLen qv = x - a(2:end) * q; dp_y(i) = b * [qv; q]; q = [qv; q(1:(N-1))]; x = 0; end |

云南快乐十分前三直选and here is a plot of the impulse response and frequency response (which both look pretty good!):

oh yeah, octave by default is using double-precision for everything. because we like fast things, let’s specify that the states should always be stored using single precision by replacing the initialisation of q with:

q = single(zeros(N, 1)); |

and here is the new impulse response:

bugger! storing the states in single precision has lead to an instability in the filter. small errors in the state are somehow being amplified by the coefficients of the difference equation and the output is growing without bound.

i’m going to try and give some intuition here: imagine that you are trying to synthesise a low-frequency discretely-sampled sinusoid that follows a slowly decaying exponential envelope. you’ve found that there is a z-transform that does exactly what you want from a :

云南快乐十分前三直选you decide to implement the difference equation directly using direct form ii leading to states that are the previous outputs of the impulse response of the all-pole component of the system.

The intuition is this: the values of the states close to are all very close to zero – but after sufficient time, will eventually reach a very high-peak relative to their starting point without any external influence. Tiny errors in these small state values could grow by multiple orders of magnitude as the state evolves. This effectively happens at every zero crossing of the signal – and that is just illustrative component of the problem.

Ideally, the energy in the states of the system would only ever decrease when regardless of the values of . Unfortunately, this almost never happens with a resonant system implemented using a difference equation. We should ask the question: can we find better states than previous outputs of the all-pole filter?

## State-space

The Direct Form II difference equations presented earlier utilising and can be mutated into some fairly gross matrix equations:

work through these and convince yourself they are equivalent. we can rewrite this entire system giving names to the matrices as:

These matrix equations are a state-space representation. The explicitly listed matrices before it are known as a “controllable canonical form” representation (for the case when ). is the state vector and in controllable canonical form, it holds previous values of 云南快乐十分前三直选 as described in the difference equations earlier.

so why would we want to do this? who cares that we mapped a difference equation into a set of matrix equations? i’m going to list out some reasons which probably deserve their own blogs:

- Ability to analyse system using a vast array of matrix mathematical tools.
- Improve the numerical stability of the system.
- Preserving physical meanings of states during discretisation.
- Ability to design for efficient implementations.

in this blog, we’re going to look at points one and two briefly to attempt to address the problems shown earlier.

firstly, let’s write the state equations in terms of their z-transforms so that we can build an expression that maps input to output:

Doing some rearranging and substitution enables us to eliminate :

The term in the denominator expands to the of in and if we are trying to map a z-domain transfer function to a state-space system, it needs to equal the denominator of the transfer function. Just as the poles of a transfer function are roots of the denominator, the poles of a state-space system are the eigenvalues of the matrix. The numerator of the above expression is a matrix of polynomials in showing how state-space is naturally able to handle multiple-input multiple-output (MIMO) systems.

Earlier, we asked can we pick better values for the states? Before getting to that, let’s answer a different question: can we pick different values for the states? The answer is: yes – and we can prove it trivially by substituting with where 云南快乐十分前三直选 is some invertible square matrix:

This changes the coordinate system that the state vector exists in. We can pick **any** invertible matrix and use it to modify a system’s , and matrices keeping the system output identical. The quantity is a on and it preserves the eigenvalues/characteristic polynomial of for all invertible .

## State-space degrees of freedom

The coefficients of a difference equation are unique for a particular impulse response. The coefficients of the state-space representation for are not. In-fact, we have already demonstrated this previously by showing we can change the coordinate system of the state-vector.

云南快乐十分前三直选let’s go through the process of taking a 2×2 siso system and finding the transfer function.

We can select all of these quantities to match a given second order transfer function – but how should we choose them? What makes for good coefficients? What does good mean? We could force some values to zero or one to minimize multiplies? If we want to improve numeric performance, given that the matrix is responsible for moving states forward in time, it seems reasonable to minimize the impact that errors in the state vector can have when multiplied by – this is akin to making as as possible. This might be a good strategy for fixing our example 6th order system.

Orthogonal matrices are well-conditioned and for those deeply familiar with IIR design, you may also know that the state update for a implementation is effectively determined by an orthonormal matrix that has been attenuated by some scalar. Coupled form state-updates can only be used to mimic a conjugate pole pair, but for every classical IIR design (Butterworth, Chebyshev I/II, Cauer/Elliptic) of even order (like our example 6th order problematic system), all the poles are in conjugate pairs. Start by defining as an orthogonal matrix:

the transfer function becomes:

There are two remaining free variables we can pick. Somewhat arbitrarily, we will select and to get:

云南快乐十分前三直选we now have a system of equations we can solve for the unknown matrix quantities that will create a state-space system that mimics a 2nd order resonant transfer function.

## Chaining systems together

云南快乐十分前三直选consider we have two systems which we would like to connect together:

and:

we can make direct substitutions to get:

we can re-write the above as a set of block matrix equations:

This provides a direct method for connecting systems together into one larger system. As is an upper-triangular block-diagonal matrix, the eigenvalues of the new system are the same as those of the existing systems and the term in the connected system has no influence. Given that, can we decouple the sections completely? i.e. can we completely eliminate the term by using a similarity transform as described previously? Let’s pose the question using block matrices:

there is only one valid set of solutions for these equations:

Where the final equation is a which can be solved without too much pain programatically (i.e. using a matrix inverse) if and only if and 云南快乐十分前三直选 share no common eigenvalues. If we do this, we end up with the following system:

this will be better conditioned and have some other benefits in terms of efficient implementations. this block-diagonalisation we just performed is the equivalent of performing the partial-fraction decomposition on the cascaded system (which i’ve never seen a nice looking algorithmic solution for).

## Bringing it home

云南快乐十分前三直选let’s take our problematic 6th order system, break it up into 3 second order state-space sections using the coupled-form mapping we described and evaluate the performace again:

There are 4 data-sets:

- The original double-precision difference equation results.
- The concatenated coupled-form systems without block-diagonalisation storing the states in single precision.
- The concatenated coupled-form systems without block-diagonalisation storing the states in a q15 format (15 bits of fractional precision).
- The concatenated coupled-form systems with block-diagonalisation storing the states in q15 format.

the performance difference between 2 and the block-diagonalised system with states in signle precision is negligible and so is not included in the plots. the performance difference between 1, 2 and 4 is practiacally identical in the passband. there is some fudging going on with the q15 plots because the precision is fixed but the range is not. i’ve tried to make things more even by:

- 2-Norm matching the and matrices of the systems without diagonalisation.
- 2-Norm matching each sub-section of the and matrices of the system with diagonalisation (which we can do because they are all operating independetly).
- 2-Norm matching the matrices of the diagonalised and undiagonalised systems after the above has happened (which also requires modifying ).

the performance of 1, 2 and 4 are all incredibly good. the performance of 3 shows that cascading leads to error build-up that significantly compromises the output.

these coupled-form state-space filter realisations are good down to very low frequencies. i have had success synthesising sub 10 hz 16th order elliptic filters that work just fine with single-precision floating point. next time, we’ll look at preservation of state meanings when we discretise an analogue system.