2x oversampled IIR HP/LP filter code?

DSP, Plug-in and Host development discussion.
KVRian
551 posts since 30 Aug, 2012

Post Sat Apr 17, 2021 12:09 pm

I need some simple HP/LP 12dB/Oct filters (similar to RBJs) that are oversampled so as to hold their shape as fc approaches Nyquist. Can someone point me to code for such - or explain the approach to "oversample" inside the filter code? I did it once before but don't recall the tricks so that gain, fc and Q remain as intended.

Any info appreciated!

KVRian
1245 posts since 9 Jan, 2006

Post Sat Apr 17, 2021 12:48 pm

I'm not sure about upsampling "inside" the filter. Typically you would upsample the input signal, filter then down sample.

But it's probably more efficient to use some kind of modified matched z transform filter and get a very closely matched frequency response at the base sample rate. Like this example:

Matched Second Order Digital Filters

KVRian

Topic Starter

551 posts since 30 Aug, 2012

Post Sat Apr 17, 2021 1:45 pm

Thank you for the info and link. It was several years ago when I did the oversampled IIR but I remember it was straightforward and simple - like running the filter twice (with coefficients calculated at 2x samplerate) than summing the 1st and second outputs. I've tried that now but it doesn't seem to be working right, so there's something I'm missing.

KVRian
1245 posts since 9 Jan, 2006

Post Sat Apr 17, 2021 3:00 pm

Something like that should work. Feeding the same sample twice is equivalent to a really low quality low pass filter, so you'll just have to deal with that causing roll off in your passband. You'll also have aliasing in your frequency response. But other than that it should work

KVRian

Topic Starter

551 posts since 30 Aug, 2012

Post Sat Apr 17, 2021 3:25 pm

matt42 wrote:
Sat Apr 17, 2021 3:00 pm
Feeding the same sample twice is equivalent to a really low quality low pass filter, so you'll just have to deal with that causing roll off in your passband. You'll also have aliasing in your frequency response.
LOL - sounds horrible (pun intended). Maybe that's why I abandoned it!

Will study your link. Thank you.

KVRian

Topic Starter

551 posts since 30 Aug, 2012

Post Sun Apr 18, 2021 10:26 am

I remember what I did. It's simply the classic oversampling technique using "zero stuffing".

Run the filter twice (with coefficients calculated at 2 * samplerate) but use 2 * input as the first pass input and 0 as the second pass input. The 2 * input is for gain compensation and 0 input on the second pass (rather than repeating the input sample) eliminates aliasing. That second pass allows the filter to settle to a more accurate end than it would with just a single sample (i.e., it's "oversampled").

I tried it with RBJ filters and it works. AFAIK this technique can be used with nearly any IIR filter (watch out for levels with 2 * input) and can be expanded/looped for any desired integer oversample rate.

Cool. Now I have a lot of other ideas on where to use this....

KVRian
1245 posts since 9 Jan, 2006

Post Sun Apr 18, 2021 10:53 am

It'll work, but aliasing will effect the frequency response. The amount will vary depending of the filter parameters

KVRian

Topic Starter

551 posts since 30 Aug, 2012

Post Sun Apr 18, 2021 11:31 am

matt42 wrote:
Sun Apr 18, 2021 10:53 am
It'll work, but aliasing will effect the frequency response. The amount will vary depending of the filter parameters
Yes...seems I spoke too soon. It works pretty well on LPF but not on HPF.

KVRian
1245 posts since 9 Jan, 2006

Post Sun Apr 18, 2021 12:25 pm

When zero padding, the spectral content of the signal is mirrored above the original nyquist. For decent resampling a steep low pass filter, centred around the nyquist frequency, can be used to remove the mirrored frequencies.

In your case the low pass will act, to some degree, as an anti aliasing filter by removing some of the content above the original nyquist frequency. Whereas the high pass removes non of it, so the frequency response will suffer more as a result

KVRian

Topic Starter

551 posts since 30 Aug, 2012

Post Sun Apr 18, 2021 1:22 pm

matt42 wrote:
Sun Apr 18, 2021 12:25 pm
When zero padding, the spectral content of the signal is mirrored above the original nyquist. For decent resampling a steep low pass filter, centred around the nyquist frequency, can be used to remove the mirrored frequencies.

In your case the low pass will act, to some degree, as an anti aliasing filter by removing some of the content above the original nyquist frequency. Whereas the high pass removes non of it, so the frequency response will suffer more as a result
Yes, I understand. I use oversampling w/FIR methods in other applications but I was hoping there was, perhaps, a shortcut to be found here that would that avoid that complication and FIR latency. I saw a similar "run it twice" approach described in Music DSP but it didn't work well here so I tried to roll my own. Will try some other things.

BTW - I am using RBJ filters - which produce fairly accurate response curves over most of the audio range - but I'm getting some erratic phase shifts at different Fcs which is messing with transient response. Not sure what's causing that. I tried a State Variable algo and got smooth phase response but it had gain drift with Fc, so it's no good either. Any favorite, well-behaved, filter algos you use? I need 2nd order HP and LP with variable Q and Fc.

KVRian
1245 posts since 9 Jan, 2006

Post Sun Apr 18, 2021 2:46 pm

For the best behaved under modulation I'd recommend a zero delay feedback/TPT state variable filter. You could look up The Art of VA filter design , also there's an implementation in the technical papers area of the cytomic website

Return to “DSP and Plug-in Development”