Namespaces

Variants
Actions

MSP Filter Tutorial 1: Simple Filters

From Cycling '74 Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "Click here to open the tutorial patch: 01fSimpleFilters.maxpat ===Introduction=== This group of tutorials look at different ways to use ''filters'' in MSP. This includes...")
 
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
Click here to open the tutorial patch: [[01fSimpleFilters.maxpat]]
+
Click here to open the tutorial patch: [[Media:01fSimpleFilters.maxpat]]
  
 
===Introduction===
 
===Introduction===
  
This group of tutorials look at different ways to use ''filters'' in MSP.
+
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.
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?===
 
===So what is a filter, anyway?===
  
Filters are just algorithms that alter the frequency spectrum of
+
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.
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
+
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:
signal. If we plot a waveform, we can intuit the visual difference between
+
low frequency and high frequency content:
+
  
 
[[Image:Filterchapter01a.png|border]]
 
[[Image:Filterchapter01a.png|border]]
 +
 
''Two waveforms, one with a low frequency, one with lots of high frequencies''
 
''Two waveforms, one with a low frequency, one with lots of high frequencies''
  
As we can see, the top waveform (stored in the {{maxword|name=buffer~}}
+
As we can see, the top waveform (stored in the {{maxword|name=buffer~}} named <code>lowfrequency</code>, contains a sine wave at 50 Hz. The bottom waveform (in the <code>highfrequency</code> {{maxword|name=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.
named <code>lowfrequency</code>, contains a sine wave at 50 Hz. The bottom
+
waveform (in the <code>highfrequency</code> {{maxword|name=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
+
Let's say that we take a much simpler signal, that of a single sample of <code>1</code> in a sea of <code>0</code>'s. This is called an ''impulse'':
of <code>1</code> in a sea of <code>0</code>'s. This is called an ''impulse'':
+
  
 
[[Image:Filterchapter01b.png|border]]
 
[[Image:Filterchapter01b.png|border]]
 +
 
''An impulse in an audio signal''
 
''An impulse in an audio signal''
  
An impulse has a frequency response equivalent to pure noise...
+
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:
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:
+
  
 
[[Image:Filterchapter01c.png|border]]
 
[[Image:Filterchapter01c.png|border]]
 +
 
''Our impulse, smoothed over one sample''
 
''Our impulse, smoothed over one sample''
  
This has the result of smearing the energy of the impulse
+
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.
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===
 
===Some filter definitions===
  
If we were to generalize what we just did to our impusle
+
If we were to generalize what we just did to our impusle when we smoothed it, we could say this:
when we smoothed it, we could say this:
+
  
 
y<sub>n</sub> = 0.5x<sub>n</sub> + 0.5x<sub>n-1</sub>
 
y<sub>n</sub> = 0.5x<sub>n</sub> + 0.5x<sub>n-1</sub>
  
where '''x''' represents ''incoming'' samples, '''y''' represents
+
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.
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
+
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''.
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:
 
Now consider this equation:
Line 80: Line 45:
 
y<sub>n</sub> = 0.5x<sub>n</sub> + 0.5y<sub>n-1</sub>
 
y<sub>n</sub> = 0.5x<sub>n</sub> + 0.5y<sub>n-1</sub>
  
This filter uses the previous ''outgoing'' sample from the filter
+
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:
as part of the filter itself; by implementing feedback in the filter,
+
we get a much stronger effect:
+
  
 
[[Image:Filterchapter01d.png|border]]
 
[[Image:Filterchapter01d.png|border]]
 +
 
''Our impulse, averaged with the previous output sample''
 
''Our impulse, averaged with the previous output sample''
  
This equation defines a ''recursive'' filter; as a result, the 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:
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:
+
  
 
(x<sub>n</sub> + y<sub>n-1</sub>) / 2 = y<sub>n</sub>
 
(x<sub>n</sub> + y<sub>n-1</sub>) / 2 = y<sub>n</sub>
Line 105: Line 65:
 
and so on...
 
and so on...
  
In the filter described above, the energy of the click, hypothetically,
+
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).
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
+
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.
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: {{maxword|name=lores~}}===
 
===Our first filter: {{maxword|name=lores~}}===
  
Take a look at the tutorial patcher. Patcher area <code>1</code> contains
+
Take a look at the tutorial patcher. Patcher area <code>1</code> contains a simple sampler, playing the ''sacre.aiff'' sound (loaded into a {{maxword|name=buffer~}} named <code>chords</code>) using the {{maxword|name=groove~}} object. The circuit shown in this patch allows us to "play" the sample at any pitch with the {{maxword|name=kslider}}.
a simple sampler, playing the ''sacre.aiff'' sound (loaded into
+
a {{maxword|name=buffer~}} named <code>chords</code>) using the {{maxword|name=groove~}} object.
+
The circuit shown in this patch allows us to "play" the sample at any
+
pitch with the {{maxword|name=kslider}}.
+
  
* Turn on the audio in the patcher by clicking on the {{maxword|name=ezdac~}} object. Adjust the {{maxword|name=number}} box
+
* Turn on the audio in the patcher by clicking on the {{maxword|name=ezdac~}} object. Adjust the {{maxword|name=number}} box labeled 'Dry volume' and play some of the notes on the {{maxword|name=kslider}}. You should hear the sample play at different notes.
labeled 'Dry volume' and play some of the notes on the {{maxword|name=kslider}}. You should
+
hear the sample play at different notes.
+
  
* Turn down the 'Dry volume' and turn up the next {{maxword|name=number}} box,
+
* Turn down the 'Dry volume' and turn up the next {{maxword|name=number}} box, labeled 'Lowpass volume'. Notice the change in sound. Turn the {{maxword|name=dial}} object at the top of patcher region <code>2</code>. As you move the {{maxword|name=dial}} to a higher value, more of the high frequencies from the sample are audible.
labeled 'Lowpass volume'. Notice the change in sound. Turn the {{maxword|name=dial}} object
+
at the top of patcher region <code>2</code>. As you move the {{maxword|name=dial}} to a higher
+
value, more of the high frequencies from the sample are audible.
+
  
The {{maxword|name=lores~}} object implements a ''lowpass'' filter on an incoming audio
+
The {{maxword|name=lores~}} object implements a ''lowpass'' filter on an incoming audio signal (in our case, the output of the {{maxword|name=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).
signal (in our case, the output of the {{maxword|name=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
+
The cutoff frequency of a lowpass filter determines the frequency at which the audio is attenuated 6 dB. The resonance amount, when greater than <code>0.</code>, 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 like this:
audio is attenuated 6 dB. The resonance amount, when greater than <code>0.</code>,
+
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
+
like this:
+
  
 
[[Image:Filterchapter01e.png|border]]
 
[[Image:Filterchapter01e.png|border]]
''A lowpass filter with and without resonance: '''A''' and '''B''' are the
 
cutoff frequencies; '''C''' shows the resonance peak''
 
  
* With the sound going, adjust the {{maxword|name=number}} box labeled 'Resonance' in patcher
+
''A lowpass filter with and without resonance: '''A''' and '''B''' are the cutoff frequencies; '''C''' shows the resonance peak''
area <code>2</code>. Notice how as the resonation approaches <code>1</code> the ringing at the
+
 
resonance frequency becomes very loud. Adjust the cutoff frequency with the
+
* With the sound going, adjust the {{maxword|name=number}} box labeled 'Resonance' in patcher area <code>2</code>. Notice how as the resonation approaches <code>1</code> 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.
resonance set to a high number. Notice how you can now audibly "sweep" the filter
+
based on hearing the resonation.
+
  
 
===Bandpass filters: the {{maxword|name=reson~}} object===
 
===Bandpass filters: the {{maxword|name=reson~}} object===
  
* Turn down the volume of the lowpass filter, and look at section <code>3</code> in the
+
* Turn down the volume of the lowpass filter, and look at section <code>3</code> in the tutorial. Turn up the {{maxword|name=number}} box labeled 'Bandpass volume'. Sweep the {{maxword|name=dial}} labeled 'Center frequency' and listen to the result.
tutorial. Turn up the {{maxword|name=number}} box labeled 'Bandpass volume'. Sweep
+
the {{maxword|name=dial}} labeled 'Center frequency' and listen to the result.
+
  
Just as a lowpass filter passes low frequency, a ''bandpass'' filter passes
+
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 {{maxword|name=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''.
a '''band''' of frequencies, attenuating anything lower or higher than a center
+
frequency. The MSP {{maxword|name=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 {{maxword|name=dial}} controlling the center frequency at 12 o'clock, click in
+
* With the {{maxword|name=dial}} controlling the center frequency at 12 o'clock, click in the {{maxword|name=number}} box labeled 'Q' in patcher area <code>3</code>. Type the number <code>3</code> and hit return. Listen to the results. Try other positive numbers, such as <code>6</code>, <code>10</code>, or <code>0.5</code>. Notice that the higher the number is, the less frequencies make it through the filter. If necessary, adjust the 'Gain' with the {{maxword|name=number}} box to the left.
the {{maxword|name=number}} box labeled 'Q' in patcher area <code>3</code>. Type the number <code>3</code>
+
and hit return. Listen to the results. Try other positive numbers, such as <code>6</code>,
+
<code>10</code>, or <code>0.5</code>. Notice that the higher the number is, the less
+
frequencies make it through the filter. If necessary, adjust the 'Gain' with
+
the {{maxword|name=number}} box to the left.
+
  
Q is a measure of the ''width'' of a bandpass filter, and is expressed as the
+
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):
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):
+
  
 
[[Image:Filterchapter01f.png|border]]
 
[[Image:Filterchapter01f.png|border]]
 +
 
A bandpass filter with Q values of <code>0.5</code>, <code>1.0</code>, <code>3.0</code>, and <code>30.0</code>, respectively.
 
A bandpass filter with Q values of <code>0.5</code>, <code>1.0</code>, <code>3.0</code>, and <code>30.0</code>, respectively.
  
 
===The state-variable filter: {{maxword|name=svf~}}===
 
===The state-variable filter: {{maxword|name=svf~}}===
  
* Turn down the 'Bandpass volume' and look at patcher area <code>4</code>. Turn up
+
* Turn down the 'Bandpass volume' and look at patcher area <code>4</code>. Turn up the {{maxword|name=number}} box labeled 'Lowpass', adjust the 'Cutoff/Center Freq.' {{maxword|name=dial}}, and set the 'Resonance' {{maxword|name=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 {{maxword|name=number}} box labeled 'Lowpass', adjust the 'Cutoff/Center Freq.' {{maxword|name=dial}},
+
and set the 'Resonance' {{maxword|name=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 {{maxword|name=svf~}} object simulates an analogue ''state-variable'' filter.
+
The MSP {{maxword|name=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 {{maxword|name=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:
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 {{maxword|name=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:
+
  
 
[[Image:Filterchapter01g.png|border]]
 
[[Image:Filterchapter01g.png|border]]
 +
 
The outputs of a state-variable filter: lowpass, highpass, bandpass, notch.
 
The outputs of a state-variable filter: lowpass, highpass, bandpass, notch.
  
* Play with different combinations of the filters in the tutorial, mixing
+
* 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.
them in different ways with different settings. In the next tutorial, we'll
+
look at building more complex filter arrangements.
+
  
 
===Summary===
 
===Summary===
  
In digital signal processing, ''filters'' refer to equations which
+
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 {{maxword|name=lores~}} object, bandpass filters with the {{maxword|name=reson~}} object, and all four with the {{maxword|name=svf~}} object. Filters commonly have controls for their center or cutoff frequency and their Q or resonance.
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 {{maxword|name=lores~}} object, bandpass filters with
+
the {{maxword|name=reson~}} object, and all four with the {{maxword|name=svf~}} object. Filters
+
commonly have controls for their center or cutoff frequency and their Q or
+
resonance.
+
  
 
===See Also===
 
===See Also===

Latest revision as of 15:30, 28 June 2012

Click here to open the tutorial patch: Media:01fSimpleFilters.maxpat

Contents

[edit] Introduction

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.

[edit] 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:

Filterchapter01a.png

Two waveforms, one with a low frequency, one with lots of high frequencies

As we can see, the top waveform (stored in the buffer~ named 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 of 1 in a sea of 0's. This is called an impulse:

Filterchapter01b.png

An impulse in an audio signal

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:

Filterchapter01c.png

Our impulse, smoothed over one sample

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.

[edit] 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:

Filterchapter01d.png

Our impulse, averaged with the previous output sample

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.

[edit] Our first filter: lores~

Take a look at the tutorial patcher. Patcher area 1 contains 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.

  • Turn on the audio in the patcher by clicking on the ezdac~ object. Adjust the number box 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 0., 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 like this:

Filterchapter01e.png

A lowpass filter with and without resonance: A and B are the cutoff frequencies; C shows the resonance peak

  • With the sound going, adjust the number box labeled 'Resonance' in patcher area 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.

[edit] Bandpass filters: the reson~ object

  • Turn down the volume of the lowpass filter, and look at section 3 in the tutorial. Turn up the number box labeled 'Bandpass volume'. Sweep the dial labeled 'Center frequency' and listen to the result.

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 3 and hit return. Listen to the results. Try other positive numbers, such as 6, 10, or 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):

Filterchapter01f.png

A bandpass filter with Q values of 0.5, 1.0, 3.0, and 30.0, respectively.

[edit] 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:

Filterchapter01g.png

The outputs of a state-variable filter: lowpass, highpass, bandpass, notch.

  • 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.

[edit] Summary

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.

[edit] See Also

lores~ - Resonant lowpass filter

reson~ - Resonant bandpass filter

svf~ - State-variable filter with simultaneous outputs