Building a Filter

Brennon Bortz's icon

Hello all,

I'm working through the second volume of Musimathics, by Gareth Loy, and learning quite a lot. I'd like to actually start realizing some of the topics he covers on my own in Max/MSP to help make them more concrete, and I'm having some troubles.

For instance, I'm now reading about filters, and their supporting mathematics. As a rather simple example, I'd like to build my own lowpass filter in Max/MSP. I know how to implement one through the built in filtering objects in Max/MSP, but building one on my own will give me the opportunity to better understand what's really occuring.

He gives the equation for a lowpass filter thus:

y(nT) = x(nT) + x[(n-1)T]

where y(nT) is the input signal, n is an instantaneous sample, and T is time.

His explanation of the operation of this filter makes perfect sense, but realizing it in Max/MSP is giving me problems. I'm sure there's a simple answer, but I'm just not getting it.

How would one realize such an equation? I appreciate your help!

Thanks,
Brennon

Falk's icon

Hi Brennon,

Have a look at the bufir~ object. You can build finite response filters with up to 256 points.There is a patch in the examples folder called buffir-eq-help.pat using bufir~

Here is a patch on convolution I made years ago when I was studying this. You need the lobjects for that.

have fun

falk

Max Patch
Copy patch and select New From Clipboard in Max.

Graham Wakefield's icon

On Jan 4, 2008, at 11:32 AM, Brennon wrote
>
> He gives the equation for a lowpass filter thus:
>
> y(nT) = x(nT) + x[(n-1)T]
>
> where y(nT) is the input signal, n is an instantaneous sample, and
> T is time.
>
> His explanation of the operation of this filter makes perfect
> sense, but realizing it in Max/MSP is giving me problems. I'm sure
> there's a simple answer, but I'm just not getting it.

Well, you'd want to rearrange this to be in terms of the output signal:

x(nT) = y(nT) - x[(n-1)T]

So you subtract a 1 sample delay of the output from the input.
Unfortunately, you can't get a 1 sample delay in a feedback path in a
Max patch. You can use comb~ or biquad~ for example, but I don't
think that's the autodidactic answer you were looking for...

Steven Miller's icon

A couple things:

1) FYI - I recently got an email from Gareth re: the Musimat
programming language he created to demo the ideas in the books. He
gave me some detailed info on getting it to run on Intel Macs. I'll
check with him to make sure it's OK to post the info here, and follow
up accordingly.

2) I believe you have the interpretation of the equation slightly
wrong: y is the output, x is the input. In a nutshell, a given output
sample y(nT) is produced by averaging the current x(nT) and most
previous x((n-1)T) inputs. There should, however, be some
coefficients in there, in this case to multiply each of the two
inputs by .5 before adding them, in order to average them. That's
what simple low-pass filtering is, averaging inputs to generate
outputs. You could achieve this in MSP by using [delay~] set to one
sample delay, and adding that to the direct signal after multiply the
delayed and direct signals by .5. This will then give you the
filtered (averaged) output. It achieves this by simply 'slowing down'
fast-changing signals (i.e. signals with high-frequency components).

Hope this helps.

Max Patch
Copy patch and select New From Clipboard in Max.

On Jan 4, 2008, at 12:32 PM, Brennon wrote:

>
> Hello all,
>
> I'm working through the second volume of Musimathics, by Gareth
> Loy, and learning quite a lot. I'd like to actually start
> realizing some of the topics he covers on my own in Max/MSP to help
> make them more concrete, and I'm having some troubles.
>
> For instance, I'm now reading about filters, and their supporting
> mathematics. As a rather simple example, I'd like to build my own
> lowpass filter in Max/MSP. I know how to implement one through the
> built in filtering objects in Max/MSP, but building one on my own
> will give me the opportunity to better understand what's really
> occuring.
>
> He gives the equation for a lowpass filter thus:
>
> y(nT) = x(nT) + x[(n-1)T]
>
> where y(nT) is the input signal, n is an instantaneous sample, and
> T is time.
>
> His explanation of the operation of this filter makes perfect
> sense, but realizing it in Max/MSP is giving me problems. I'm sure
> there's a simple answer, but I'm just not getting it.
>
> How would one realize such an equation? I appreciate your help!
>

