Guitarists will do just about anything to get just the right sound out of their setup, including purposely introducing all manner of distortion into the signal. It seems counter-intuitive, but it works, at least when it’s done right. But what exactly is going on with the signal? And is there a way to simulate it? Of course there is, and all it takes is a little math and some Arduino code.
Now, there are a lot of different techniques for modifying the signal from an electric guitar, but perhaps the simplest is the humble diode clipping circuit. It just uses an op-amp with antiparallel diodes either in series in the feedback loop or shunting the output to ground. The diodes clip the tops and bottoms off of the sine waves, turning them into something closer to a square wave, adding those extra harmonics that really fatten the sound. It’s a simple hack that’s easy to implement in hardware, enough so that distortion pedals galore are commercially available.
In the video below, [Sebastian] explains that this distortion is also pretty easy to reproduce algorithmically. He breaks down the math behind this, which is actually pretty approachable — a step function with a linear part, a quadratic section, and a hard-clipping function. He also derives a second, natural exponent step function from the Schockley diode equation that is less computationally demanding. To implement these models, [Sebastian] chose an Arduino GIGA R1 WiFi, using an ADC to digitize the guitar signal and devoting a DAC to each of the two algorithms. Each distortion effect has its own charms; we prefer the harsher step function over the exponential algorithm, but different strokes.
Kudos to [Sebastian] for this easy-to-understand treatment of what could otherwise be a difficult subject to digest. We didn’t really expect that a guitar distortion pedal would lead down the rabbit hole to diode theory and digital signal processing, but we’re glad it did.
>The diodes clip the tops and bottoms off of the sine waves
Coincidentally, the same diode distortion filter can be used to make a pretty good sine wave oscillator for simple analog synths.
You start with an integrating op-amp circuit followed by a schmitt trigger, and loop back the resulting square wave to the input of the integrator. When you tune the zero reference of the integrator to be half of the input square wave voltage, you get a triangle wave with a constant amplitude at the integrator output. The frequency is easily adjusted by varying the integrating RC element (e.g. potentiometer or variable capacitor), or by using the schmitt-trigger to switch a control voltage at the input on and off.
The triangle wave is then fed to the distortion filter and the amplitude is trimmed just so it just rounds off the peaks but doesn’t clip it back to square. With the right selection of diodes, you get a very close approximation of a sine wave at a constant amplitude.
Triangle waves are interesting in another sense, because you can put them through a rectifier to flip the bottom half up and get another triangle wave at twice the frequency, and rectify that again to quadruple the frequency etc. so you can create a whole harmonic series and then put those signals through diode clippers to get the sine waves. You will then have the fundamental square wave, triangle waves, and sine waves, which you can mix together to create more complex sounds that resemble flutes, violins, brass instruments etc.
This is nothing new, but of course interesting to poke around. Many modern guitar players, including me, are chosing modellers, which are all digital DSP processing units. The biggest, the best, and the most expensive is by no doubt Fractal Audio FX3. Awesome unit.
With these “huge” micro-controllers, why not use a RAM table-lookup for the AD/DA translation? The ADC is only 12 bit. In RAM it can be recalculated on the fly to support other shapes too.
I was actually considering that option. There’s plenty of memory to pre-calculate coefficients and then use them on the fly. This might be especially useful when multiple effects are stacked on top of each other, like in my Tremolo effect example. It would save significant computing time to just have all coefficients pre-calculated with a 2-dimensional array, one index for the ADC value, the other for the relative sampling position. Might actually do that in future versions.
There’s another thing called IR (impulse response). Normally these comes for 48kHz 24bit, but I’m sure it would be possible to convert these files for 12bit and other sampling rate, losing quality, but making it work for your project.
Can an impulse response capture the nonlinear characteristics too? I thought it can be used for filtering, reverb etc. while behaving in a “small-signal fashion”, responding equally for any amplitudes.
Yes, AFAIR, impulse response is only a characteristic of linear systems. Maybe a combination of an impulse response and non-linear function (out=f(in)) will allow producing every possible effect.
For me as a audio hobbyist IR on low performance hardware is very interesting topic. Sebastian, if you are reading this – if you can make IR working with 12 bit processing and will publish your results for free – I’ll buy you a beer.
Hey Andrejs, up your wager to a 6-Pack and I’ll accept the challenge of at least trying. Shoot me an eMail: sebastian [AT] baltic-lab.com
You have a deal. I’ll shoot you.
This is how I do it. Linear interpolation of a transfer function – that way you can have an arbitrary TF and bounded compute time in the processing loop. Anti-aliasing before the NLP is your friend here :>
Everything that people like to hear is distortion. What are you going to do, listen to a sine wave?
Many guitarists prefer the tube distortion sound. My thinking is that the tube distortion could be reproduced by using software to clip the audio. Robert Fripp of King Krimson used 6 separate clipping circuits one for each string. This separation of the individual strings resulted in an amazing distortion sound. He called it frippertoronics.
At bottom Frippertronics is the use of a tape loop.