Page 1 of 3
Oversampling Toolkit (Revisited)
Posted: Fri Dec 19, 2014 2:32 am
by MyCo
Hi,
I had a go at the "Oversampling Toolkit" from the SynthMaker forum. I completely rebuilt it from scratch. Instead of using dozens of Biquad-Allpasses, I converted them to higher order Allpasses and optimized the code from there. It didn't get any easier to read, though.
For the Polyphase IIR up/downsamping (That is order: 10, hard slope in my version), I get about 1/3 less CPU time. It's still a bit on the high side, but can't see any chance to get it even lower.
I've also include other order versions with hard/soft slopes that use even less and might be better sometimes.
For test purpose I've also included a decimator effect with x2/x4 oversampling.
Have fun!
Updated version, with Martins and TheOm's suggested optimizations
Re: Oversampling Toolkit (Revisited)
Posted: Fri Dec 19, 2014 2:59 am
by KG_is_back
Did you also tested their magnitude response and SNR? High order IIR filters generally suffer from much more significant rounding errors and sometimes even go unstable because of that. It might just be that you traded speed for precision.
Re: Oversampling Toolkit (Revisited)
Posted: Fri Dec 19, 2014 3:13 am
by MyCo
I've tested the halfband filters separately with white noise and FFT, the response looks right, although I couldn't zoom in. I tried to merge the two parallel halfpass filters into a giant lowpass, but this works only for Orders less then 10. At Order 10 you'll get resonance effect due to rounding errors. As the code was also a lot slower I canceled that approach.
Re: Oversampling Toolkit (Revisited)
Posted: Fri Dec 19, 2014 6:47 am
by martinvicanek
MyCo wrote:I've also include other order versions with hard/soft slopes that use even less and might be better sometimes.
Thanks, MyCo, that looks like a lot of fruitful work. What exactly are hard/soft slopes?
MyCo wrote:[...] I get about 1/3 less CPU time. It's still a bit on the high side, but can't see any chance to get it even lower.
On my machine it's even 50% less.

You could exploit SSE to squeeze out still more (unless you really need to process all 4 channels in parallel). For example reusing the halfband filters for both interpolation and decimation cuts it by another 50% while you still have stereo capacity.

Re: Oversampling Toolkit (Revisited)
Posted: Fri Dec 19, 2014 7:29 am
by MyCo
martinvicanek wrote:What exactly are hard/soft slopes?
Hard Slope drops extremely fast at the half band frequency. Soft Slope roles off smoothly a little bit earlier. The soft slope versions in most cases also attenuate more in the stop band.
martinvicanek wrote:On my machine it's even 50% less.

You can only compare "O10 hard" or "Linear", all other versions didn't exist in the original kit. My selector always chooses the "O10 hard" of the original kit as fallback for the non-existent.
martinvicanek wrote:You could exploit SSE to squeeze out still more (unless you really need to process all 4 channels in parallel)
I didn't want to go that way, because I thought about using it for oscillators in a poly section. And in the mono section I would use it stereo anyway. But an optimized stereo version would make sense, I guess.
martinvicanek wrote:For example reusing the halfband filters for both interpolation and decimation cuts it by another 50% while you still have stereo capacity.

Not sure what you mean with "reusing", but any hint to better performance is appreciated because I'll definetely use the decimator effect in the future, it makes nice dirty sounds

BTW: The original kit has "power series" up/downsampling code. I tried that, but it doesn't look right to me. The second channel doesn't get the right amplitude. But I've no clue, where this comes from, so I couldn't fix it. My thought is that it's supposed to be something like quadric/cubic interpolation, no idea...
Re: Oversampling Toolkit (Revisited)
Posted: Fri Dec 19, 2014 11:35 am
by adamszabo
Cool stuff! Can you also use this on oscillator sync to remove aliasing occurring from that?
@martinvicanek: can you whip up a small demo of the mono version to show what you mean?
Re: Oversampling Toolkit (Revisited)
Posted: Sat Dec 20, 2014 11:16 am
by MyCo
adamszabo wrote:Cool stuff! Can you also use this on oscillator sync to remove aliasing occurring from that?
yeah, but it would make your CPU explode. You need as much oscillators as you oversample, so for 4x upsampling you would need 4 identical oscillators running phase shifted in parallel. You wouldn't get rid of aliasing entirely but it would sound better. Maybe you would loose some higher frequencies too...
Re: Oversampling Toolkit (Revisited)
Posted: Sat Dec 20, 2014 11:30 am
by MyCo
Just an example for generating oversampled naive saw.
Re: Oversampling Toolkit (Revisited)
Posted: Sun Dec 21, 2014 3:25 am
by martinvicanek
MyCo wrote:martinvicanek wrote:For example reusing the halfband filters for both interpolation and decimation cuts it by another 50% while you still have stereo capacity.

Not sure what you mean with "reusing", but any hint to better performance is appreciated
Note that the upsampling filter is the same as (edit: <del>one branch of</del>) the downsampling filter, so I thought it would be straightforward to use it in channelized mode. At second thoght, however, I dropped the idea because it is better to keep the two modules separate. (you might not always want to use them both).
MyCo wrote:An optimized stereo version would make sense, I guess.
Yes, a stereo version of both modules can be obtained by processing even and odd samples simultaneously in parallel SSE channels. I have done this for the second order modules. The efficiency gain is not quite a factor 2, but it is there. Might be more for the higher orders.
adamszabo wrote:@martinvicanek: can you whip up a small demo of the mono version to show what you mean?@martinvicanek: can you whip up a small demo of the mono version to show what you mean?
I also did a mono version of the decimator by channelizing filters 1 and 2 for even and odd samples, respectively. It is a bit tricky, but it works if you can split the filters in two similar chunks (like a section of 2n biquads, for example).
Time to hit the sack.

Re: Oversampling Toolkit (Revisited)
Posted: Sun Dec 21, 2014 4:06 pm
by martinvicanek
@MyCo, there may be more potential for optimization. Observe that the a and b coefficients are equal but in reverse order (as it must be for allpass fiters): a_0 = b_n, a_1 = b_n-1, etc. This is true for both the upsampling and the downsampling filters (the latter have a normalization factor 0.5 which can be applied at the input or output). So instead of calculating
out = b_0*in + b_1*in_1 + ... + b_n*in_n
out = out - a_1*out_1 - ... - a_n*out_n
you could simplify as
out = b_0*(in - out_n) + b1*(in_1 - out_n-1) + ... + b_n-1*(in_n-1 - out_1) + in_n
which reduces the number of multiplications and coefficient loads by 50%. Applying this scheme, I have been able to squeeze out a cycle or two for the 2nd orde filters, but would leave it up to you if you think it worthwhile to try for the higher orders. (You are better at optimizing tha I am.

)