# Digital Distortion effect algorithm

I read the DAFX book by Udo Zölzer about the distortion effect at pages 124-125 and it says that suitable simulation of the distortion is given by the function:

$$f(x)=\frac{x}{|x|}\left(1-e^{x^2/|x|}\right)$$

Can someone explain this formula and what kind of signal we get?

From what I understand 'x' is the sampled signal, so this is a sequence of numbers. What does |x| mean? Does it refer to the absolute value of x for each sampled value?

So if I want to implement this simulation of the distortion effect,

1. I need to know the length of x (It's given by the number of samples)
2. In a loop, I need to calculate this formula for each sample value
3. after the loop ends, I get the distorted signal (in a digital form)

After that, I need to convert it to an analog signal so I can hear it.

• Note that there's an error in the formula given in the book (there should be a negative sign in the exponent). See my answer below. Feb 21, 2016 at 20:00
• Hi I have tried this and its almost what i was looking for. I wonder if i could add a curve to the clipper. it sounds real fuzzy and harsh and is there a way to curve the clipper so it sounds more smooth and softer? asking this because i have just started creating modules for synthedit. It would be great I've it can sound more smooth like a drive units, say the sinus becomes more of a curved pulse instead of the dipped one.
– Gijs
Jul 8, 2020 at 1:13

|x| denotes the absolute value - the x / |x| bit of the formula is there to make sure that the sign of the input is preserved in the output. Regarding the implementation, yes, the steps you have listed are correct.

• What do you mean by "real distortion"? Absolutely any operation you do on the original signal would be distortion anyway! What are you trying to do? Dec 12, 2013 at 10:31
• Distortion is a very vague term which describes any (usually unwanted) transformation that alters the signal. Guitar distortion is achieved by many different processes - clipping, rectification, overloading - depending on the kind of pedal/amp in which it happens - there is no single "true" formula... The formula you have looks like it'll give a sigmoid-like function which would simulate overloading ; but I think it might have a mistake somewhere. Dec 12, 2013 at 11:00
• You have to do this in the time domain. Dec 13, 2013 at 22:56
• Because that's how guitar distortion effects work. They were originally made with non-linear elements like tubes, diodes, and later transistors whose behaviour is described in the time domain by a non-linear function. And you're trying to emulate that digitally... Dec 14, 2013 at 8:39
• Pitch-shifting, fancy harmonies generator (say EHX micro pog) or fancy spectral morphing (can't recall the product name) require frequency-domain processing. Some amps/speakers simulator require long convolutions, which are performed efficiently by multiplications in the frequency domain. But in any case it's NEVER "take the whole FFT of the signal" - this is implemented by overlap-add of small-length FFT (1024 samples or so). Dec 14, 2013 at 12:33

Thanks to the plot in Olli Niemitalo's answer I got convinced that the formula given in the book has a sign error. The non-linearity used for fuzz or distortion is always some type of smoothed clipping function, which compresses the input signal. So small input amplitudes experience little change whereas high input amplitudes are (more or less) softly clipped. And the figure shown in Olli's answer does exactly the opposite.

So I'm convinced that the correct formula should be

$$f(x)=\frac{x}{|x|}\left(1-e^{-x^2/|x|}\right)= \text{sgn}(x)\left(1-e^{-|x|}\right)\tag{1}$$

For small values of $x$ we have $f(x)\approx\text{sgn}(x)|x|=x$, and for large (magnitude) values we get $f(x)\approx\text{sgn}(x)$, i.e., clipping.

This is a plot of the corrected non-linearity $f(x)$ (WolframAlpha): The formula should also be simplified like the right-most expression in $(1)$, because a beginner might be inclined to literally implement the other formula and try to evaluate the terms $x/|x|$ and $x^2/|x|$, which is unnecessarily complex and also gives trouble when $x$ is close to zero. A typical implementation would look like this:

if (x > 0)
y = 1 - exp(-x);
else
y = -1 + exp(x);
end

• Oh yeah the book misquotes web.archive.org/web/20070826204128/http://www.notam02.no/… and the above is the correct formula. Feb 21, 2016 at 21:33
• OK, thanks. Do you think that this was the book's source? Feb 22, 2016 at 8:29
• Yes the book referenced that student thesis. There was a second Norwegian student thesis that had the wrong formula and cited the first student thesis. I didn't bother to check dates to see if the book copied the second thesis without checking the original source or if the second thesis copied the book. Feb 22, 2016 at 8:38
• @OlliNiemitalo: Typical case of error propagation. I'm also not sure why they use silly expressions like $x^2/|x|$. As I've added to my answer, some beginner might end up implementing this literally. Feb 22, 2016 at 8:40

You can write the body of the function directly into Wolfram Alpha and it plots it: It looks like a waveshaper to me, and those can be used as you describe. But there was an error in the formula, see @Matt L.'s answer.

• Now that I see your plot, I'm quite convinced that the formula in the book is wrong. See my answer. What do you think? Feb 21, 2016 at 19:54
• @MattL.Yes that makes much more sense. The book's function is also descending which would cause an unwanted phase inversion. Feb 21, 2016 at 20:07