Anti-Aliasing files for sampler playback

DSP, Plug-in and Host development discussion.
User avatar
SNFK
KVRist
42 posts since 8 Nov, 2020

Post Thu Oct 07, 2021 9:18 am

A wavetable synth is pretty straightforward: you have a 2048 length buffer and use FFT to anti-alias it to fit the pitch, and then you oversample it to get ride of any remaining artifacts.

Let’s say that instead of a short waveform, I were to use a 3 second file as my “waveform”. You’ll need to do some sort of anti-aliasing if you play a note high enough. If the sample rate is 44.1k, the file is roughly 132,000 samples long and FFT will not work. You could use oversampling, but to completely remove the aliasing it would require a lot of cpu power.

So how should this be done with FFT? Splitting the sample up causes me phase issues. Oversampling with HIIR would cause to much cpu. Any ideas?

rafa1981
KVRian
662 posts since 4 Jan, 2007

Post Thu Oct 07, 2021 10:17 am

Total newbie here, so total naive solution. The signal is already bandlimited and you know how much frequency headroom you require, so you could just brickwall lowpass and then transpose.

mystran
KVRAF
6720 posts since 12 Feb, 2006 from Helsinki, Finland

Post Thu Oct 07, 2021 1:32 pm

SNFK wrote:
Thu Oct 07, 2021 9:18 am
So how should this be done with FFT? Splitting the sample up causes me phase issues. Oversampling with HIIR would cause to much cpu. Any ideas?
The FFT trick (ie. zero out high harmonics) works because the wavetables are periodic single-cycles, but that approach doesn't work for anything else. For non-periodic audio, you need to filter with an actual brickwall lowpass (eg. windowed sinc or whatever).

You can still precompute multiple versions similar to what is done with wavetables, you just need to use a proper filter... although the filtering itself can be done using FFT convolution if desired (eg. search the web for "overlap add" or something).

Oversampling on it's own doesn't do you any good, it just makes it easier to interpolate without introducing additional aliasing once your source is already band-limited.
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

User avatar
SNFK
KVRist

Topic Starter

42 posts since 8 Nov, 2020

Post Fri Oct 08, 2021 6:20 am

For non-periodic audio, you need to filter with an actual brickwall lowpass (eg. windowed sinc or whatever).
What would you recommend? I saw somewhere on this forum a while ago that Chebyshev 2 filter would work well, but others also said a sinc FIR filter would work well too. It wouldn’t need to be a perfect brickwall low pass because then I could oversample the little artifacts off…?

rafa1981
KVRian
662 posts since 4 Jan, 2007

Post Fri Oct 08, 2021 8:56 am

The point is that for oversampling you are going to zero stuff, interpolate (brickwall lowpass) and then lowpass (another brickwall) and then drop samples. So you have two brickwalls involved and if the filters are polyphase both brickwalls will work at the base rate.

To me it seems that the advantages of this vs a single brickwall before transposing are:

-There is no coefficient recalculation, should you use FIR.
-The cutoff will be set higher around FS/2. On the single lowpass version the higher octaves would require filters with a lower cutoff: long kernels for a FIR filter.

But it still may happen that the single filter variable cutoff version ends up doing less work. Especially if the filter is not a FIR.

mystran
KVRAF
6720 posts since 12 Feb, 2006 from Helsinki, Finland

Post Fri Oct 08, 2021 1:08 pm

SNFK wrote:
Fri Oct 08, 2021 6:20 am
For non-periodic audio, you need to filter with an actual brickwall lowpass (eg. windowed sinc or whatever).
What would you recommend? I saw somewhere on this forum a while ago that Chebyshev 2 filter would work well, but others also said a sinc FIR filter would work well too. It wouldn’t need to be a perfect brickwall low pass because then I could oversample the little artifacts off…?
IIR filter like Chebychev's are going to cause some additional trouble here and can't be computed with FFT fast convolution, so I'd just go with windowed sinc (eg. Kaiser let's you tweak it easily).
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

User avatar
Music Engineer
KVRAF
4013 posts since 8 Mar, 2004 from Berlin, Germany

Post Fri Oct 08, 2021 9:05 pm

SNFK wrote:
Fri Oct 08, 2021 6:20 am
What would you recommend? I saw somewhere on this forum a while ago that Chebyshev 2 filter would work well, but others also said a sinc FIR filter would work well too.
if you really want to create mip-maps of your samples before playback, then i would probably indeed go with a bidirectional (forward/backward) chebychev-2 or elliptic filter. that said, i'm not so sure that i would go down the mip-mapping route at all and would probably just use windowed-sinc interpolation at realtime, with the sinc stretched out in time when transposing upwards. i should mention that i did not yet implement this - it is just what i would try first. i'll soon have to implement some of this stuff anyway for a project i'm currently working on....so we'll see...

