Digital emulation of analog comparator

But why?

Recently, I decided to implement digital emulation of all my current eurorack modules. That way, I'll be able to share them with everyone to play and experiment before they decided to spend cash on an actual module. There's just one big obstacle in a way - SSH uses analog switches driven by comparators in the signal path. While analog switches can be nicely modeled with multiplication, I was unable to find any publications regarding accurate modeling of comparators.

Naive approach

The simplest way to try to model an analog comparator would be to use a digital comparator. Unfortunately, while simple, this solution suffers from a great amount of aliasing. It can be thought of in two different ways. One is that digital comparator outputs bandwidth unlimited step at the output leading to great aliasing, another is that digital comparator works in discrete time so can't sense when exactly the threshold has been crossed.

Oversampling

One, often thrown around, method of improving quality and reducing aliasing of digital modeling is to use oversampling with naive approaches like the one described above. While it indeed helps, the computation cost involved quickly gets prohibitively expensive. Oversampling N times requires much more than N times more calculations. That's due to low pass filtering required at the end of the chain where filter length also grows with oversampling ratio, effectively making scaling grow closer to N^2. In case of a comparator, where the output is discontinuous (step function) harmonics decay no faster than 6dB/oct which would require high oversampling ratios to greatly reduce them, making it infeasible.

Proposed approach

After some analysis, I came up with a different way to simulate a comparator. The digitalized output of ideal analog comparator is bandlimited step function. It can be obtained with arbitrary subsample delay by simply integrating sinc function. It can be generated offline, at high resolution, and stored for later. By simply traversing the table with offset proportional to subsample shift and step equal to table resolution, one can obtain bandlimited step function centered at an arbitrary point in time.

The next task then is to somehow extract the threshold crossing point of the input signal. There are a few well known methods of doing that depending on exact properties of the signal.

Finding exact cross point

In simplest case, signal in between two samples can be approximated with linear curve. It works well for signals where bandwidth is much smaller than sample rate, failing spectacularly when the signal approaches the Nyquist frequency. The extreme case would be sinewave with its peak positioned in between two sample points, which would make it effectively never cross threshold if it'd be close to peak level. Another approach to analyzing it is to think of linear interpolation between samples as FIR low pass filter with impulse response of [1,1]. Such filter has zero at Nyquist frequency, distorting amplitude response at high end.

A better option is to use higher order polynomial approximation and find threshold crossing by solving the polynomial equation. For 3rd order polynomials, there is a closed-form mathematical solution that can be used directly, higher orders will require an iterative approach. It's important to note that the problem can be divided into two sub problems: finding if crossing exists between two samples and only later trying to estimate where is it.

How do polynomial approximation cope with high frequency response? It's beyond scope of this short post, so let's just say that the higher order the better. For anyone curious I can't recommend enough reading about Savitzky-Golay Filters.

Closing thoughts

It's important to note that bandlimited step response is infinitely long. Designer has to decide at which point to truncate it, windowing may also be employed to further limit spectral leak. Because the response length is independent of anything but arbitrary choice, it can be longer than the period of edges in the input signal. The implementation has to be able to handle that effectively by summing the individual contribution of all step functions currently "in flight".

Comments

Popular posts from this blog

Active Eurorack Busboard Part 3

External Cavity Diode Laser - part 1: optical path and mechanical design

PCB holder 2