Building a digital filter for use in synthesisers

This is a tutorial on how to build a digital implementation of a 2nd-order, continuously-variable filter (i.e. one where you can change the parameters runtime) that has dynamic behaviour that mimics an analogue filter. The digital model is linear (i.e. we are not going to model any transistor/diode behavior – we’ll do that in another blog). The resulting implementation is suitable for use in a digital synthesiser or equaliser. It is not attempting to be a perfect model – but it should be quite close.

The tutorial follows this rough guide:

• Difference equations and a recap on state-space.
• Description of the circuit we will be modelling (this can be skipped – it is included for completeness).
• Expressing the desired system digitally using a partially-transformed state-space model.
• Making the filter more useful
• Summary of implementation and demo video

Difference equations and state-space recap

Most EE grads know how to get a digital implementation of an analogue filter:

1. Pick a transfer function and select values for the natural frequency () [rads/second] and the dimensionless Q-factor () – these two controls are like the “frequency” and “resonance” knobs on a synth filter.

2. Discretise this using your favourite method (e.g. the Bilinear Transformation) to obtain a transfer function in .
3. Implement the difference equation which has coefficients trivially taken from the previous step.

More detail on those last two steps can be found in this older blog Three step digital Butterworth design.

This process works fairly well and difference equations are relatively-inexpensive to implement. But what if we want to attach and to knobs a user could twiddle (as is the case if we were building a synth)? Can we simply update the difference equation coefficients as the system processes data?

The answer is: it depends. Assuming you are using the bilinear transform, the discretisation process will always result in coefficients that are stable i.e. if the input is zero, the output will eventually decay to zero and changing the difference equation coefficients on the fly will never cause the system to end up in some state which it cannot recover from. However, the transient that the coefficient change introduces may be inappropriate for the use case. For example: if changing the coefficients can cause a large burst of energy in the output that takes thousands of samples to decay – well, that might not be okay in a synthesiser.

In the State-Space Representation – An Engineering Primer post, we went into a decent amount of detail of the benefits of working in state-space representation. The main relevant takeaway from that post is that while the transfer function representation is unique for a particular system, the state-space representation is not. A change of coordinates can be applied to the states which does not impact the steady-state response of the filter – but this change will impact the transient response to system parameter changes!

The state vector ( using terminology from the old blog) of a system derived from a difference equation consists of previous output samples from an all-pole filter. Every sample processed through the system shifts the states of the vector (discarding one element) and creates a new value. Think about this for a while as there is some intuition to be had. Consider a low-pass, very-low-frequency and under-damped (i.e. exhibits an exponentially-decaying sinusoidal impulse response) system. When an impulse is pushed in, the magnitude of all elements in the state vector slowly rise, eventually all have similar values (near the peak), then all start falling until they all have similar values again in the trough. A small error introduced in the state when all values are the same could lead the filter onto a completely different trajectory. This was demonstrated again in the state-space primer blog with the 6th order system’s inability to run with a difference equation with single-precision floats.

So how do we pick the 2nd order system to use? For a synth, the answer is probably: whatever sounds “best” as perceived by the designer. All systems will exhibit different behavior as their coefficients change. In this blog, we will chose an analogue circuit to approximately model – but that is just that: a choice.

The analogue circuit

The circuit below is what we are going to model. It is a Voltage-Controlled Voltage Source (VCVS) filter topology. The VCVS design permits the Q-factor and natural frequency to be controlled independently. I have explicitly labeled the voltages across the two capacitors because these are the states we are going to try and preserve in our digital implementation i.e. we’re not going to use the two previous all-pole output values as with a typical difference equation. By doing this, when we vary parameters, it will be like changing component values while leaving the instantaneous voltages across those capacitors the same – this is realistic, we can vary resistors by replacing them with potentiometers.

VCVS filter topology

Explaining how to apply circuit analysis is way out of scope for this blog. But one set of equations that describes the circuit in the Laplace domain is given below:

Before we go further, we will make some substitutions in the above equations to simplify further working. Define and to get:

At this point, we can do some substitution of the equations above to find i.e. the unique transfer function of the circuit:

This looks very similar to the resonant lowpass function given previosuly with and an additional gain of tacked on.

