]> Sound and music - Practical Considerations

## Practical Considerations

Speed is critical in real-time applications, yet in some code I have seen around the web, there are obvious optimizations that were missed:

• Reduce divisions by calculating the reciprocal once. For example, if you need to work out $a/d$ and $b/d$, it is better to calculate $1/d$ first and then multiply $a$ and $b$ by this quantity because divisions are typically much slower than multiplications. Another obvious example is computing $1/{d}^{2}$ from $1/d$: square it, rather than dividing by $d$. Even though a smart compiler might be able to do this, it is better to rely on yourself.

• If you’re using math libraries to do trigonometry (rather than lookup tables), it may be worth rewriting equations that minimize the number of calls to the math libraries. For example, following the equations for the cookbook LPF directly will mean that both $\mathrm{sin}$ and $\mathrm{cos}$ must be computed. However, if an alternative derivation is used, then only $\mathrm{tan}$ is needed. It’s not clear to me why formulas are given in a relatively inefficient form.

• Common subexpressions should only be computed once. For example, in the Cookbook’s LPF, it’s faster to compute the ${b}_{i}$'s (which by the way correspond to the ${a}_{i}$'s in our notation) by working out ${b}_{0}$ first, and then setting ${b}_{1}=2{b}_{0}$, ${b}_{2}={b}_{0}$.

• Some people use the bilinear transform $k\frac{z-1}{z+1}$ for some constant $k$. This is pointless because $k$ gets cancelled out by the prewarping. The explanation in the Cookbook does not suffer from this problem: there, the prewarping is viewed as part of the bilinear map.

• When coding a specific 2-pole lowpass or highpass filter, a few lookups and multiplications can be avoided by leaving the numerator of the transfer function $H\left(z\right)$ as ${z}^{2}+2z+1$ and hardcoding this expression.

For the cookbook LPF the filter takes the form

$y\left[n\right]=a\left(x\left[n\right]+2x\left[n-1\right]+x\left[n-2\right]-by\left[n-1\right]-cy\left[n-2\right]\right)$

where ${f}_{s}$ is the sampling frequency, $f$ is the cutoff frequency, and

$\begin{array}{ccc}t& =& 1/\mathrm{tan}\left(\pi f/{f}_{s}\right)\\ a& =& 1/\left({t}^{2}+t/q+1\right)\\ b& =& 2-2{t}^{2}\\ c& =& {t}^{2}-t/q+1\\ \end{array}$

(Of course ${t}^{2}$ and $t/q$ need only be computed once.)

The cookbook HPF is similar:

$y\left[n\right]=a\left(x\left[n\right]-2x\left[n-1\right]+x\left[n-2\right]-by\left[n-1\right]-cy\left[n-2\right]\right)$

with

$\begin{array}{ccc}t& =& \mathrm{tan}\left(\pi f/{f}_{s}\right)\\ a& =& 1/\left({t}^{2}+t/q+1\right)\\ b& =& 2-2{t}^{2}\\ c& =& {t}^{2}-t/q+1\\ \end{array}$

Cookbook bandpass:

$y\left[n\right]=a\left(b\left(x\left[n\right]-x\left[n-2\right]\right)-cy\left[n-1\right]-dy\left[n-2\right]\right)$

where

$\begin{array}{ccc}\omega & =& 2\pi f/{f}_{s}\\ e& =& 0.5W\omega /\mathrm{sin}\left(\omega \right)\\ B& =& {2}^{e}-{2}^{-e}\\ t& =& \mathrm{tan}\left(\omega /2\right)\\ a& =& 1/\left(1+{t}^{2}+Bt\right)\\ b& =& Bt\\ c& =& 2{t}^{2}-2\\ d& =& 1+{t}^{2}-Bt\\ \end{array}$

$W$ is the bandwidth in octaves. Note the value given to $B$ is an approximation. For the bandpass filter with peak gain $Q$, leave $b$ out (i.e. $b=1$). The cookbook formulas may be better since we must compute $\mathrm{sin}\left(\omega \right)$ anyway.