PDA

View Full Version : Bessel filter implementation


Tale
03-21-2011, 02:51 AM
Hi,

I have added a low-pass Bessel filter (besselfilter.h) implementation using matched Z-transform to my WDL repository (http://www.taletn.com/WDL.git). I have extracted this implementation from from the source code of mkfilter (http://www-users.cs.york.ac.uk/~fisher/mkfilter).

I have divided the implementation into three classes: one for calculating coefficients (WDL_BesselFilterCoeffs), one for the actual filter (WDL_BesselFilterStage), and a third class combining both (WDL_BesselFilter).

Here are a couple of (pseudo) code examples of its use:

Example #1:
// 8th order anti-alias filter
#define WDL_BESSEL_FILTER_ORDER 8
#include "besselfilter.h"

int oversampling = 8;

WDL_BesselFilterCoeffs bessel;
WDL_BesselFilterStage filter;

bessel.Calc(0.5 / (double)oversampling);
filter.Reset();

for (int i = 0; i < nFrames; ++i)
{
filter.Process(inputs[0][i], bessel.Coeffs());
outputs[0][i] = filter.Output();
}

Example #2:
#include "besselfilter.h"

int order = 4;
int oversampling = 8;

// 2 cascaded filters
WDL_BesselFilterStage filter[2];
filter[0].Reset();
filter[1].Reset();

WDL_BesselFilterCoeffs coeffs;
coeffs.Calc(0.5 / (double)oversampling, order);

for (int i = 0; i < nFrames; ++i)
{
filter[0].Process(inputs[0][i], &coeffs);
filter[1].Process(filter[0].Output(), &coeffs);
outputs[0][i] = filter[1].Output();
}

Example #3:
#include "besselfilter.h"

int order = 8;
int oversampling = 8;

WDL_BesselFilter bessel;
bessel.Calc(0.5 / (double)oversampling, order);
bessel.Reset();

for (int i = 0; i < nFrames; ++i)
{
bessel.Process(inputs[0][i]);
outputs[0][i] = bessel.Output();
}

junioreq
03-21-2011, 03:01 AM
Tale this is Great! But the first thing that popped out was the oversampling.. Could this be "modded" to work on its own oversampling without the filter? Seems oversampling for distortions etc is just way over my head at the moment. Thanks for the class though, its so nice to have these things built in and ready for use.

~Rob.

olilarkin
03-21-2011, 04:55 AM
wow, thanks for this tale!

rob - the filter is part of the oversampling - you need to filter out frequencies above the nyquist when up or down sampling

Tale
03-26-2011, 03:22 PM
I tried to include besselfilter.h in an Xcode project today, but my MacBook wouldn't build it because _hypot is supposed to be called hypot (although Microsoft seems to disagree). Anyway, this has now been fixed (see my Git repository (http://www.taletn.com/WDL.git)).

Tale
09-18-2011, 06:38 AM
I have added the option to zero denormals in the output of the Bessel filter. I have also added an example using this filter for oversampling:

mAntiAlias.Calc(0.5 / (double)mOversampling);

...

double sample = <input sample>;
double output;

for (int j = 0; j < mOversampling; ++j)
{
// Upsample
if (j > 0) sample = 0.;
mUpsample.Process(sample, mAntiAlias.Coeffs());
sample = (double)mOversampling * mUpsample.Output();

...

// Downsample
mDownsample.Process(sample, mAntiAlias.Coeffs());
if (j == 0) output = mDownsample.Output();
}

<output sample> = output;

The complete (working) example can be found in WDL/IPlug/Example/Distortion in my WDL Git repository.

olilarkin
10-20-2011, 01:42 AM
Do you get a dc offset/clicks when you change the drive amount in your example?

Tale
10-20-2011, 04:55 AM
Do you get a dc offset/clicks when you change the drive amount in your example?
Yes. Adding a small DC offset and then removing it again is part of the distortion algorithm. I remove the DC offset again by sending the downsampled signal through a high-pass filter, which causes the clicks. I guess it would be better to simply substract the distorted DC offset:

// Distortion
if (WDL_DENORMAL_OR_ZERO_DOUBLE_AGGRESSIVE(&sample))
sample = mSilence;
else
sample = mGain * fast_tanh(mDC + mDrive * sample);

// Add this line:
sample -= mSilence;

Of course this can be further optimized, and the high-pass filter can then be removed altogether. It will still use a tiny bit more CPU than the original version, but there will be no more clicks.

EDIT: I have updated the distortion example in my Git repository (currently on the "next" branch).

Mich
11-06-2011, 04:30 AM
I tried to include besselfilter.h in an Xcode project today, but my MacBook wouldn't build it because _hypot is supposed to be called hypot (although Microsoft seems to disagree).

It is supposed to be called hypot() according to the ANSI C99 standard (http://pubs.opengroup.org/onlinepubs/007908799/xsh/hypot.html).
But Microsoft is somewhat confused about it.

onqel
07-24-2012, 02:55 AM
I tried this on one of my high gain ampsims, it can't compete with my original polyphase approach unfortunately (for 2X/4X oversampling), aliasing all over the place.. I tried order up to 10

Tale
07-24-2012, 06:48 AM
Yeah, polyphase should surely give you better quality, but the Bessel filter is probably less CPU costly (I hope so anyway). I use 6 consecutive 4th order Bessel filters with 64x oversampling to suppress aliases on naive waveforms, with virtually no audible aliasing. Then again, polyphase might also work just as well in my specific case, but I haven't been able to really wrap my head around it yet.

random_id
11-30-2012, 07:44 AM
Thank you for your oversampling Distortion sample. I am trying to implement oversampling in my own plugin and I have two questions (due to my extremely small math/dsp brain).

1. When changing oversampling from 4X, 8X, 16X, etc., should you also change the WDL_BESSEL_FILTER_ORDER? In your example, you have this set to 8, and oversampling set to 8X. I think this determines the filter order and would not be related to the oversampling.

2. You mention DC offset. Is this inherent to the oversampling process? I know that you solved it in your example by subtracting out a similar value.

Thanks

Tale
11-30-2012, 08:18 AM
1. When changing oversampling from 4X, 8X, 16X, etc., should you also change the WDL_BESSEL_FILTER_ORDER? In your example, you have this set to 8, and oversampling set to 8X. I think this determines the filter order and would not be related to the oversampling.
The filter order and oversampling are not related. In my example both the oversampling and the filter order happen to be 8, but you could just as well use a filter order of 8 with 16x oversampling (or whatever other combination suits your purpose).

2. You mention DC offset. Is this inherent to the oversampling process? I know that you solved it in your example by subtracting out a similar value.
The DC offset is part of the distortion algorithm, not the oversampling process.

random_id
11-30-2012, 08:40 AM
Thanks for responding to my ignorance.

hannesmenzel
10-25-2018, 09:45 AM
Taking into account that this is a rather old thread, how to implement this oversampling algorithm for multichannel plugins?

Looking into the Saturation Example in WDL-OL I see the plugins class testing for mono input. If I'd like to up- and downsample, let's say, stereo in and out, do I need two filters declared in the header? Am I supposed to process the incoming audio twice, declaring

WDL_BesselFilterCoeffs mAntiAlias1, mAntiAlias2;
WDL_BesselFilterStage mUpsample1, mDownsample1, mUpsample2, mDownsample2;

and then

mUpsample1.Process(sample1, mAntiAlias1.Coeffs());
sample1 = (double)mOversampling * mUpsample1.Output();
mUpsample2.Process(sample2, mAntiAlias2.Coeffs());
sample2 = (double)mOversampling * mUpsample2.Output();


Thanks in advance!

Tale
10-28-2018, 02:51 AM
Looking into the Saturation Example in WDL-OL I see the plugins class testing for mono input. If I'd like to up- and downsample, let's say, stereo in and out, do I need two filters declared in the header?
Yeah, you would need two pairs of up and downsamplers, one pair for each channel.

BTW, for oversampling distortion perhaps a cascaded short FIR filter might me more suited. But you would also need two of those.

hannesmenzel
10-29-2018, 04:24 AM
Yeah, you would need two pairs of up and downsamplers, one pair for each channel.

BTW, for oversampling distortion perhaps a cascaded short FIR filter might me more suited. But you would also need two of those.

Thanks a lot for the answer, will try to implement it in my test plugin.

For the second part of the answer: I'm learning C++ at the moment, my skills are on that level, that I'm totally glad to evaluate pre-written classes and their implementation. But I'll keep your comment in mind until I'll understand more of it.

And thanks for everyone for helping comments out of this community.

hannesmenzel
10-29-2018, 03:18 PM
Would you maybe have a look on the code? I see different output levels depending on the oversampling multiplier.

First the header:



#define WDL_BESSEL_FILTER_ORDER 8
#define WDL_BESSEL_DENORMAL_AGGRESSIVE
#include "../../WDL/besselfilter.h"

//...

int mOversampling;
WDL_BesselFilterCoeffs mAntiAliasL, mAntiAliasR;
WDL_BesselFilterStage mUpsampleL, mDownsampleL, mUpsampleR, mDownsampleR;


Then in the plugins class I have this function which is called from the constructor, Reset() and the OnParamChange(kOversampling):


void SRChannel::InitOversampling() {
mAntiAliasL.Calc(0.5 / (double)mOversampling);
mAntiAliasR.Calc(0.5 / (double)mOversampling);
mUpsampleL.Reset();
mUpsampleR.Reset();
mDownsampleL.Reset();
mDownsampleR.Reset();
}


Then in DoubleReplacing I have this:


for (int j = 0; j < mOversampling; ++j)
{
// Upsample
if (j > 0) {
*out1 = 0.;
*out2 = 0.;
}
mUpsampleL.Process(*out1, mAntiAliasL.Coeffs());
mUpsampleR.Process(*out2, mAntiAliasR.Coeffs());
*out1 = (double)mOversampling * mUpsampleL.Output();
*out2 = (double)mOversampling * mUpsampleR.Output();

// here are my distortion functions, then...

mDownsampleL.Process(*out1, mAntiAliasL.Coeffs());
mDownsampleR.Process(*out2, mAntiAliasR.Coeffs());
if (j == 0) {
*out1 = mDownsampleL.Output();
*out2 = mDownsampleR.Output();
}
}


Finally, OnParamChange in case of my integer kOversampling parameter:


case kOversampling:
mOversampling = int(pow(2, GetParam(paramIdx)->Value() - 1)); // results in 1x, 2x, 4x ...
GetParam(paramIdx)->SetDisplayText(1, "1");
GetParam(paramIdx)->SetDisplayText(2, "2");
GetParam(paramIdx)->SetDisplayText(3, "4");
GetParam(paramIdx)->SetDisplayText(4, "8");
GetParam(paramIdx)->SetDisplayText(5, "16");
InitOversampling(); // calls the function again, see above
break;


By the way, on compiling I get an assert error concerning this one in besselfilter.h:

assert(alpha >= 1e-37 && alpha < 0.5);


Can you or someone who knows tell me, what I am doing wrong?

Cheers and thanks in advance!

Tale
10-30-2018, 02:25 AM
By the way, on compiling I get an assert error concerning this one in besselfilter.h:

assert(alpha >= 1e-37 && alpha < 0.5);

That is because you can't do mAntiAliasL.Calc(0.5 / (double)mOversampling) when mOversampling <= 1, so you should handle mOversampling == 1 differently.

hannesmenzel
10-30-2018, 04:16 AM
That is because you can't do mAntiAliasL.Calc(0.5 / (double)mOversampling) when mOversampling <= 1, so you should handle mOversampling == 1 differently.

Thanks, yes, meanwhile I figured that out. Any ideas what causes the loudness differences in the code? I see no system in it, it's somewhat +3dB when 2x oversampled, but then around -20dB for 4x, then again +2dB for 8x...

Likely that I didn't get the concept of the process. I tried to understand the oversampling implementation of this distortion example. Do I have to do additional interpolation on it?

Tale
11-03-2018, 03:25 AM
Any ideas what causes the loudness differences in the code? I see no system in it, it's somewhat +3dB when 2x oversampled, but then around -20dB for 4x, then again +2dB for 8x...
Hmm, you're right... I don't really have a solution ATM. Sorry!

olilarkin
11-04-2018, 12:26 PM
I never had much luck with the bessel filter oversampler.

Lately i've been using the HIIR library and the iPlug2 i've been working on comes with a helper class and slightly modified HIIR...

here is a zip with all you need:

https://www.dropbox.com/s/5yebb3md2e8jzef/IPlugOversampler.zip?dl=0

blog post about it:

https://www.patreon.com/posts/oversampling-in-17662230

oli