7 ms·
Diodes Hate It – Clipping with DSPs
- diydsp 4y agoany indication of whether this introduces aliasing? which clipping alg is used?
- razerbeans 4y agoHey there, no aliasing in this case. As for the clipping, this is effectively just hard clipping like you'd see from two alternating diodes in series on an analog gain circuit. 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.
- valdiorn 4y agoI'm sorry, but waveshaping absolutely introduces aliasing - and I was a bit disappointed to not see a section in that in the post.
- razerbeans 4y agoInteresting, I was under the impression that aliasing was something on the presentation layer (e.g. plotting on an oscilloscope). Have some breadcrumbs/links to share that would be a good resource on understanding aliasing in your context? Would be excited to learn more!
- raphlinus 4y agoHere's a fairly recent paper on techniques to reduce the aliasing: https://www.dafx.de/paper-archive/2016/dafxpapers/20-DAFx-16_paper_41-PN.pdf https://www.dafx.de/paper-archive/2016/dafxpapers/20-DAFx-16...
- TreeRingCounter 4y agoTake a sine wave below your system's Nyquist frequency. Chop off the top. Take the continuous Fourier transform. You will notice that there are now frequency components above the Nyquist limit of your system. Those will now be aliased down to lower frequencies. 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.
- fluoridation 4y agoHow would simple hard clipping cause aliasing?
- bigbillheck 4y agoClipping adds high-frequency components.
- deleted 4y ago[deleted]
- TreeRingCounter 4y agoTake the fourier transform of a clipped signal - it will have high frequency components. 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.
- Gordonjcp 4y agoIn the analogue realm - or more correctly, in the continuous-time realm, because an analogue sample-and-hold will show the same issues, it doesn't. It just introduces more harmonics at high frequencies. 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.
- fluoridation 4y agoWhat I don't get is, isn't that an inevitability of working with a digital signal? That is, if you were to sample a signal that was clipped in continuous time, wouldn't you get the same pattern of samples than if had clipped the signal after sampling?
- diydsp 4y ago
- duped 4y agoWhen you clip in continuous time you're pushing additional energy into the harmonics of the baseband signal being clipped. Since spectra in continuous time is infinite, you don't get any aliasing (a better way to say it is that "aliasing" isn't as meaningful in continuous time) 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.
- TheOtherHobbes 4y agoOversampling won't help with the brickwall clipping being attempted here. 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.
- razerbeans 4y agoI appreciate your feedback and sharing your knowledge! I'll definitely be digging more into some of the things your mentioned to get a better understanding. The topic is a super deep one–though my goal (at least at this point) is to stay high level enough so that anyone could get started in making their own sounds/effects. 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 :)
- Gordonjcp 4y agoWhy isn't there any aliasing? You're flattening off the peaks of the signal which absolutely must generate more harmonics, and at some point those are going to extend far beyond Nyquist. I don't see you doing anything in particular to bandlimit your waveshaping, but I might well have missed it.
- ben7799 4y agoDoes it matter? This is for distortion/noise, like in music. 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.
- duped 4y agoYes, it matters a lot. A mild amount of low aliased crunch sounds very different from a mild amount of very aliased distortion, and people pay a bunch of money for software that sounds good. 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.
- ben7799 4y agoI play electric guitar.. I really doubt what you're saying. 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.
- 4y ago
- samdafi 4y agoIt does. Oversampling is the classic way to reduce that problem, but some newer techniques to mitigate hard clipping aliasing (or any transfer function aliasing) with GitHub repo examples here: https://jatinchowdhury18.medium.com/practical-considerations-for-antiderivative-anti-aliasing-d5847167f510 https://jatinchowdhury18.medium.com/practical-considerations...
- pjc50 4y agoClipping in the digital domain like this introduces an extremely hard corner, effectively corresponding to a set of signals near the sampling frequency and its harmonics that are required to perform the shaping. That's where the "interesting distortion" comes from. 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.
- alexjplant 4y agoCliff Chase, the guy who started Fractal Audio [1] to build the Axe-Fx digital guitar processor, often writes about these types of disparities [2] since his business is modeling analog components in the digital domain. I'm not a trained EE and have a very surface-level understanding of the subject matter but I find these types of talks fascinating nonetheless. [1] https://www.fractalaudio.com/ https://www.fractalaudio.com/ [2] https://forum.fractalaudio.com/forums/tech-notes.77/ https://forum.fractalaudio.com/forums/tech-notes.77/
- Turing_Machine 4y agoSome still insist on old-school germanium diodes and transistors for things like clones of the classic guitar effects pedals. Those are also ~0.3 V, but their proponents argue that the shape of the "knee" and other parameters are different to silicon, thus leading to a perceptible difference in sound quality (in this application, the devices are often intentionally operated in non-linear regions) I've also heard that the specs for old germanium devices varied a lot more than modern silicon ones, thus leading to stories that (e.g.) Jimi Hendrix would order a dozen pedals and pick the one he thought sounded best.
- ilyt 4y agoThat reminds me about podcast with some mic guru that was talking about zener diodes. They used ones to stabilize high voltage used for a capacitive microphone and found out that while initially they didn't care much as long as voltage matches, some models had harder or softer knee and the softer ones reduced the noise level coz they provided more noise-free power.
- wyager 4y agoI used the Teensy as a DSP platform for https://yager.io/vumeter/vu.html https://yager.io/vumeter/vu.html 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.
- LeifCarrotson 4y agoAs an EE who uses Teensies occasionally in embedded and industrial contexts, I'm always surprised by the expectation of "the audio guys" regarding hardware/software solutions. 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.
- osigurdson 4y agoAny waveform can be composed by a (sometimes infinite) series of sinewaves. The goal of distortion is to create a set of such sinewaves that sound pleasing to the ear. A much more complicated problem is to create such waveforms that fit optimally in a given mix with other instruments.
- razerbeans 4y agoOP here! I'm admittedly new to the world of digital signal processing and don't have a math background. But what I _do_ have is a background in programming and guitar effect pedal building. 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!
- throwaway17_17 4y agoI’ve recently started to work through implementing DSP as VST plugins. But I never really thought that a pedal type DSP unit was tractable as a hobbyist. I’m pretty excited to follow your work and see if I can do some pedal style effect or basic synth work in hardware.
- perishabledave 4y agoThanks for the post! Right before I saw this on HN I was watching a video on how to build a Klon clone, so this write-up couldn't have come at a better time.
- razerbeans 4y agoIf you haven't taken the leap, go for it! That's actually how I started getting into effect pedals and now I'm hooked.
- rerdavies 4y agoAgree with all those who have raised the issue of problems with aliasing. Just to flush things out, it's worth mentioning what effect aliasing has on the quality of the audio output. Nobody seems to have address that issue yet. 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.
- razerbeans 4y agoYou've done a fantastic job of describing what I couldn't put my finger on! One of the issues I had when developing this code was what I referred to as a "jarring" noise when distortion gain was turned up. It's like you describe: almost "metallic." I alluded to the "jarring" sound in the article, and it's great to know the culprit! There weren't a lot of repos, articles, or forum posts that had examples of solid, straightforward distortion implementations, and now I can see why. Hopefully, we'll see someone get some code out there that helps clean this up!