Given that there are no common components in determining and , the analogue VCVS filter topology has the ability to provide independent control of and (although, the overall gain of the system is affected by ).

We’ve stated what we want: the states to approximate the voltages across the capacitors. We know that we cannot do that if we use a standard difference equation obtained via a transfer function. So what next?

Straight to state-space

We derived the equations for the analogue model carefully in the previous section. We ensured that the terms only existed on the left hand side and only multiplied each of the states we said we were interested in. This enables us to write out the state-space model directly as follows:

If we define the state vector , the input vector and the output vector we can write the above in the usual state variable representation:

The top equation is the state equation. The bottom is the output equation. Recall that the state-space mapping is not unique. We can change the coordinates of the vector using any invertible matrix as demonstrated in the state-space primer post. But here, we have intentionally chosen the states we want to use so that our system behaves similarly to the circuit. What we will now do is apply the bilinear transform to this system in such a way that our output equation remains the same as in the continuous system – which it must if we are actually modelling the circuit. Defining the bilinear transform as:

Where is related to the sampling rate of the system we are building and can be used to control the specific frequency in the frequency response that maps exactly to the -domain transfer function (see the wikipedia page for the bilinear transform for more information). Make the substitution to the state update equation and attempt to make the left hand side :

Note that the input mixing term includes . Normally, this term would be moved into the output equation (so that it can be eliminated) and would cause the and matrices to change indicating that we have changed the states – but we don’t want this! Instead, we simply implement that operation on the input before mixing it.

The only thing that remains is to make the system causal i.e. the system has the following form:

This depends on future samples of . We multiply the state update equation by to get:

Converting this into a state-space difference-equation we get:

The above is what we implement. and are the same as what we derived for our continuous time system ( and ). and are defined as:

Making it more useful

If we stopped here, we would have the lowpass filter the circuit represented. But who just wants a lowpass filter?

Based on how we designed the system earlier, all the properties of the circuit are modeled by the and matrices. The and matrices can be thought of as buffering values available at various points in the circuit and mixing them with various gains to create the output i.e. the and matrices can be thought to not really impact the circuit – only how the output is tapped from it. If we let and and expand the analogue transfer function, we get:

We can use , and to create any numerator of our choosing. The denominator is completely determined by the matrix, but recall the list of resonant filters at the start of this post also share the same denominator. We can use this information to build multiple outputs from the same states and input sample! We can even build some smooth control to transition between them. Let’s try obtain these quantities from something a bit more readable:

Solving for , and :

• Setting , and makes the resonant lowpass filter.
• Setting , and makes the resonant highspass filter.
• Setting , and makes the resonant bandpass/bandstop filter dependent on the value of .

We could introduce a parameter to transition between lowpass and highpass as follows:

When , the system is lowpass. When , the system is highpass. When , we have a bandstop filter with a wideband attenuation of 6 dB. That’s kinda funky, but it would be cool if we could hack in some controllable bandpass/bandstop behaviour too. We can fudge this by setting:

Now when we have a controllable bandpass/bandstop filter dependent on the value of .

The controls in the red outline control the filter.

The “Sync” checkbox controls whether should be:

• If unchecked, a constant. The slider next to it will be in Hz.
• If checked, a multiple of the main oscillator frequency.

The “LP/BP/HP” slider directly controls . gets the linear gain corresponding to the value of “BP Gain”. The value of the “Q” slider is converted to as described earlier.

Summary of implementation and demo video

The implementation has 4 input parameters:

• is the continuous mode control. It is in the range [0, 1]. At 0, the system is lowpass. At 1, the system is highpass. At 0.5, the system is bandpass/bandstop.
• is the linear bandpass/bandstop gain. When and , the system is bandstop. When and , the output is the input. When and , the system is bandboost.
• the natural frequency of the filter – this will be close to where resonance occurs.
• the derived from the Q-factor.

For every sample:

1. Smooth the control parameters , , , with 1-pole smoothers to avoid instantaneous jumps which will cause glitches.
2. Form the and matrices and compute their discretised counterparts as described (yes, this is an entire bilinear transform per sample).
3. Evaluate the state update equation to form the new states.
4. Derive and using:

5. Evaluate the output equation to get the next output sample.

Here’s a video of it in action: