# MSP Filter Tutorial 2: Variable Type Filters

(Difference between revisions)
Jump to: navigation, search

Click here to open the tutorial patch: Media:02fVariableTypeFilters.maxpat

In the first filter tutorial, we introduced some basic filters in MSP. This tutorial looks at two new filter objects: biquad~, which can create any kind of simple filter, and cascade~, which can create and manage multiple independent filters in a single object. We'll also look at a user interface object that helps us edit these filters in an intuitive way.

## Contents

### Biquadratic filters

• Look at the tutorial patcher. Just as in the last tutorial, area `1` consists of a looping playback system that plays a buffer~ using the groove~ object. Turn on the audio, turn up the number box labeled 'Dry Volume', click on a note in the kslider, and make sure you can hear some sound.
• Take a look at area `2` of the tutorial. The sound from the groove~ object feeds into an object called biquad~. Turn down the 'Dry Volume' and turn up the number box labeled 'Biquad Volume'. Click on the different circles in the preset object at the top of patcher aread `2`. Listen to the changes in the sound.

In the last tutorial, we discussed how filters could be expressed as equations, e.g.

yn = 0.5xn + 0.5yn-1

The `0.5` values in the equation above set the respective gains of the different samples used in the filter. If we wanted a more flexible filter, we could generalize this filter so that those numbers are variable, e.g.:

yn = Axn + Byn-1

By modifying the values of `A` and `B`, we could control the frequency response of this filter. While the math behind this operation is beyond the scope of this tutorial, it's generally true that the more energy given to the delayed output sample (the yn-1 term), the smoother the output and the more the high frequencies are supressed.

A fairly standard tactic in digital filter design is to create a filter equation that can perform any kind of standard filtering operation (lowpass, bandpass, etc.) on an input signal. The most common implementation of this is called the biquadratic filter equation (or biquad). It consists of the following equation:

yn = Axn + Bxn-1 + Cxn-2 - Dyn-1 - Eyn-2

This equation uses the incoming sample, the last two incoming samples, and the last two outgoing samples to generate its filter (another term for a biquadratic filter is a two-pole, two-zero filter, because it has four delay coefficients to affect its behavior). By adjusting the five coefficients (A, B, C, D, E), you can generate all manner of filters. The MSP biquad~ object implements a biquadratic filter. Unfortunately, it takes as its input the input signal and the five coefficients that define the filter. What we really want to do, though, is specify our filter not by the equation, but by how we want it to sound.

### The filtergraph~ object

The user-interface object connected to the biquad~ object in the tutorial patcher is called a filtergraph~. It allows us to physically drag around within a box to set parameters of a filter. In addition, you can send messages to a filtergraph~ object to set its parameters, as well as change what kind of filter it shows. Whenever you make a change to the filtergraph~ object, it outputs a set of coefficients as a Max list which the biquad~ object interprets to perform the equivalent filter equation. The message box below the filtergraph~ in the patcher shows us these coefficients.

• Using the umenu object, select a `lowpass` filter from the filtergraph~. Using the mouse, click and drag in the pink region in the middle of the filter response. You can adjust the cutoff frequency of the filter as well as its overall gain. Click on the edges of the pink region and drag inward and outward. This allows you to set the resonance of the filter. The values you are creating (in terms of cutoff frequency, gain, and slope) can be seen in the <link type="refpage" name="number">number box</link> objects to the right of the object.

The filtergraph~ object is capable of graphing and generating coefficients for a wide variety of filter curves, including lowpass, highpass, bandpass, bandstop (or notch), peaknotch (a band filter that can either boost or cut a frequency band), lowshelf / highshelf (filters that can boost or cut frequencies lower or higher than a cutoff), resonant (a bandpass filter where its gain increasing with the Q), and allpass (a filter that changes the phase relationships of frequencies without altering their gains). All of these filters can be modeled by a biquad~ object.

• Acclimatize yourself to the user interface of the filtergraph~ object by selecting different filter types from the umenu and dragging around to create different filters. Note that some filters have inherently more potential gain than others.

### Filters in series: cascade~

Filtering plays a huge role in audio-related creative work, whether we're discussing contemporary music production techniques or avant-garde sound art. It's often the case that a simple filter that only affects one range of frequencies isn't enough to perform the task we want. You can easily chain biquad~ objects (or other filters) by hand in an MSP patcher to create filters operating in series. If you have an idea of how many filters you want to operate in this manner, you can instead use a single object that creates and manages up to 24 different filters arranged in series: cascade~.

• Turn down the 'Biquad Volume' and turn your attention to area `3` of the tutorial patcher. Turn up the 'Cascade Volume' number box and click on the different selections in the preset object. Notice how the sound changes. Using the number box labeled 'Select a filter', send the message `selectfilt 0` to the filtergraph~ in this part of the patcher. Change the number box to `1`, `2`, `3`, and `4`, and see how different component filters within the filtergraph~ highlight themselves. The settings for each of these filters also comes out of the object, setting the <link type="refpage" name="number">number box</link> objects to the right.

As we can see, the filtergraph~ object is capable of maintaining the state of more than one filter and showing us what the frequency response of these filters would be if they were connected in series. The cascade~ object takes a signal in its left inlet and a set of coefficients in its right, interpreting each incoming list as sets of 5 values, one set for each filter.

• Pick a preset setting you like for the cascade~ object. Select a filter to edit with the `selectfilt` message, and drag your mouse in the filtergraph~ to edit the filter. Notice how the filters interact with one another. For example, a lowpass filter set at the same frequency as a highpass filter will cancel one another out. Similarly, a bandpass filter can sit at the 'slope' of a lowpass filter to simulate a resonation point.

If we unlock the patcher and open the Inspector for the filtergraph~ object, we can edit the specific types of filters that it generates coefficients for; in addition, we could change how many filters are maintained and some features of the user interface. By the opposite token, if you'd rather work with filters without a user interface, the MSP object filtercoeff~ takes the same settings as the filtergraph~ object without a graphical component, allowing you to feed coefficients to a biquad~ or cascade~ object without having to graph the filter yourself.

### Summary

The biquad~ object implements a generic filter equation that can model most types of simple filters. Because it expects coefficients for the filter equation, its often useful to use it in conjunction with another object that will compute these numbers. The filtergraph~ object is a user-interface object that lets you visually construct simple and complex filters of many types; the filtercoeff~ object will accomplish the same thing, without the user interface. The cascade~ object acts as a group of biquad~ objects arranged in series, and allows you to easily construct compound filter curves in your patcher.

### See Also

biquad~ - Two-pole, two-zero filter

filtergraph~ - Graphical filter editor

cascade~ - Cascaded series of biquad filters

filtercoeff~ - Signal-rate filter coefficient generator