# PolyBLEP

An anti-aliasing method for waveform step discontinuities, using polynomial bandlimited step function.

# 1 Algorithm

Here is a plot of a sawtooth waveform, and its bandlimited counterpart. The bandlimited version has 3 harmonics, because any higher than samplerate/2 (the Nyquist limit) would cause the digital spectrum to fold over, causing aliasing (which sounds bad).

waveforms

The aliasing comes from the sharp step discontinuities in the ideal sawtooth waveform. The strategy will be to replace the sharp steps with bandlimited steps.

A step function is the integral of the Dirac delta (aka impulse). A bandlimited Dirac impulse (with sample rate normalized to 1) is the sinc function sinc(t)=sin(πt)/(πt)\operatorname{sinc}(t) = \sin(\pi t) / (\pi t).

Unfortunately the sinc function extends forever in both directions, so the next step is to window it with a function that is 0 everywhere outside a small support region.

I tried the window from a Lanczos filter which uses the central lobe of sinc(x/a)\operatorname{sinc}(x / a) for small positive integer aa in the interval [−a,a][-a, a] and 0 outside, but in my tests a raised cosine window worked much better: (1+cos(πk/a))/2(1 + \cos(\pi k / a)) / 2 in the interval [−a,a][-a, a] and 0 outside.

Unfortunately (again) these functions are hard to integrate. The strategy I adopted is to approximate the curve by a cubic spline through integer t=k∈[−a,a]⊂ℤt = k \in [-a, a] \subset \mathbb{Z}.

The cubic spline needs the value and slope at the end of each of the 2a2 a intervals [−a,−a+1],[−a+1,−a+2],…,[a−1,a][-a, -a + 1], [-a + 1, -a + 2], \ldots, [a - 1, a]. These can be calculated readily (differentiation is easier than integration):

f(0)=1f(k)=0k≠0f′(0)=0f′(k)=((1+cos(πk/a))/2)(−1)k/kk≠0 \begin{aligned} f(0) &= 1 \\ f(k) &= 0 & & k \not= 0 \\ f'(0) &= 0 \\ f'(k) &= ((1 + \cos(\pi k / a))/2) (-1)^k / k & & k \not= 0 \end{aligned}

Now the cubic spline for x∈[k,k+1]x \in [k, k + 1], letting t=x−⌊x⌋t = x - \lfloor x \rfloor be the fractional position, is defined by the matrix equation:

yk(t)=(1tt2t3)(10000100−3−23−121−21)(f(k)f′(k)f(k+1)f′(k+1)) y_k(t) = \begin{pmatrix} 1 & t & t^2 & t^3 \end{pmatrix} \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ -3 & -2 & 3 & -1 \\ 2 & 1 & -2 & 1 \end{pmatrix} \begin{pmatrix} f(k) \\ f'(k) \\ f(k+1) \\ f'(k+1) \end{pmatrix}

Piecewise polynomials yy are easy to integrate to piecewise polynomials YY: integrate each section symbolically, accumulating the value of Yk(1)Y_k(1) as the constant of integration of Yk+1(t)Y_{k+1}(t) (iteratively). Letting Y−a−1(1)=0Y_{-a-1}(1) = 0, the symbolic integration is simple:

Yk(t)=(tt2/2t3/3t4/4)(10000100−3−23−121−21)(f(k)f′(k)f(k+1)f′(k+1))+Yk−1(1)=(tt2t3t4)(100001/200−1−2/31−1/31/21/4−1/21/4)(f(k)f′(k)f(k+1)f′(k+1))+Yk−1(1)=(1tt2t3t4)⋅sk \begin{aligned} Y_k(t) &= \begin{pmatrix} t & t^2/2 & t^3/3 & t^4/4 \end{pmatrix} \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ -3 & -2 & 3 & -1 \\ 2 & 1 & -2 & 1 \end{pmatrix} \begin{pmatrix} f(k) \\ f'(k) \\ f(k+1) \\ f'(k+1) \end{pmatrix} + Y_{k - 1}(1) \\ &= \begin{pmatrix} t & t^2 & t^3 & t^4 \end{pmatrix} \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1/2 & 0 & 0 \\ -1 & -2/3 & 1 & -1/3 \\ 1/2 & 1/4 & -1/2 & 1/4 \end{pmatrix} \begin{pmatrix} f(k) \\ f'(k) \\ f(k+1) \\ f'(k+1) \end{pmatrix} + Y_{k - 1}(1) \\ &= \begin{pmatrix} 1 & t & t^2 & t^3 & t^4 \end{pmatrix} \cdot s_k \end{aligned} where the precalculated sks_k is a constant vector of 5 real numbers, determined by the window size parameter aa. Then each cubic spline segment can be evaluated by a dot product with the same vector of powers of tt.

construction

Now for a sawtooth waveform, we can sample the ramp part naively, but when we reach a step discontinuity we replace it with a bandlimited step by shifting the cubic spline (minus the ideal step) and adding it to the output waveform (scaled by the size of the input step, in this case -1, in other words subtraction).

We do this by calculating the subsample offset tt of the ideal discontinuity from the known frequency of the waveform. If the step occurs between samples mm and m+1m + 1, with sawtooth values pmp_m and pm+1p_{m + 1}, then Δp=pm+1−pm+1\Delta p = p_{m+1} - p_m + 1 is the slope of the ramp and pm+tΔp=1p_m + t \Delta p = 1 at the discontinuity, which gives t=1−pm1−pm+pm+1.t = \frac{1 - p_m}{1 - p_m + p_{m + 1}}.

Then calculate (1tt2t3t4)\begin{pmatrix} 1 & t & t^2 & t^3 & t^4 \end{pmatrix} and compute the 2a2a dot products with the precomputed sks_k.

scatter convolution

The output is accumulated into a rolling buffer of length 2a2a: the regular values are added into on+a−1o_{n+a-1} and the PolyBLEPs are added into on,on+1,…,on+2a−1o_{n}, o_{n + 1}, \ldots, o_{n+2a-1}. This means the algorithm adds a−1a - 1 samples of latency.

The resulting waveform is smooth and close to the bandlimited curve (see first image above).

# 2 Spectrum

Plotting spectra shows that aliasing is reduced, especially at low frequencies, but high frequencies are reduced. Hence oversampling 2x may be needed for best results. Setting the parameter to a=3a=3, may be the best trade-off between quality and efficiency.

# 2.1 Low Frequency Saw

3 cycles in 1024 samples (140.625 Hz at 48000 Hz sample rate)

3/1024 spectrum

# 2.2 Mid Frequency Saw

17 cycles in 1024 samples (796.875 Hz at 48000 Hz sample rate)

17/1024 spectrum

# 2.3 High Frequency Saw

97 cycles in 1024 samples (4546.875 Hz at 48000 Hz sample rate)

97/1024 spectrum

# 3 Listen

In particular the high frequency naive saw sounds very bad.

# 3.1 Low Frequency Saw

3 cycles in 1024 samples (140.625 Hz at 48000 Hz sample rate)

# 3.2 Mid Frequency Saw

17 cycles in 1024 samples (796.875 Hz at 48000 Hz sample rate)

# 3.3 High Frequency Saw

97 cycles in 1024 samples (4546.875 Hz at 48000 Hz sample rate)

# 4 Source Code

Licence: GNU AGPL, v3 only

# 5 References