edit: oh - i just recall that i actually did implement windowed-sinc resampling - just not in the context of a sampler but rather for non-realtime sample-editing purposes. my experience there was: with a kernel-length of 64, you get a quite good quality. of course, the aliasing suppression will depend on your transposition factor - higher upward transpositions call for longer kernels

mystran
KVRAF
6720 posts since 12 Feb, 2006 from Helsinki, Finland

Post Fri Oct 08, 2021 11:18 pm

Music Engineer wrote:
Fri Oct 08, 2021 9:05 pm
SNFK wrote:
Fri Oct 08, 2021 6:20 am
What would you recommend? I saw somewhere on this forum a while ago that Chebyshev 2 filter would work well, but others also said a sinc FIR filter would work well too.
if you really want to create mip-maps of your samples before playback, then i would probably indeed go with a bidirectional (forward/backward) chebychev-2 or elliptic filter.
What is the supposed advantage over a windowed sinc or some such FIR design that you can quickly process with FFT convolution? When you do bidirectional filtering like that it isn't even Chebychev-optimal anymore.. so like.. why?
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

User avatar
Music Engineer
KVRAF
4013 posts since 8 Mar, 2004 from Berlin, Germany

Post Sat Oct 09, 2021 12:00 am

mystran wrote:
Fri Oct 08, 2021 11:18 pm
What is the supposed advantage over a windowed sinc or some such FIR design that you can quickly process with FFT convolution? When you do bidirectional filtering like that it isn't even Chebychev-optimal anymore.. so like.. why?
in this context, i like the flatness and monotonicity of the chebychev-2 response in the passband, which i would deem a desirable feature in this application. with windowed-sinc, i would expect some amount of passband ripple. yes, ok, it may perhaps be minimized via numeric optimization of the window - but that's costly and i like my filters to be easily tweakable*. i also generally tend to prefer IIR over FIR filters due to their higher bang-for-the-buck ratio. all that said, FIR should be fine, too :shrug:

(*) i also mentioned elliptic as an option because with an elliptic filter, you may "dial-in" some passband ripple in a controlled way to steepen the transition (chebychev-2 is the limiting case of an elliptic filter when the passband ripple goes to zero - just for the record, mystran knows this, of course).
Last edited by Music Engineer on Sat Oct 09, 2021 4:43 am, edited 1 time in total.

mystran
KVRAF
6720 posts since 12 Feb, 2006 from Helsinki, Finland

Post Sat Oct 09, 2021 3:47 am

Music Engineer wrote:
Sat Oct 09, 2021 12:00 am
mystran wrote:
Fri Oct 08, 2021 11:18 pm
What is the supposed advantage over a windowed sinc or some such FIR design that you can quickly process with FFT convolution? When you do bidirectional filtering like that it isn't even Chebychev-optimal anymore.. so like.. why?
in this context, i like the flatness and monotonicity of the chebychev-2 response in the passband, which i would deem a desirable feature in this application.
Here's the problem: your IIR is infinite by definition... and when you truncate it, you'll get passband ripple and this is not trivial, because with higher orders some of the poles have quite a high Q. For FIR the maximally flat pass-band condition leads to Lagrange interpolators, but those are kinda terrible for just about anything and I'm not sure if there's anything better that avoids passband ripple.

Now, don't get me wrong, I'm very much a fan of Cheb-2 in audio, it's just that I'm not convinced it's a good idea in this particular application. Note that Kaiser-windowed sinc is also very much "tweakable" (you get to adjust both the cutoff and the Kaiser-alpha), which is why I'm also a fan of Kaiser windows. As far as I'm concerned, by the time you tweak alpha high enough to get reasonable stop-band attenuation, the pass-band ripple is negligible.
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

User avatar
Music Engineer
KVRAF
4013 posts since 8 Mar, 2004 from Berlin, Germany

Post Sat Oct 09, 2021 7:19 am

mystran wrote:
Sat Oct 09, 2021 3:47 am
Here's the problem: your IIR is infinite by definition... and when you truncate it, you'll get passband ripple
why do you assume truncation - or what sort of truncation? are you referring to the fact that when i reach the end of the forward pass, i would need to let the filter ring out infinitely long before reversing direction (and then processing this ring-out tail in reverse in the backward pass, to warm the filter up) ...and i have to truncate this ringout-phase at some point? or do you assume, i just turn around without giving the filter a ringout/warmup phase? what i conceptually do is to append enough zeros to the signal such that the filter has enough time to completely ring out after the forward pass, apply the backward pass, and then cut the resulting signal back to it's original length (that's not how it's implemented, but that's the idea). one could of course also keep the tail and also prepend a "forward tail" - the sample would get longer to both sides - but i usually cut it back to the original length. yes, i know - in theory, the filter never rings out completely - but from a practical point of view, when it's down to - say - 200 or 300 dB, you can safely call it done
which is why I'm also a fan of Kaiser windows. As far as I'm concerned, by the time you tweak alpha high enough to get reasonable stop-band attenuation, the pass-band ripple is negligible.
interesting - i did not yet have a close look at the kaiser window. maybe i should do. i happen to be a fan of the dolph-chebychev window ever since i learned about it. :-)

