DSP, Plug-in and Host development discussion.
KVRAF
1605 posts since 4 Dec, 2006
Filters are a weakness of mine. When I need one, my first stop is musicdsp.org. I plug in the code and go from there. I'm trying to get more of an intuitive understanding of filters, i.e. I don't mind the theory and even welcome it, but would like to get my hands dirty along with developing my theoretical knowledge.

I've gone through the archives here and found a lot of good posts, but I still have questions.

I was looking at Chamberlain's Musical Applications of Microprocessors tonight and was studying his section on filters. There's a diagram of a digital integrator - it simply takes the previous output, adds it to the input, and stores the results. There's a couple of graphs showing the results of running sine waves through it.

Can a digital integrator be thought of as a filter?

Next Chamberlain describes the RC circuit. I'm familiar with this in that I've used it to create my ADSR envelope. From the diagram in Chamberain, the digital version looks like a digital integrator with the input run through an amplifier. The delayed output is also run through an amplifier before being summed with the amplified input. In digital terms, the amplifiers simply multiply their inputs. Programmically, it looks like this:

Code: Select all

``````output = a0 * input + b1 * output;
``````
a0 and b1 are the coefficients. They represent the amplifiers in the diagram.

Is it possible to add resonance to the above filter? If so, where?

I was looking at the diagram for the state variable filter, and it looks like two cascaded digital integrators with an amplifier placed between them and one placed before the first one. There are also other elements on the diagram as well. As I understand it the state variable filter is a two pole filter.

I see where the resonance is coming from; it's the output of the first digital integrator fed through an amplifier and subtracted from the input.

Anyway, it could be that I'm still in over my head with this stuff and need to do a lot more homework, but I think I'm making progress. My goal is to reach a point where I may not have a complete command over the theory but know at least enough to play with a 2 pole filter with resonance and have a halfway decent idea of what I'm doing.

Thanks for any help.

KVRAF
3080 posts since 17 Apr, 2005 from S.E. TN
State Variable filter came from analog, and it is a very cunning circuit.

There is deep mathematical understanding that any decent engineer should have, and then there is a 'seat of the pants' understanding, which is more my speed.

I think you need at least a second-order filter to get resonance. The first-order integrator can't conveniently have resonance I think.

If you have a first-order low-pass filter (which could be called a 'leaky integrator'), or a perfect integrator that doesn't leak, you get a -90 degree phase shift at very high frequencies, and a 0 degree phase shift at zero frequency. The Phase Shift is -45 degrees at the center frequency of the filter.

When you cascade two integrators, the center frequency has -90 degree phase shift, and the phase shift at very high freqs approaches -180 degrees.

So if you add feedback around the two phase shifters, the phase-shifted feedback will begin to reinforce the non-phase-shifted input signal near the center freq of the filter. So you get a gain-bump near the center frequency. That is one seat-of-the-pants way to visualize where the resonance comes from.

Just as an aside, in early days I played with Chamberlin's digital SVF a lot, because I was so accustomed to using analog SVF's. Analog SVF's behave just so nicely. Almost a cunningly ideal circuit.

But Chamberlin's SVF doesn't have the good generic behavior of the analog SVF, if the Chamberlin SVF is used with a center freq above SampleRate / 8. And if you want textbook behavior, it is more advisable to stay below SampleRate / 16.

That isn't a slam at Chamberlin, who is a gifted engineer and I'm just a dullard dabbler. You can crank up the Q of a Chamberlin SVF and get wah-filter special FX higher than those limits. But an analog SVF has some special properties, and a naive Chamberlin SVF won't give you all the special properties except at fairly low freqs. You could oversample the SVF and overcome those issues, but at that point, an un-oversampled RBJ cookbook filter will use less CPU for equivalent performance, than a tweaky-oversampled SVF.

I only offer that advice in case you might be lured into spending months trying to workaround limitations of the digital SVF. Folks have done that already (GRIN).

KVRAF
12422 posts since 7 Dec, 2004
you should simplify the function to

b += r * (in * gain - b)

since this actually is what happens in pretty well all the filters you're interested in. gain should be 1 for again pretty much every filter you'll be interested in, so we can remove that.