----
Steven M. Miller
Professor, Contemporary Music Program
College of Santa Fe

Home
SFIFEM
Atrium Sound Space
OVOS
CMP

Brennon Bortz's icon

Quote: Falk wrote on Fri, 04 January 2008 12:52
----------------------------------------------------
> Hi Brennon,
>
> Have a look at the bufir~ object. You can build finite response filters with up to 256 points.There is a patch in the examples folder called buffir-eq-help.pat using bufir~
>

I'm starting to see how buffir~ might help, but it's abstracted the problem in such a way that I don't really see how such an equation is really operating in the filter. Is there no way to build such a filter out of "rudimentary" objects?

Brennon Bortz's icon

Quote: lists@grahamwakefield wrote on Fri, 04 January 2008 12:56
----------------------------------------------------
> Unfortunately, you can't get a 1 sample delay in a feedback path in a
> Max patch. You can use comb~ or biquad~ for example, but I don't
> think that's the autodidactic answer you were looking for...
>

So, how are such filters built? In a separate language? If so, what language is common?

Thanks again,
Brennon

Brennon Bortz's icon

Quote: smill wrote on Fri, 04 January 2008 13:13
----------------------------------------------------
> A couple things:
>
> 1) FYI - I recently got an email from Gareth re: the Musimat
> programming language he created to demo the ideas in the books. He
> gave me some detailed info on getting it to run on Intel Macs. I'll
> check with him to make sure it's OK to post the info here, and follow
> up accordingly.
>

That would be wonderful! I've fought with MUSIMAT quite a bit on Mac OS X to no avail. Although it seems that most of the music/DSP-related functions he has created seem relatively easy to implement in another language (classes in Java, etc.), I'd like to be able to play with MUSIMAT, as well. Please send my congratulations to him on these two excellent volumes. I've also struggled my way through the Digital Audio Signal Processing anthology edited by Strawn, and Musimathics has done wonders to fill in the holes for me.

> 2) I believe you have the interpretation of the equation slightly
> wrong: y is the output, x is the input. In a nutshell, a given output
> sample y(nT) is produced by averaging the current x(nT) and most
> previous x((n-1)T) inputs. There should, however, be some
> coefficients in there, in this case to multiply each of the two
> inputs by .5 before adding them, in order to average them. That's
> what simple low-pass filtering is, averaging inputs to generate
> outputs. You could achieve this in MSP by using [delay~] set to one
> sample delay, and adding that to the direct signal after multiply the
> delayed and direct signals by .5. This will then give you the
> filtered (averaged) output. It achieves this by simply 'slowing down'
> fast-changing signals (i.e. signals with high-frequency components).
>
> Hope this helps.
>

