Practical Considerations for Antiderivative Anti-Aliasing

In audio signal processing, it is often useful to use nonlinear functions for things like saturation, wavefolding, and more. While nonlinear functions can create interesting sounds, they often create difficulty in the world of signal processing, particularly by invalidating many of the handy mathematical theorems that hold up in the world of linear signal processing. One particularly nasty issue that arises when using nonlinear functions on digital audio is aliasing.

In this article, we’ll give a brief discussion of what aliasing is, and how it is typically dealt with. Then, we’ll introduce a relatively new anti-aliasing technique called Antiderivative Anti-Aliasing (ADAA), and discuss some practical considerations for implementing various nonlinear systems with ADAA.

As usual, this article can be read with code as a Jupyter Notebook.

Aliasing

Typically, digital systems use filters to supress any signal above the Nyquist frequency, so that aliasing artifacts don’t occur. However, when using a nonlinear function, the digital system can create signal above the Nyquist frequency, thus creating aliasing artifacts. As an example let’s look at a specific type of distortion known as “hard-clipping” distortion. A hard-clipper is a waveshaping distortion with a waveshaping curve that looks as follows:

If I now create a 1.2 kHz sine wave, and process it through a hard-clipping distortion with no aliasing, I would see the following frequency response:

The spike farthest to the left represents the original sine wave, while all the other spikes represent the higher order harmonics generated by the hard-clipper. Note that all of the harmonics are evenly spaced.

Now, if we were to sample the sine wave at fs=48 kHz before processing it with the hard clipper, any generated harmonics above fs/2=24 kHz would be reflected back to produce aliasing artifacts. Let’s see what that looks like:

In this case, we see that all the blue spikes not marked with the red x’s are aliasing artifacts. This level of aliasing will almost certainly be audible for people listening to signals that have been processed with this effect, and probably won’t sound very pleasant.

Anti-Aliasing with Oversampling

The idea behind oversampling is very simple: if we run our nonlinear process at a higher sample rate, then any harmonics produced above the Nyquist frequency can be filtered out before the audio is downsampled back to its original sample rate. Let’s see how well 4x oversampling works for out hard-clipping distortion:

The result is pretty good! While aliasing artifacts are still visible they are at a much lower magnitude than the original signal. More oversampling can be used to supress aliasing artifacts even further.

However, using oversampling has some drawbacks, notably with performance. Specifically, the processing time for an effect will be multiplied by the oversampling factor. In other words, if a process is done with 4x oversampling, the oversampled process will be 1/4th as efficient as the original process. With that in mind, let us look for other anti-aliasing methods, that hopefully introduce less performance overhead.

Antiderivative Antialiasing

1st-order ADAA

Unfortunately, when |x[n]−x[n−1]| is very small, this equation becomes ill-conditioned, similar to dividing by zero. To remedy this issue, we define a tolerance, below which |x[n]−x[n−1]| is treated as if it were zero, and then use the following equation to express first-order ADAA:

For the hard-clipper, we can write the original nonlinear function as follows:

And the first antiderivative as:

Let’s see how the aliasing artifacts look using first-order ADAA:

Clearly, the aliasing artifacts are significantly more supressed than with no anti-aliasing, however they are still of significant magnitude. One option would be to use first-order ADAA in tandem with a modest amount of oversampling, maybe 2x:

2nd-order ADAA

Since this equation has more divisions where a divide-by-zero error is possible, several “fallback” computations are necessary. I won’t go through them in detail here, but full derivations can be found in Stefan Bilbao’s IEEE paper (linked above), and implementations can be seen in the attached code (see below). Finally, we need the second antiderivative of our hard clipping function:

Now we can examine the response of second-order ADAA:

Again, using a modest amount of oversampling in conjuntion with 2nd-order ADAA can help immensly. Below, we show the response using 2x oversampling:

Tanh Distortion with ADAA

Let’s take a look at the frequency domain output of the tanh distortion, with and without aliasing:

In order to implement ADAA, we’ll need to find the first and second antiderivatives of f(x)=tanh(x):

Note that log(x) refers to the natural logarithm, and Li₂(x) refers to the dilogarithm function. While the dilogarithm is a tricky function to implement by hand, there are decent open-source implementations available in Python’s Scipy library, and for C/C++ in the polylogarithm library on GitHub. Note that depending on the implementation of the dilogarithm function, it may need to be called in the form Li₂(1−x).

Let’s take a look at how well first- and second-order ADAA supress aliasing artifacts for the tanh nonlinearity:

ADAA with Stateful Systems

The resonant frequency of the nonlinear waveguide resonator is dependent on the round-trip delay around the nonlinear feedback loop. If we were to use first-order ADAA to implement the nonlinearity, then the resonant frequency would be skewed low due to the extra half sample added to the round-trip delay time by the ADAA process. While the error in frequency might be pretty minimal (especially for low-pitched resonators, or systems with high sample rates), it can be noticeable. For example, waveguide resonators are often used to model vibrating guitar strings, an error in the frequency of the waveguide could make a guitar string go out of tune!

In this case, ADAA can be still be used, just with a small adjustment: after calculating the correct length of the delay line to use at the current sample rate, the user must subtract half a sample, to compensate for the delay added by the ADAA process.

In general, using ADAA for stateful systems can be slightly more difficult, though it is possible. For more details, see Martin Holters’ 2019 DAFx paper on the subject.

Implementation

Currently, I’ve implemented the hard-clipper, and tanh distortion, as well as a nonlinear waveguide, using the tanh function as the nonlinearity in the feedback path. In the future, I hope to add more nonlinear systems, such as nonlinear filters, and wave digital filters (inspired by Davide Albertini’s recent paper). The project is open-source, so if you have a nonlinear system you’d like to see included, feel free to code it up for yourself!

Source code and further documentation can be found on GitHub.

Acknowledgements

Jatin Chowdhury is a student.