Diodes Hate It – Clipping with DSPs(straighttoground.com) |
Diodes Hate It – Clipping with DSPs(straighttoground.com) |
Real diodes are not quite as sharp, because there's an exponential region between forward voltages of 0V and the forward voltage of the junction (~0.7V for P-N diodes, ~0.3 for Schottky).
And vaccum tubes are softer still, with a considerable rolloff region.
This is true. The important feature of germanium diodes, wrt this application, isn't their lower forward voltage as Schottky diodes are even better at that , but rather their characteristic V/I curve which is softer than silicon (0) and produces less odd harmonics which would otherwise contribute to a harsher sound.
Regarding germanium transistors, they're mostly used to replicate old school fuzz guitar pedals. In that application, aside the above characteristic, the particular configuration in which they're employed contributes to the sound: old fuzz pedal produce a very pleasant asymmetric distortion in which one half wave is much less distorted than the other, so that the output can contain a blend of clean and distorted sound. R.G. Keen at Geofex (.com) wrote a thoroughly informative article about how that circuit works which anyone interested in building guitar pedals will find very informative (1).
(0) https://commons.wikimedia.org/wiki/File:V-a_characteristic_d...
(1) http://www.geofex.com/article_folders/fuzzface/fffram.htm
The way around it is to make sure the upper harmonics generated by distortion beyond the limit is below the audible threshold, either by making the period larger (over sampling) or modifying the distortion process. It's not lossless and a difficult design problem when factoring in computational limits. It's often cheaper to use discrete analog components with all their flaws than a good-enough processing chain and computer that can evaluate it.
Even offline whitebox modeling tools like SPICE, and realtime black box tools like Volterra series models are limited by this.
Aliasing causes higher harmonics to reflect at the Nyquist sampling rate and be heard as a signal with lower frequency. e.g. a 25khz signal with a sampling rate of 24khz will end up sounding like a 23khz signal instead. A 26khz signal will sound like a 22khz signal. And so forth. Because there are exponentially more harmonics as frequency increases, aliased harmonics can end up reflecting a startling amount of energy into audio frequency ranges, even if the amplitude of each harmonic is much lower than the amplitude of the fundamental harmonic.
Because the harmonics are reflected, they no longer form a harmonic series, so they end up adding a metallic sound to the overall output signal. The sound of inharmonic aliased signals alone is not unlike the sound of striking a metal dinner plate with a spoon, or a copper pipe with a wrench. Not a pleasant sound.
We have very inexact vocabularies for describing sound. But clipped signals with heavy aliasing are usually described in terms like the following: "harsh", "metallic", "cold", "fizzy".
In fairness, this is a toy example. One would hope that in a mature ecosystem, those few who are capable of doing the very significant heavy math lifting required to implement better clipping will contribute components that can be shared and reused by everyone.
Hopefully, we'll see someone get some code out there that helps clean this up!
My hope with this site and series is to get people like me exposed to the possibilities a little bit of code and a Teensy can unlock for them. The lower the barrier of entry, the more cool things people can create. :)
That being said, I'd love to get feedback on good sources to learn more about signal processing/theory or any resources folks would suggest for covering the topic even better!
It did a great job! It has a lot of processing power and since it's bare-metal, latency and variance concerns are easily avoided.
I was disappointed that there didn't seem to be a good ecosystem of SPDIF/Toslink-based audio I/O for the Teensy, at least at that point. I had to make the Toslink hardware myself.
Toslink is an awesome connector for this type of thing, and I think it would be great if people started using it for Eurorack-style audio processing modules instead of analog signals.
You can get stereo pretty easily this way, and if someone could write up some ADAT lightpipe code, you could even get 8-channel polyphony!
Glad to see some posts about using Teensy for this purpose.
For most users of these chips, "making the X hardware yourself" is the expected outcome, the fact that there's a convenient compute module that has USB power, the processor, a debug interface, a USB connector, and an open-source toolchain to quickly reach the "blink LED" stage is just a small leg up on the normal way of doing things.
I think it's so cool that there's a whole culture of people building little mixers, synths, and guitar pedals and so on in their garage. That's kind of tangential to the high school kids learning to code on Arduinos. And that's kind of tangential to those of us using these development boards professionally, either for prototyping or for low-volume custom stuff.
Side note: You might be interested in the TMDX5535EZDSP DSP eval board (still active, now over 10 years old, but it was a great platform for me to learn the fundamentals of DSP) or the newer TMDSOSKL137 eval board (more appropriate for building a giant mixer), they integrate a lot more audio processing power and have FFT accelerators/SOC audio interfaces that are better than even the Teensy 4.0/4.1.
https://jatinchowdhury18.medium.com/practical-considerations...
Admittedly, I'm early into moving from analog circuits to digital signal processing, so I could be off the mark on my answer. :) Hope it helps, though.
When you clip in discrete time, the spectra is finite (more technically, it's periodic with a period of the sample rate frequency). That means the energy that would go into harmonics past nyquist gets "wrapped" around.
This is the big difference between analog and digital distortion. In analog, it's really quite difficult to create energy at non-harmonic frequencies of the signal. In digital clippers like you have here, it's trivial, and the design problem is figuring out how to deal with it. Most products will use some kind of anti-aliasing strategy (usually oversampling before clipping) to handle it.
I don't see you doing anything in particular to bandlimit your waveshaping, but I might well have missed it.
Nobody using clipping as an effect in their music cares if a fuzz/distortion/overdrive effect is less than pristine from an audiophile standpoint.
Audiophiles would throw up if they realized exactly what's going on in a guitar rig.
Audiophiles also drop a lot of money on gear that intentionally distorts the signal. Aliasing is one reason why they buy tube amplifiers instead of tube modelers in their receivers, although I don't doubt the latter exists.
As for the cost efficiency of DSP emulation vs using genuine parts - sure maybe it makes sense to use real analog hardware. It depends on the specific requirements of your design.
This circuit is not a diode emulator, it's a comparator. It's the worst-sounding of all distortions. It sounds even worse in digital because of the aliasing.
And it will always alias, no matter how much you oversample it, because a vertical edge - aka "Heaviside Step Function" - has an infinite harmonic series. If you oversample it enough it won't alias much because the series terms become smaller. But they never disappear.
A better way to do this kind of clipping is with a tanh (logistic/s-curve) approximation. That can give you a variety of valve-like [1] smooth clipping curves. Unfortunately tanh is pretty expensive computationally, so a more practical alternative is a piecewise curve, perhaps with some interpolation.
Although if you only have 8-bit or 16-bit resolution you may as well just use a lookup table.
OP might want to consider learning a little more about signal theory and practical DSP before posting more how-tos.
[1] Not really because real valves are more complicated. But it will do for a first approximation.
The lower the barrier of entry, the more cool things that people can come up with! I'm hoping that more without a math/EE/audio background like myself can get started and explore some more of these deeper topics :)
The classic soft clipper is something like
tanh(kx) / tanh(k)
which gives you a normalized output. It's not that expensive in the grand scheme of things, and is certainly cheaper than oversampling.If you want something even more valve-like, a bit of DC bias to the signal before tanh will give you even order harmonic distortion, which sounds warmer (it beefs up the signal and puts energy into the octave harmonics, where as hard clippers usually only shove energy into the odd harmonics which sound harsher).
I'd bet I could ask 10 other people who play guitar and none of them even know what aliasing is.
And none would be able to talk intelligently about what it means and what sounds good when thinking about a transistor distorting, an op-amp distorting, diode or LED clipping, a tube causing clipping/distortion, or a digital algorithm clipping. Most just try something and fiddle with the knobs, decide whether it sounds good or not, and move on with trying to figure out what music to create.
I actually find the stuff that gives more options around clipping tends to just be annoying. I prefer the designer to just pick the best sounding one and just go with that. Pretty much every time I've had an effect that offered a clipping switch or rotary dial one sounds the best, it takes a couple minutes to decide, after that I just wish the designer had left only that best one in the product without a switch.
I'd kind of argue intentional aliasing as a purposeful strategy to get a unique sound is probably something that has been overlooked. I guess "bit crushing" counts and that is somewhat common though.
Audiophiles are a whole different thing. I love tubes for playing guitar where everything is very non-audiophile but it's still really hard to wrap my head around it for audio reproduction.
I have been doing tons of anti-aliasing and saturation DSP lately and agree you mostly should be picking a vibe and sticking to it as a DSP sound designer. However the hell that comes together in the math is irrelevant, as long as the minimal set of controls you provide to the musician are fun to tweak and you’re not delivering an airplane cockpit.
There is a lot of marketing blather about analog this legendary that but at the end of the day, a unique sound that is fun for musicians to “play” or intuitively useful for the tradespeople (mixers, mastering engineers, trackers) to use is all that matters!
For example, if you were comparing AxeFX, Kemper, or Line6 Pod none of this would be on the labeling, but it's deeply tied to how they were designed.
But beyond musicians being able to hear what they like and don't like I don't think anybody understands what is going on. It's quite possible aliasing is part of "sounds digital" when a musician uses that as a pejorative... newer digital processing sounds less digital through better algorithms and likely things like better handling of aliasing.
One trick for doing nonlinear waveshaping without introducing too much aliasing is to perform the wave shaping at a higher sample rate than the rest of your system and then downsampling with a low pass filter. Thankfully, the high frequency components introduced by nonlinearity tend to decrease in magnitude reasonably quickly.
In the digital (or again more correctly discrete-time, you have a sample rate) realm it totally makes a difference, because many of the harmonics you generate will extend above the Nyquist frequency, half the sample rate, and "reflect" back down.
In general, the more pointy edges you introduce to a waveform, the more high frequency artifacts you get.
This aspect of pontryagin duality (narrow in one domain means wide in the other) is also what underlies the heisenberg uncertainty principle. If you "hard clip" a photon's position (with a slit) you get a lot of frequency domain (momentum) noise, leading to a spread-out beam.
So for example, one might think drawing a basic rectangle in time would create a pulse waveform... but in the digital domain a square wave has ripples in it. Recall that in the spectral domain, a rectangle is approximated by ever-higher and ever-smaller sinusoids to infinity... in sampled signals, though, only frequencies below Nyquist are available. So the sampled version of the waveform will have ripples that the highest frequency components would have completed. The requirement then is to work with a higher Nyquist/sampling frequency than audible so that audible part of the square is correct.
From 0 to 1, 1 to to, 127 to 128, 254 to 255, nothing surprising happens, the output voltage slowly rises. When the counter rolls over from 255 to 0, something surprising *does* happen though, we get a massive and instant negative-going spike.
Because this jump from 255 to 0 is instant, it has to have a lot of harmonics, up into high frequencies. If you plot the spectrum of a sawtooth wave you'll see that there's a sine wave at every harmonic, reducing in amplitude as 1/f - that is, the first one at full level, the second harmonic, twice the frequency, and at half level, 3rd at 1/3, 4th at 1/4 and so on.
Now here's your problem - some of those harmonics, which extend off to infinity, are going to be beyond the Nyquist frequency. As you increase the frequency of the sawtooth relative to the sample rate, you'll start to creep into an area where those harmonics are actually still quite loud compared to the lower ones, and that gives you a problem.
Aliasing occurs because those harmonics "reflect" off the Nyquist frequency. Think about watching a Western, where the wagon wheels appear to spin backwards at certain speeds - they are aliasing. If they were turning slowly they'd move a little before the next frame, but at some point - where one spoke moves exactly into the space left by the previous spoke - they will appear to stand perfectly still! With a bit of thought, you'll see that the fastest the wheel can go puts the spoke exactly in the middle of the gap between spokes in the previous frame, and any faster will make it look like it's going backwards.
This is exactly what's happening with aliasing. The sinewave that's just above half your sampling rate is coming back down towards you, backwards. What goes up must come down.
In a real-life analogue sawtooth oscillator this is of course a huge problem because that big negative-going spike has infinite energy, and we're lucky that it's also infinitely short because anything that requires infinite energy is going to have terrible battery life. In practice, the maximum frequency of the harmonics in the sawtooth are limited by other parameters in the circuit (mostly how quickly the capacitor discharges through the reset transistor). And herein lies our major clue.
In order to generate a sawtooth that doesn't alias, we must calculate that step so it is no longer infinitely fast. There are a bunch of ways we can do this, but a nice simple way is to calculate an amount to "correct" the sample just before and just after the step so that you've got a rough approximation of passing the signal spectrally through your sinc window. This is crude but effective, and can be done with a couple of adds and multiplies making it ideal for real-time computation on even fairly crappy chips.
You can actually generate a sinc wavelet and "paste" that over the step, and when you get that right it sounds perfect. If you've got a fairly chunky DSP or general-purpose CPU with good math support to play with, this will give best results.
It's important to note that if you take a sinewave and clip it, then sample it, you'll still get aliasing because you'll still have a big infinitely fast step somewhere, which is why audio equipment is so big on having steep lowpass filters before and after digital (or, sampled, at least) bits. A great example is in the resolutely analogue Roland Juno 106, where the chorus board has three 24dB/octave Butterworth filters at around 10kHz, realised as cascaded pairs of Sallen-Key filters. The first rips off any signal above about 12kHz so it cannot be passed to the chorus chip, the second two pairs are "reconstruction filters" to remove fizzy-sounding sampled steps which would be apparent as a swooshing sound as the chorus went through the "long" end of its delay.
The reason they need this is that the analogue "Bucket Brigade Delay" chips are a kind of analogue dynamic RAM, where on every clock pulse a capacitor charges up from the input to its left, and on the next discharges into the output on its right. Stick a few hundred of these capacitors together and it will "pass buckets of signal" down the line, delaying it by whatever the clock rate is divided by the number of buckets. It's all analogue, but it's still sampling!
Ah-ha. Gotcha. So my intuition was more or less correct. It's not that clipping in digital is especially susceptible to aliasing, it's that when people clip in analog they brickwall the signal to get rid of the harmonics.
You can't create a naive sawtooth (for example) and *then* filter it because the damage has already been done. That being said, the "supersaw" oscillator in the Roland JP8000 generated a bunch of naive saws and *highpass* filtered them just below their fundamental to remove the gurgly "beat note" from aliased partials.