## Sound Synthesis (2/ Filters)

By Vermeille on Tuesday 16 April 2013, 23:55 - Permalink

## Introduction

We saw, in the last article, how to create simple waveforms. But this sounds really lacks modulation, like filter or ADSR envelops. Today, we'll see how to implement low-pass and high-pass filters.

Here is the result!

## Time domain

When you are listening to music, your speakers moves accordingly to signal's amplitude. This is what we call the time domain: we describe how our signal's amplitude vary through the time.

## Frequency domain

The difference between a noise and a note is periodicity of the signal. This is therefore natural to cut one period of our note to analyze it. As I explained in my previous article, if you already played with a synthesizer, you certainly know that the sinus is the base "brick" of signal. In this article we analyzed frequency decomposition of square, triangle and sawtooth waves. Expressing our signal in a sum of couple amplitude / frequency is expressing our signal in the frequency domain.

## Fourier

The Fourier's Transform is the mathematical operation which analyzes a time domain representation and outputs it's frequency representation.

There is a lot of Fourier's Transforms: some can be used with a streamed signal, some outputs/inputs complex numbers, some use just cos() to forget the imaginary part.

In a general manner, the Fourier transform for a discrete signal is

Understand its output as an array of complex numbers. is the cosine phase, , the sine phase, the amplitude and the phase.

This article explains it better than I could, go read it

The Inverse Fourier's Transform goes from frequency to time representation.

## Filters

A filter is described by its response frequency: how much it attenuates or amplifies a frequency going through it. Intuitively, we think that it should be simple to implement a filter as an array of coefficients for each frequency, build the frequency domain representation of our signal, multiply it with our filter, then doing the inverse Fourier's Transform and send it to our speakers.

Well, this is really inefficient. You'll have to use a good FFT implementation if you hope to do some real-time application.

If you understood well those concept, you might have found a better way to do: set you filter's coefficients and take it's time representation with IFT. Doing such a transformation allows us to get the *impulse response* of our filter: the way an impulse evolves temporally through the filter.

So far so good, we avoided a FT on our signal every frame, and we do only one IFT on our filter. In the frequency domain, we did a simple multiplication

(Where x is the input signal, f the filter and w the pulse) How to keep the same transformation in the time domain ? The Fourier's Transform give us another identity

where is the convolution operator defined as follow

We could the take our filters coefficients, IFT to have the time representation, and use it as a convolution product on our signal. That's really better, but, as you can see, for a sample rate of 44100 Hz, the number of samples in our impulse response becomes high and convolution could be expensive.

## FIR Filters

This process is the base of a Finite Impulse Response (FIR) Filter. This kind of filters, as the name suggests, can only act on a duration depending of the number of samples of the impulse response. It's the simple convolution seen before.

A N-order Filter is a filter wich uses at most the sample x(n - N). The more you want a good quality FIR filter, the more the order grows. It can be computationally inefficient for a high quality filter. However, they are numerically stable, and easy to design (ie: it's simple to find impulse response coefficients). FIR filters are a pure digital product.

## IIR Filters

### Definition

We can simulate closely RLC circuits and introduce also some feedback, so that our filters use their own response.

A N-order IIR filter is equivalent to an analog N-order filter, and we can imitate them. the a are called forward coefficients whereas b are feedback coefficients. For an IIR, in audio applications, an IIR filter of order 2 is good, and we can define one of IIR filter categories: Biquad filters.

This representation is called Direct Form I, and uses two buffers, for forward samples and feedback samples. A most convenient representation is Direct Form II

This form is better for floating point operations too. IIR filters are computationally cheap, but now we need a way to design them.

### Design

To design IIR filters, there is two possibilities:

- You are a god, you do not fear maths, and you design your own. In this case, I can't help you since I belong to the 2nd category.
- You aren't, and you'll base your work on previous researches.

There is a lot of "plug and play" filters, Chebyshev's or Butterworth's for instance. Butterworth's filters maybe interesting to know, but you certainly won't use it in your synthesizer (it may be perfectly well-suited for an equalizer). It's main interest is that it's response frequency is as flat as possible in the passband (see article on Wikipedia for more).

#### Low pass filter

Coeff | Lowpass | Highpass |
---|---|---|

- | - | |

On a synthesizer, you can see another parameter, Q factor (or resonance), a little amplification just before the frequency response. In order to design such filters, I found this great blog about audio programming

You can then design a filter using their interactive designer which will give you the coefficients you need.

If you cannot hardcode your coefficients, here are formulas

There are a huge quantity of filter's categories, if you want to study them, musicdsp is a great website.

## Conclusion

We wrote our filters, they sound good, and you already can start to code your own synth, just like I did.

Since I'm a beginner in audio programming, any comment about mistakes, improvements, or even resources about audio DSP in general would be really really really appreciated.

And you can see the implementation of my synthesizer on my Bitbucket profile

## Comments

Oh, cool! This is what I do for one of my jobs. I have a plug-in company that sells audio software, and it's a lot of fun.

Filtering is an extremely interesting topic, as it is a deep rabbit hole of increasingly complicated mathematics and diminishing returns. My best advice towards filters (if you are doing programming) is to learn the basics--many of which are in this article--and to leave it at that. Many great filtering algorithms are easy to find, but difficult to improve upon.

If you absolutely must know more about filters, and aren't afraid of some serious math, it's hard to do better than the (free, online) books of Julius O. Smith, especially this one: https://ccrma.stanford.edu/~jos/fil...

For an interesting free book that goes in depth on filters, check out www.dspguide.com. It covers impulse responses and how awesome they are. It's very approachable and practical, and not mathematically rigorous.