Author |
Message |
synaesthesia
Joined: May 27, 2014 Posts: 291 Location: Germany
Audio files: 85
|
Posted: Sat Mar 11, 2017 10:12 am Post subject:
Simple resonant low pass filter Subject description: using only 16-bit integer arithmetic |
|
|
Does anybody know a good resonant low-pass filter using only 16-bit integer arithmetic? The code below is what I currently use.
It sounds quite good already, but it needs a bit of taming (the division by 3) because it otherwise easily overflows during 16-bit calculations.
Code: |
// in the main loop
// get unsigned values for fcut and qfilter in range 25..250
feedback = qfilter + ((qfilter*128)/(256-fcut));
// in the interrupt, use input value in range 0..255
int sample = (input-128)/3; // shift to -128..127 and scale by /3
// buf0 and buf1 are static integers
buf0 = buf0 + (fcut*((sample-buf0) + (feedback*(buf0-buf1)/256))/256);
buf1 = buf1 + (fcut*(buf0-buf1)/256);
int output = (buf1+128)&255; // shift signed result back into range 0..255
|
Instead of the division by 256, a shift right by 8 bits can be used of course, but it must be an arithmetic shift to preserve the sign bit. |
|
Back to top
|
|
|
JovianPyx
Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Sat Mar 11, 2017 3:00 pm Post subject:
|
|
|
What CPU?
Take a look at dspic33FJ128GP802. It's a 16 bit CPU that runs as fast as 40 MIPS. That particular part number is for the 28 pin DIP package, so it's quite DIY friendly. I've written a state variable filter for it in assembly language and it works wonderfully well. The filter I wrote also uses the full 16 bits of range for data which is -32768 to +32767. Using Q values higher than 1.0 requires some attenuation of the input signal. There is information about this filter here: http://www.earlevel.com/main/2003/03/02/the-digital-state-variable-filter/ _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
|
synaesthesia
Joined: May 27, 2014 Posts: 291 Location: Germany
Audio files: 85
|
Posted: Tue Mar 21, 2017 11:28 am Post subject:
|
|
|
I am using a PIC24FJ64GA002 right now, but I am looking for generic code that can run on any controller without foating point support.
By the way, I had to change the scaling to /4 because even with an input scaled by /3 the filter sometimes overflows the internal values, for example when an envelope with a very steep attack phase is used. |
|
Back to top
|
|
|
VA1
Joined: Aug 20, 2018 Posts: 98 Location: Nederland
|
Posted: Wed Aug 29, 2018 3:02 am Post subject:
|
|
|
You mean with resonance the overflow occurs.
I heard a FIR filter will stay in limit, IIR is problems.
FIR has more delay and more CPU.
i dont know how FIR behaves with resonance ?
this is always a big problem, some filters has it covered somehow, still finding out how.
i am no filter expert.
You should adjust levels per project, lower the max reso and so on to get some results.
My filter adjust levels per waveform, i have the same problem.
Maybe you can try http://www.microchip.com/forums/m1003759.aspx |
|
Back to top
|
|
|
JovianPyx
Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Wed Aug 29, 2018 8:39 am Post subject:
|
|
|
IIR filters like the SVF have a property called "Q enhancement". This means that as the Q is raised, the amplitude of the signal at Fc also increases. The gain of the system at Fc becomes greater than 1.0 as Q is increased. This can easily cause overflow. Attenuation is the only way to handle this. You may be able to handle this automatically with a table that relates Q values back to an attentuation value. This can be tricky to handle with fixed point (integer based) arithmetic and still maintain a signal that is close to saturation. _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
|
VA1
Joined: Aug 20, 2018 Posts: 98 Location: Nederland
|
Posted: Mon Oct 22, 2018 8:15 am Post subject:
|
|
|
My filters make weird beeping noises sometimes, you can hear it when no audio is playing, like it hangs,
when you press note it can be gone.
These sounds are also hearable very soft in the audio.
What is this with my IIR filter ?
Normally for float on PC you have to normalize ( remove small values ),
i dont have this in my filter, mabe its needed somehow ?, i dont wanto waste a if. |
|
Back to top
|
|
|
blue hell
Site Admin
Joined: Apr 03, 2004 Posts: 24079 Location: The Netherlands, Enschede
Audio files: 278
G2 patch files: 320
|
Posted: Mon Oct 22, 2018 9:20 am Post subject:
|
|
|
Normalization is for floats only .. and only on some floating point units (like the 8087 stuff on a PC which gets very slow for denormal numbers), it is not needed for fixed point arithmetic, as there are no denormal numbers in fixed point. _________________ Jan
also .. could someone please turn down the thermostat a bit.
|
|
Back to top
|
|
|
VA1
Joined: Aug 20, 2018 Posts: 98 Location: Nederland
|
Posted: Tue Oct 23, 2018 5:30 am Post subject:
|
|
|
Thanks, i,m still figuring out why my filter is so noisey. |
|
Back to top
|
|
|
|