MSP Filter Tutorial 1: Simple Filters
Click here to open the tutorial patch: 01fSimpleFilters.maxpat
This group of tutorials look at different ways to use filters in MSP. This includes the basic uses of filters for the equalization and shaping of a sound and using filters to create timbres in subtractive synthesis. Along the way, we'll look at some of the theory behind filters and how they work.
So what is a filter, anyway?
Filters are just algorithms that alter the frequency spectrum of a sound. When working with digital audio in the time domain (i.e. as a stream of samples representing the amplitude of a wave), filters are implemented as equations that use short delays to shape an incoming waveform.
As an example, let's say we wanted to roll off the treble on an audio signal. If we plot a waveform, we can intuit the visual difference between low frequency and high frequency content:
As we can see, the top waveform (stored in the buffer~
lowfrequency, contains a sine wave at 50 Hz. The bottom
waveform (in the
highfrequency buffer~) contains a
complex FM tone with lots of high frequencies. If we wanted to roll
off the treble on the bottom waveform, we could think of how it looks:
high frequencies look like sharper angles when plotted in time. In
order to cut high frequencies, we could smooth this waveform.
One way to smooth a signal is to average it over time.
Let's say that we take a much simpler signal, that of a single sample
1 in a sea of
0's. This is called an impulse:
An impulse has a frequency response equivalent to pure noise... hypothetically, all frequencies are present at equal volume (think of a 'click' in a digital audio signal or any other short burst of sound). So it contains plenty of high frequencies. If we wanted to smooth this signal, we could average each sample with the previous sample in this signal:
This has the result of smearing the energy of the impulse across two samples. As a result, its frequency response will contain much less high-frequency energy; in fact, it's almost as if we've lowered the sampling rate: a click that lasts one sample at 44,100 Hz contains energy all the way up to 22,050 Hz; by derivation, a click that lasts two samples at that sampling rate is the same as a one-sample click at half that rate, i.e. it only has energy up to 11,025 Hz.
Some filter definitions
If we were to generalize what we just did to our impusle when we smoothed it, we could say this:
yn = 0.5xn + 0.5xn-1
where x represents incoming samples, y represents outgoing samples, and n represents the current time on the sample clock (i.e. now). This equation defines the filter: we're averaging (multiplying by 0.5) the current and previous incoming samples to generate the outgoing samples.
To put a name on this filter, we could call it a first-order non-recursive lowpass filter. The order of a filter refers to how many samples of delay it contains: because we're only looking at one previous input, it's a first-order filter. Because the filter only uses incoming samples in its equation, it's non-recursive. As for what it does, it passes low frequencies (and cuts high ones): hence the term lowpass.
Now consider this equation:
yn = 0.5xn + 0.5yn-1
This filter uses the previous outgoing sample from the filter as part of the filter itself; by implementing feedback in the filter, we get a much stronger effect:
This equation defines a recursive filter; as a result, the effect of the filter is dissipated beyond the order of the filter. While our first equation spread the energy of our one-sample click over two samples, this new equation spreads the energy over many, because of the averaging. Consider how the click interacts with the equation:
(xn + yn-1) / 2 = yn
(1.0 + 0.0) / 2 = 0.5
(0.0 + 0.5) / 2 = 0.25
(0.0 + 0.25) / 2 = 0.125
(0.0 + 0.125) / 2 = 0.075
and so on...
In the filter described above, the energy of the click, hypothetically, will never fully dissipate. Another term for this kind of filter is an IIR, or infinite impulse response, filter; our first filter, which only uses incoming samples in its terms, has a finite impulse response (an FIR filter).
In a later tutorial, we'll revisit some more filter theory. For now, it's simply important to understand that filters are made by manipulating very short (often single sample) delays (either with or without feedback) and mixing them with the current sample.
Our first filter: lores~
Take a look at the tutorial patcher. Patcher area
a simple sampler, playing the sacre.aiff sound (loaded into
a buffer~ named
chords) using the groove~ object.
The circuit shown in this patch allows us to "play" the sample at any
pitch with the kslider.
labeled 'Dry volume' and play some of the notes on the kslider. You should hear the sample play at different notes.
- Turn down the 'Dry volume' and turn up the next number box,
labeled 'Lowpass volume'. Notice the change in sound. Turn the dial object
at the top of patcher region
2. As you move the dial to a higher
value, more of the high frequencies from the sample are audible.
The lores~ object implements a lowpass filter on an incoming audio signal (in our case, the output of the groove~ object. A lowpass filter, as we saw in the tutorial introduction, passes the low frequencies and attenuates the high frequencies of the incoming signal. The two parameters that the filter takes are the cutoff frequency (specified in the middle inlet or as the first argument to the object) and the resonance (specified in the right inlet or as the second argument).
The cutoff frequency of a lowpass filter determines the frequency at which the
audio is attenuated 6 dB. The resonance amount, when greater than
controls a peak of resonation (boosted frequencies) immediately below the cutoff.
If we plot the response of the filter on a graph with the X axis
representing frequency and the Y axis representing gain, it would look
- With the sound going, adjust the number box labeled 'Resonance' in patcher
2. Notice how as the resonation approaches
1 the ringing at the
resonance frequency becomes very loud. Adjust the cutoff frequency with the
resonance set to a high number. Notice how you can now audibly "sweep" the filter
based on hearing the resonation.
Bandpass filters: the reson~ object
- Turn down the volume of the lowpass filter, and look at section
Just as a lowpass filter passes low frequency, a bandpass filter passes a band of frequencies, attenuating anything lower or higher than a center frequency. The MSP reson~ object implements a bandpass filter with three parameters (controllable as inlets or arguments): the filter's gain, the center frequency, and something called the Q.
- With the dial controlling the center frequency at 12 o'clock, click in
the number box labeled 'Q' in patcher area
3. Type the number
and hit return. Listen to the results. Try other positive numbers, such as
0.5. Notice that the higher the number is, the less
frequencies make it through the filter. If necessary, adjust the 'Gain' with
the number box to the left.
Q is a measure of the width of a bandpass filter, and is expressed as the ratio of the center frequency divided by the bandwidth of the filter, i.e. the distance (in Hz) between the lower and higher -6 dB points around the center frequency. Higher Q values mean narrower filters (less bandwidth relative to the center frequency):
The state-variable filter: svf~
- Turn down the 'Bandpass volume' and look at patcher area
4. Turn up
the number box labeled 'Lowpass', adjust the 'Cutoff/Center Freq.' dial, and set the 'Resonance' number box to something that sounds good to you. Now, turn down the 'Lowpass' control and raise the 'Highpass'. Notice the difference. Do the same with the <link type="refpage" name="number">number box</link> objects labeled 'Bandpass' and 'Notch'. Play with different combinations of volumes and settings.
The MSP svf~ object simulates an analogue state-variable filter. Because of the way in which filters are wired using electronic components, the difference between one type of filter and another is often simply a matter of how you wire (or where you 'tap') the circuit. A state-variable filter is a filter that allows you to 'tap' energy from several places in the filter, getting four simultaneous filters for the price of one. The svf~ object gives you four filtered sounds: a lowpass output (which cuts frequencies above the cutoff frequency), a highpass output (which cuts frequencies below the cutoff), a bandpass output (cutting frequencies around the center frequency), and a notch output. The notch output cuts the area directly around the center frequency and should mirror the response of the equivalent bandpass filter. Notch filters are often called bandstop or bandreject filters. A plot of these possibilities shows their frequency responses:
- Play with different combinations of the filters in the tutorial, mixing
them in different ways with different settings. In the next tutorial, we'll look at building more complex filter arrangements.
In digital signal processing, filters refer to equations which modify the frequency response of a signal. Filters are constructed by mixing small amounts of delayed signal with the original, smoothing or sharpening the waveform to accentuate or attenuate different frequencies. Common filter types include lowpass, highpass, bandpass, and notch. Lowpass filters can be created with the lores~ object, bandpass filters with the reson~ object, and all four with the svf~ object. Filters commonly have controls for their center or cutoff frequency and their Q or resonance.
lores~ - Resonant lowpass filter
reson~ - Resonant bandpass filter
svf~ - State-variable filter with simultaneous outputs