Thank you for the patch (I was mistaken in believing that delay~ took as it's argument a number of milliseconds--not samples.) And yes, I did transpose x(n) and y(n)--thank you for drawing my attention to this. Either way, I do understand what the filter is doing, and it's nice to see it in action--even if it is merely a simple example. Just a note, Loy does specify this primitive lowpass filter without averaging (at least at this point in the text).

Thank you again,
Brennon

Steven Miller's icon

On Jan 4, 2008, at 12:56 PM, Graham Wakefield wrote:

>
> Well, you'd want to rearrange this to be in terms of the output
> signal:
>
> x(nT) = y(nT) - x[(n-1)T]
>
> So you subtract a 1 sample delay of the output from the input.
> Unfortunately, you can't get a 1 sample delay in a feedback path in
> a Max patch. You can use comb~ or biquad~ for example, but I don't
> think that's the autodidactic answer you were looking for...

Again, to be correct, x is an input, y is the output, so the
explanation here is a little off. There are, however, filters of this
sort (output = average of input & output), but this isn't the same
topology as the original question, which was an example of a simple
averaging FIR filter. For FIR (finite impulse response) filters, no
feedback is need. IIR filters need feedback.

A good reference for these types of filter equations is Dodge & Jerse
'Computer Music, 2nd Ed.' (pp. 201-219), and/or Roads 'Computer Music
Tutorial' (pp. 397-419).

----
Steven M. Miller
Professor, Contemporary Music Program
College of Santa Fe

Home
SFIFEM
Atrium Sound Space
OVOS
CMP

Steven Miller's icon

On Jan 4, 2008, at 2:42 PM, Brennon wrote:
>
> That would be wonderful! I've fought with MUSIMAT quite a bit on
> Mac OS X to no avail. Although it seems that most of the music/DSP-
> related functions he has created seem relatively easy to implement
> in another language (classes in Java, etc.), I'd like to be able to
> play with MUSIMAT, as well. Please send my congratulations to him
> on these two excellent volumes. I've also struggled my way through
> the Digital Audio Signal Processing anthology edited by Strawn, and
> Musimathics has done wonders to fill in the holes for me.

Agreed! I'll post the info here if/when he gives me the OK.

>
> Thank you for the patch (I was mistaken in believing that delay~
> took as it's argument a number of milliseconds--not samples.) And
> yes, I did transpose x(n) and y(n)--thank you for drawing my
> attention to this. Either way, I do understand what the filter is
> doing, and it's nice to see it in action--even if it is merely a
> simple example. Just a note, Loy does specify this primitive
> lowpass filter without averaging (at least at this point in the text).

You're welcome. See another post I just sent on this thread for other
references to filter equations & examples. Yes, Loy's example does
omit the coefficients, but the classic examples I've seen all use
them in order to do actual averaging rather than simple addition of
successive samples. You can also manipulate the coefficients (as long
as they all add up to 1) to change the characteristics of the filter,
as well.

>

----
Steven M. Miller
Professor, Contemporary Music Program
College of Santa Fe

Home
SFIFEM
Atrium Sound Space
OVOS
CMP

Brennon Bortz's icon

Quote: smill wrote on Fri, 04 January 2008 14:51
----------------------------------------------------
>
> A good reference for these types of filter equations is Dodge & Jerse
> 'Computer Music, 2nd Ed.' (pp. 201-219), and/or Roads 'Computer Music
> Tutorial' (pp. 397-419).
>

I do have the Dodge & Jerse. Thank you for drawing my attention to it again. I had pushed it to to the side when I started into Musimathics. I checked the Roads out of the library several times when I was a student, but never purchased it (quite pricey!) With what I do have handy (Loy, Dodge & Jerse, the Strawn anthology, and Allen Strange's "Electronic Music..."), do you think the Roads will give me much more (at this point?) In which areas might it be stronger?

Thanks,
Brennon

Emmanuel Jourdan's icon

On 4 janv. 08, at 21:38, Brennon wrote:

> So, how are such filters built? In a separate language? If so,
> what language is common?

They are written in C. You can also write them in Java. You'll find an
example of biquad in java in the "mxj~ examples" folder.

HTH,
ej

Steven Miller's icon

In terms of the specific filtering info, I don't think there's much
'value added' in the Roads over the Dodge & Jerse. It is, however, in
general more comprehensive and wide-ranging. I recently heard from
Curtis that MIT Press gave the go-ahead on a new edition of it,
though, so you might want to wait until it's out (he didn't give me a
time frame...). I actually find that both books have strengths and
have slightly different takes on similar material, which is a help in
teaching.

On Jan 4, 2008, at 3:11 PM, Brennon wrote:

> I do have the Dodge & Jerse. Thank you for drawing my attention to
> it again. I had pushed it to to the side when I started into
> Musimathics. I checked the Roads out of the library several times
> when I was a student, but never purchased it (quite pricey!) With
> what I do have handy (Loy, Dodge & Jerse, the Strawn anthology, and
> Allen Strange's "Electronic Music..."), do you think the Roads will
> give me much more (at this point?) In which areas might it be
> stronger?

----
Steven M. Miller
Professor, Contemporary Music Program
College of Santa Fe

Home
SFIFEM
Atrium Sound Space
OVOS
CMP

pvillez@gmail.com's icon

A very nice book and a classic for intro to DSP with Java code in the back is:
Introductory Digital Signal Processing with Computer Applications, by
Lynn and Fuerst

specially if using linear difference equations (as per the low pass
discussed here)

Their example of a low pass and derivatives are very clear and informative.

On 04/01/2008, Steven Miller wrote:
> In terms of the specific filtering info, I don't think there's much
> 'value added' in the Roads over the Dodge & Jerse. It is, however, in
> general more comprehensive and wide-ranging. I recently heard from
> Curtis that MIT Press gave the go-ahead on a new edition of it,
> though, so you might want to wait until it's out (he didn't give me a
> time frame...). I actually find that both books have strengths and
> have slightly different takes on similar material, which is a help in
> teaching.
>
>
> On Jan 4, 2008, at 3:11 PM, Brennon wrote:
>
> > I do have the Dodge & Jerse. Thank you for drawing my attention to
> > it again. I had pushed it to to the side when I started into
> > Musimathics. I checked the Roads out of the library several times
> > when I was a student, but never purchased it (quite pricey!) With
> > what I do have handy (Loy, Dodge & Jerse, the Strawn anthology, and
> > Allen Strange's "Electronic Music..."), do you think the Roads will
> > give me much more (at this point?) In which areas might it be
> > stronger?
>
>
> ----
> Steven M. Miller
> Professor, Contemporary Music Program
> College of Santa Fe
>
> Home
> SFIFEM
> Atrium Sound Space
> OVOS
> CMP
>
>
>

--
A Pereshaped definition of an Apple:
"You will never know what size worm is in your next byte"!

Trond Lossius's icon

For 1st and 2nd order FIR and IIR filters biquad~ is your friend. For
higher order filters there is the possibility of figuring out how to
translate the coefficients so that you can use cascade~.

Or you can make your own filters in C or Java. ASAIR there is a filter
example in the examples for mxj~. Java filters will be a bit less
efficient than filters programmed in C, but it's way simpler to code
audio objects in Java for mxj~, so it could be a good point of
departure. There is also a filter example in the Max SDK.

Tim Place and myself are currently working on a new filter object that
will be a wrapper for all kinds of filters. Once we have it up and
running it should be fairly easy to add more filters to it. It will be
implemented using the TTBlue library and included as part of Jamoma.

Best,
Trond

Brennon wrote:
> Hello all,
>
> I'm working through the second volume of Musimathics, by Gareth Loy, and learning quite a lot. I'd like to actually start realizing some of the topics he covers on my own in Max/MSP to help make them more concrete, and I'm having some troubles.
>
> For instance, I'm now reading about filters, and their supporting mathematics. As a rather simple example, I'd like to build my own lowpass filter in Max/MSP. I know how to implement one through the built in filtering objects in Max/MSP, but building one on my own will give me the opportunity to better understand what's really occuring.
>
> He gives the equation for a lowpass filter thus:
>
> y(nT) = x(nT) + x[(n-1)T]
>
> where y(nT) is the input signal, n is an instantaneous sample, and T is time.
>
> His explanation of the operation of this filter makes perfect sense, but realizing it in Max/MSP is giving me problems. I'm sure there's a simple answer, but I'm just not getting it.
>
> How would one realize such an equation? I appreciate your help!
>
> Thanks,
> Brennon

Brennon Bortz's icon

Quote: Trond Lossius wrote on Mon, 07 January 2008 01:43
----------------------------------------------------
> Or you can make your own filters in C or Java. ASAIR there is a filter
> example in the examples for mxj~. Java filters will be a bit less
> efficient than filters programmed in C, but it's way simpler to code
> audio objects in Java for mxj~, so it could be a good point of
> departure. There is also a filter example in the Max SDK.
>

Would you have any resources to recommend on programming filters in C?

> Tim Place and myself are currently working on a new filter object that
> will be a wrapper for all kinds of filters. Once we have it up and
> running it should be fairly easy to add more filters to it. It will be
> implemented using the TTBlue library and included as part of Jamoma.
>

I look forward to it! Should I just watch maxobjects.com for updates? Or, is there somewhere else I should check?

Thanks for your help,
Brennon

Steven Miller's icon

Hi Maxers,

OK, having got the green light from Gareth to post this, here's some
basic instructions for getting Musimat to run on a Mac. He asked me
to alert folks that a much nicer graphical version, based on XCode on
Intel Macs, will be coming soon. Check the Musimat website for that
.

Best,

Steven

---------------
From Gareth:

Steven,
I got the Musimat tutorial working on my Mac! It was relatively
straightforward. Here's what I did.

o I downloaded Musimat.zip from www.Musimat.com. The Mac unzipped
it automatically and placed it as a folder on my desktop.

o I dragged the folder to be a subdirectory of my username
(garethloy for me), for convenience.

o I had to install the latest development tools from Apple to pick
up the g++ compiler and make program. There's two ways to get these
tools.

1) Via your Mac OS X Install Disk. There should be an Xcode Tools
folder on Install Disc 1. Open it and launch XcodeTools.mpkg. When
you get to the "Installation Type" step in the installation wizard,
click the Customize button. Be sure you get Developer Tools
Software, and either gcc 3.3 or gcc 4.0 (depending on which version
of Mac OS X you have). I don't think you need anything else.

OR

2) Via the Apple Developer Connection. Go to http://
developer.apple.com and click on "Download latest Xcode" under Quick
Links. You have to be a member of ADC, but that's free. Just set up
an account and get the download. Choose the same elements to install
as described above.

o Now you are ready to build and run the Musimat tutorial.

o Launch the Mac terminal window. In the Finder, double-click on
Applications/Utilities/Terminal. This is effectively a UNIX shell.

o cd to the Musimat root directory. In my case it's /Users/
garethloy/Musimat because that's where I placed the Musimat folder
after it was downloaded. A shorthand for this directory is ~/Musimat.

$ cd ~/Musimat

o Issue the make command:

$ make

o Sit back and watch it compile and link... or not. I hit a snag.
It compiled for a while then complained Undefined symbols:
__Unwind_Resume. Google eventually told me that a Makefile was
trying to use gcc instead of g++ to link the compiled objects. The
fix is to edit all files named Makefile and change the line "CC =
gcc" to "CC = g++". You might or might not need to do this depending
upon the version of Xcode tools you are using. I used the vi editor
because I'm familiar with it, but you can also just edit them using
TextEdit.

$ vi ~/Musimat/*/Makefile

o I'll eventually fix the sources at www.Musimat.com to fix this
problem, but it won't be for a while.

o Eventually you will succeed in compiling all the applications.
Files named *.exe are created in each subdirectory of ~/Musimat.

o For example, cd to Musimat/MusimatChapter9 and execute Chapter9.exe.

$ cd ~/Musimat/MusimatChapter9
$ ./Chapter9.exe

o (You have to say ./Chapter9.exe instead of just Chapter9.exe
because ".", the current working directory, is not in the PATH
variable by default.)

o The standard output of this program executes all the functions
discussed in Chapter 9. It goes by rather quickly! To slow it down
and watch it execute, run a debugger, such as gdb. For example,

$ gdb Chapter9.exe

o Type help to get info on gdb. But for starters, try the following
to set a breakpoint in main and then execute that far:

(gdb) help
(gdb) b main
(gdb) run

o You can then single-step the program, examine variables, etc.

(gdb) step
(gdb) list

There are better ways to use gdb than this, such as running it with
Emacs (if you know Emacs). This has the advantage of showing the
source code graphically rather than a line at a time. Another
alternative is to use SlickEdit, which is a terrific IDE/GUI. You
can get a trial for free off the web.

Let me know how it goes. Since you are my first Mac customer, you
can be my guinea pig.

Best,
Gareth

----
Steven M. Miller
Professor, Contemporary Music Program
College of Santa Fe

Home
SFIFEM
Atrium Sound Space
OVOS
CMP

Brennon Bortz's icon

Beautiful! Thanks, Steven!

--Brennon

Trond Lossius's icon

Hi,

for building externals in Max in general the MasMSP SDK is an absolute
must. In addition you need a development environment installed (Xcode on
Mac).

If you have not programmed Max externals before, I would recommend
taking a look at the documentation for how to develop Java code for xj
and mxj~. The code is simpler and less techy to read and understand than
C/C++ code for externals, but still provides a good intro to general
concepts of what an external is made.

If you have not been programming in C or C++ before, I would recommend
that you get yourself a book providing an intruction to the languages. A
good reference is also handy. I don't have particular suggestions for
litterature, but I'm convinced that you'll find plenty at Amazon or
O'Reilly.

And apart from that, source code and projects for other externals that
are somewhat related to what you want to work on yourself is always
instructive.

If you have Xcode, SVN and the MaxMSP SDK installed, you could check out
the active branch of TTblue. If you have no prior experience with SVN,
you could check the Jamoma wiki at www.jamoma.org for information.

The TTBlue repository is available here:

and there's a mailing list for ttblue development:

Best,
Trond

Brennon Bortz wrote:
> Quote: Trond Lossius wrote on Mon, 07 January 2008 01:43
> ----------------------------------------------------
>> Or you can make your own filters in C or Java. ASAIR there is a filter
>> example in the examples for mxj~. Java filters will be a bit less
>> efficient than filters programmed in C, but it's way simpler to code
>> audio objects in Java for mxj~, so it could be a good point of
>> departure. There is also a filter example in the Max SDK.
>>
>
> Would you have any resources to recommend on programming filters in C?
>
>> Tim Place and myself are currently working on a new filter object that
>> will be a wrapper for all kinds of filters. Once we have it up and
>> running it should be fairly easy to add more filters to it. It will be
>> implemented using the TTBlue library and included as part of Jamoma.
>>
>
> I look forward to it! Should I just watch maxobjects.com for updates? Or, is there somewhere else I should check?
>
> Thanks for your help,
> Brennon
> --
> Brennon Bortz
> Composer / Engraver / Nutcase
>

Brennon Bortz's icon

Looks like I just need to check out SVN, then. Thanks for the great advice, Trond!

Best,
Brennon

mortimer59's icon

I have a question, how i Must upload musimath? Is it possible to adapt some bit of code in MaxMsp.

irvn's icon

The best way is simply to use MatLab to generate the coefficients (using the fdatool in their DSP toolbox) and then MAX can process the audio.. Both F I R & I I R filters are available.
MatLab will also process an entire sound file (not real-time) , something MAX doesn't do .. yet.
I had the requirement to use a FIR filter to cut some noise from an field recording, and the EQs in Logic Pro, Soundtools and Ableton were lame.. an FIR can produce some impressive specs, but at 300 -1000 computation per sample, it's not very suitable for live work

Peter McCulloch's icon

True, FIR coefficients can be difficult to compute if you're unfamiliar with the math (but there's also the ifft~ trick: design the desired freq response, then feed it into through ifft~...the output of ifft~ IS the impulse response for the fir filter!), but Max6 and filterdesign can create all sorts of IIR filter shapes such as the Chebychev and Butterworth filters, so you can actually get some quite steep curves. (which can also be cascaded if you need, say, a shelf and a lowpass, etc.)

In short, Max can do much more than it used to, and there's a lot that's worth checking into.