this is the basic lossy integrator. it is referred to sometimes as 'r-c'.. although that doesnt make sense if you're talking about the math. we're not concerned about temperature coefficients or thermal noise or any of that in software. (if you are, it's best emulated at a macro level)

so the basic lossy integrator is

b += r * (in - b);

the function should be obvious, we integrate the difference between the current state of the integrator and the input scaled by some fractional amount. so, we're adjusting the rate of change in the integrator. we're also integrating not simply the input, but the difference between the input and the integrator buffer, meaning that the closer we get to the input the slower we move toward it, never actually reaching the input.

you need to learn two things about this function, one is that we're scaling the rate of change and therefore removing high frequencies, or fast changes. the other is we've shifted the phase of the signal by 45 degrees at the "cutoff" point, which is the -3db point in amplitude vs. frequency. below that we get less phase shift, toward zero. above we get more, toward 90.

you should know that in order to invert the signal we need 180 degrees of shift. in order for this to happen we can apply two lossy integrators in series.

the most basic oscillating filter made from lossy integrators is then:

Code: Select all

``````f = 1.0 - exp(-2*pi * hz/rate);
q = feedback + feedback / (1.0 - f);

a += f * ((in - a) + q * (a - b));
b += f * (a - b);
``````
the multimode or state-variable filter is:

Code: Select all

``````f = 2*pi * hz/rate;
q = 2.0 - 2.0*max(f * (2.0 - f), feedback);

h = in - (l + q * b);
b += f * h;
b = sat(b, r); //clip to the range you want it to oscillate at
l += f * b;
``````
here we're doing pretty much the same thing, only we're not having the issue of our feedback being scaled down by f like in the first one. we use local feedback around one of the stages (the first, or b in this case) to cancel out the global feedback rather than adjusting the level of global feedback. you can change the order of stages or insert more and form complex filters using the multimode model. exactly where i've clipped and the type of feedback compensation i've used are just my preference for approximations and are not "correct". if you run the multimode code twice you can get a full frequency range up to nyquist,

KVRian
1153 posts since 10 Dec, 2003
http://yehar.com/dsp/dspstuff.txt

good basic intro

http://www.tobybear.de/p_filterexp.html

good for visualizing how poles & zeros relate to frequency response. Also good for seeing why sweepable filters are tricky to design. You have to find a functions that map the frequency response + parameters to pole/zero position.

http://ccrma.stanford.edu/~jos/mdft/mdft.html

Specificaly the sections on complex numbers, sinusoid & exponentials, and Eulers identity. A good understanding of that stuff is pretty essential i think.

http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt

That gives you an idea of the start and end point of IIR filter design using the BLT. It skips all the heavy math.

One thing worth mentioning is that when they talk of an 'analog prototype' or converting an analog filter, they dont usualy mean an actual electonic analog filter, they mean a continuous function. An equation that represents the required frequency response. And then they use somthing like a BLT to map that to a discrete version.

Anaog in this sense means continuous as apposed to discrete.

KVRAF

Topic Starter

1605 posts since 4 Dec, 2006
JCJR wrote: If you have a first-order low-pass filter (which could be called a 'leaky integrator'), or a perfect integrator that doesn't leak, you get a -90 degree phase shift at very high frequencies, and a 0 degree phase shift at zero frequency. The Phase Shift is -45 degrees at the center frequency of the filter.

When you cascade two integrators, the center frequency has -90 degree phase shift, and the phase shift at very high freqs approaches -180 degrees.

So if you add feedback around the two phase shifters, the phase-shifted feedback will begin to reinforce the non-phase-shifted input signal near the center freq of the filter. So you get a gain-bump near the center frequency. That is one seat-of-the-pants way to visualize where the resonance comes from.
Ah, I see. This makes sense. I didn't know about the phase releationships. This gives me a deeper understanding of resonance. Thanks.

KVRAF

Topic Starter

1605 posts since 4 Dec, 2006
aciddose, this was a tour de force reply you gave. I really appreciate it. I printed out your reply and had several "Aha!" moments while studying it. It's helped my understanding a lot. Thank you.

KVRAF

Topic Starter

1605 posts since 4 Dec, 2006
Thanks, nollock, for the sources. I will take a look. With what I've learned in this thread, I feel I have some basic knowledge that will keep my feet on the ground as I delve deeper into the theory.

And thanks again to everyone who contributed to this thread. This place is incredibly friendly and helpful.