mystran
KVRAF
6720 posts since 12 Feb, 2006 from Helsinki, Finland

Post Sat Oct 09, 2021 7:36 am

Music Engineer wrote:
Sat Oct 09, 2021 7:19 am
mystran wrote:
Sat Oct 09, 2021 3:47 am
Here's the problem: your IIR is infinite by definition... and when you truncate it, you'll get passband ripple
what i conceptually do is to append enough zeros to the signal such that the filter has enough time to completely ring out after the forward pass, apply the backward pass, and then cut the resulting signal back to it's original length
IIR filter does not "completely ring out" which is why it's called "infinite impulse response" .. and when you cut the signal to it's original length, you're causing more issues (don't do this with a FIR either, unless you know there's a fade-in/out).

Sure, at some point the tail is floating point noise, but the point I was trying to make is that for a high-order Chebychev there's poles right next to the unit circle.
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

User avatar
Music Engineer
KVRAF
4013 posts since 8 Mar, 2004 from Berlin, Germany

Post Sun Oct 10, 2021 12:56 am

mystran wrote:
Sat Oct 09, 2021 7:36 am
Sure, at some point the tail is floating point noise, but the point I was trying to make is that for a high-order Chebychev there's poles right next to the unit circle.
i just ran a little numerical experiment: took the impulse response of a chebychev-2 lowpass of 10th order with cutoff at 1 kHz, sample-rate at 44.1 kHz, stopband rejection of 80 dB. converted the raw impulse response to dB and took a look. the impulse response decays down to around -200dB in around 1200 samples. that translates to around 27.21... ms. so, if you would just (conceptually) add a (pre- and post) zero-padding of 50 ms (let's be generous), it should actually be fine - or shouldn't it? fine in the sense of giving the filter enough time to ring out for practical purposes. granted, if the cutoff would be 100Hz instead of 1000, you would need ten times as much padding. still, half a second may be not totally unreasonable, if your actual audio data is several seconds long.
.. and when you cut the signal to it's original length, you're causing more issues (don't do this with a FIR either, unless you know there's a fade-in/out).
well, you could of course also just keep the filtered (pre- and post) padding. that would be the theoretically correct thing to do. but in larger algorithms, it may often be somewhat inconvenient when each filtering process makes your signal longer at both ends. depending on what you want to achieve, this may nevertheless be an option, though
Last edited by Music Engineer on Sun Oct 10, 2021 6:34 am, edited 2 times in total.

User avatar
Music Engineer
KVRAF
4013 posts since 8 Mar, 2004 from Berlin, Germany

Post Sun Oct 10, 2021 1:07 am

a mathematically more sophisticated approach would be to not just run the filter through the ringout/warmup phases but to derive equations for how it's state variables should be initialized before the backward pass (i think, we mentioned that in at least two other threads without ever really fleshing it out). i've actually worked out the math for first order filters. then tried second order and got stuck with unmanagably unwieldy expressions coming out of the CAS. here is what i've got so far (this is kind of work in progress and at the bottom of the document (under the last boxed equation) is just garbage). the boxed equations are what is needed for an implementation (i've implemented and tested them and - lo and behold - they actually work):

https://github.com/RobinSchmidt/RS-MET/ ... States.txt

the derivation was actually quite messy - much more so than i expected it to be. maybe my CAS skills are just not good enough and/or i took the wrong approach (if anyone knows a better one, please let me know). i guess, the best way to generalize to higher order filters would be to just split it into (complex) 1st order filters (partial fraction expansion) and then use the solution for the first order filter. one day, i will work this out. but today is not this day.

btw . i wonder, how matlab's filtfilt handles it. the doc says: "filtfilt minimizes start-up and ending transients by matching initial conditions."

https://www.mathworks.com/help/signal/ref/filtfilt.html

but i have no idea what that means. match them to what? to each other? why would that make any sense? maybe in case of a periodic signal - but periodicity can't really be the general assumption of such a routine or can it?
Last edited by Music Engineer on Sun Oct 10, 2021 7:32 am, edited 1 time in total.

urosh
KVRist
339 posts since 4 Oct, 2002

Post Sun Oct 10, 2021 3:10 am

Music Engineer wrote:
Sat Oct 09, 2021 12:00 am
in this context, i like the flatness and monotonicity of the chebychev-2 response in the passband,
But, two pass Cheb2 is not as flat. We would need filter designed in such way that two such filters in series give maximally flat response, have no idea how simple/hard/doable that is.

Return to “DSP and Plug-in Development”