Functions with memory simplify writing Digital Signal Processing (DSP) code since a mem
variable can be used to as a single sample delay z^1
. I order to implement the Digital Biquad Filter (direct form 2) we can take the equations directly from Wikipedia: https://en.wikipedia.org/wiki/Digital_biquad_filter.
We can see the architecture and the equations here:
We can implement these equations in Vult as follows.
The variables w1
and w2
represent the terms w[n-1]
and w[n-2]
. The variables y0
and x0
represent y[n]
and x[n]
. The coefficients of the filter are b0
, b1
, b2
, a0
and a2
which are calculated depending on the filter. We can see that w1
and w2
are declared as mem
variables since we want them to keep their value every time the function is called.
Now that we have the architecture of the biquad, we can design filters. In order to design a filter we need to calculate the coefficients. Here we are gonna follow the formulae show in the Audio-EQ-Cookbook. The code for the filter is the following:
In the code, x
is the input signal w0
is the cut frequency (in radians) and q
controls the resonance. This filter is variable and we can adjust the cut frequency and the resonance. In order to achieve that we need to calculate the coefficients every time the cut or resonance changes. We can see in the above code that the implementation is not very efficient since it calculates the coefficients every sample. To improve it can use the function change
that we defined in the Functions with Memory
tutorial.
In the example above, we have changed the declaration of the coefficients to mem
variables because we want to remember the previous values. By wrapping the calculation inside the if-statement
we recalculate the coefficients every time the values of fc
and q
change.
Now that we have the filter, we can use it as part of a bigger program. For example:
Just for you to remember, every time you call the function lowpass
and independent memory space is generated. Therefore, the filter for signal s_1
and s_2
do not interfere each other.
In the next tutorial we are gonna create a state-variable filter and we are gonna show how easy is to do oversampling in Vult.