IIR filters may be implemented as direct form 1 or 2. But how do you decide which form to use? What are the pros and cons of each?
First of all, a bit from wikipedia on Direct Form I and II implementation.
Direct form I requires more memory, but is a somewhat simpler strategy, and is less likely to have round-off and resonance problems.
Direct form II requires less memory, but it has the potential for unusual interactions, larger numbers, and more round off error. Much of this can be reduced by cascading smaller filters, especially 2nd order filters.
A little dated but may deserve a more comprehensive answer, especially since Direct Form II can get you into a lot of trouble. First of all, there is no "one size fits all" and the best choice depends on your specific application and constraints. What you can consider is
- Memory: Direct Form II and Transposed Form II take a little less state memory then Direct Form I and Transposed Form I but in a cascaded second-order section implementation the difference is minor
- MIPS: In terms of number of multiplies and additions, all four implementations are the same. However depending on the instruction set of the specific processor, there can be significant differences in implementation efficiency. So "it depends".
- Fixed Point properties: This makes a huge difference. Primarily you have to make sure that your state variables don't overflow so you need to analyze the transfer function between in/output and the state variables. Here Direct Form I and Transposed Form II are the clear winners. State variables are bound to +106dB or thereabouts vs input and output. In direct Form II for example, the transfer function from input to state is given by the poles only. I have seen real world examples where this can actually exceed 100 dB of gain. This is an absolute no non for fixed point implementation.
- Noise: In terms of truncation and round off error, all implementations are roughly the same. The state variable transfer function problem mentioned in 3) also affects this to some extend and I have seen audible noise issues with Direct Form II filters even when using 32-bit floating point.
So in summary Transposed Form II is more often than not the best choice. In some fixed point scenarios, especially if there are significant noise problems Direct Form I is better since it can more easily augmented with things like Error Spectrum Shaping, etc.
Unless your working with a very resource-scarce system or have extreme requirements it really doesn't matter in practice whether you choose direct form I or II. For instance, if you're doing stuff on a PC or a smartphone it really doesn't matter. Personally, I prefer Form I.
The real issue is normally MIPS and if you plan to do a fixed-point implementation things get more complicated. For instance, on ARM your IIR filter will consume much more MIPS if both coefficients and filter states are 32-bits. 32 bits in states and coefficients is a requirement if you need to implement for instance a low-pass filter with very low cut-off frequency. In those cases you can use a different type of filter, such as for instance a state-variable filter.
Aside from the technical differences such as numerical accuracy, there is also the issue of stability. When digital filters have pole/zero pairs close to each other, the frequency response can become unstable at various locations (usually approaching Nyquist or approaching zero).
When IIR filters are used for musical applications, the choice of realization can have a profound effect on filter stability when filter parameters are modulated in real time (for example, varying the cut-off frequency on a low pass filter).
I have an open source application that lets you listen to the differences, while time-varying parameters such as cutoff frequency or peak gain, of each of the following realizations:
- Direct Form I
- Direct Form II
- Transposed Direct Form I
- Transposed Direct Form II
- Lattice Form
- State Variable
The project is here:
In addition to the comments above about memory, MIPS, noise, and stability; there is another factor that is commonly overlooked. This factor is reset capability; which happens to be very important in most of my situations.
With a Direct Form II implementation, there are no states for the delayed outputs, therefor if you "reset" a Direct Form II filter to say "5", it's initial output is not going to be 5. I find this to be quite counter-intuitive which has caused some serious frustration when using Simulink. For this reason I almost always use a Direct Form I implementation. Unfortunately Simulink only supports Direct Form I if you have the DSP toolbox, and even then it still doesn't let you set the initial conditions from an input signal.