10

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.

n.gaurav
  • 3
  • 4
Elior
  • 231
  • 1
  • 3
  • 11
  • 2
    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. – Matt L. Feb 21 '16 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 08 '20 at 01:13

3 Answers3

11

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):

enter image description here

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
Matt L.
  • 89,963
  • 9
  • 79
  • 179
  • Oh yeah the book misquotes https://web.archive.org/web/20070826204128/http://www.notam02.no/~rbendiks/Diplom.html#Overstyring and the above is the correct formula. – Olli Niemitalo Feb 21 '16 at 21:33
  • OK, thanks. Do you think that this was the book's source? – Matt L. Feb 22 '16 at 08: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. – Olli Niemitalo Feb 22 '16 at 08:38
  • 1
    @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. – Matt L. Feb 22 '16 at 08:40
5

|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.

pichenettes
  • 19,413
  • 1
  • 50
  • 69
  • thanks for the answer. is it true that to get a real distortion I need to mix the formulated signal with the original one and then output the result signal? – Elior Dec 12 '13 at 10:17
  • 1
    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? – pichenettes Dec 12 '13 at 10:31
  • In audio applications, it is common to provide an option to balance the level of the original signal and the distorted signal ; so you'd have (1 - alpha) x + alpha f(x) – pichenettes Dec 12 '13 at 10:32
  • I want to create a digital distortion effect. so basically I just investigate this thing right now.. – Elior Dec 12 '13 at 10:42
  • by real distortion I mean to get a guitar distortion effect.. this formula just get the sampled values and changes them to another values. so I get a different signal, right? it means that if I convert it again to analog, I get a different amplitude from the original signal. so because the amplitude is different (in some sampled it was greater than the original), I get the distortion effect? – Elior Dec 12 '13 at 10:46
  • 1
    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. – pichenettes Dec 12 '13 at 11:00
  • another question, is it better with Frequency domain or time domain? I mean, now in the above post, actually 'x' is in time domain. so if I run the FFT I get the frequency spectrum (which is the amplitude, no?) so if I calculate the formula on the spectrum values i'll get a different frequency spectrum - which determines the distortion. after that I need to convert it back to the time domain and then to analog? – Elior Dec 13 '13 at 21:23
  • 1
    You have to do this in the time domain. – pichenettes Dec 13 '13 at 22:56
  • and the reason for this, is because the effect is a real-time process? or there is another reason why this is have to be in the time domain. sorry for all these question, I'm new with DSP. – Elior Dec 14 '13 at 01:20
  • 1
    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... – pichenettes Dec 14 '13 at 08:39
  • So all the guitar effects, like echo, reverb, fuzz, panning, and other are made in time domain when I want to emulate them digitally? – Elior Dec 14 '13 at 11:07
  • The ones you mention, yes... – pichenettes Dec 14 '13 at 11:25
  • thanks a lot! :) can you mention some examples of digital effect that are in frequency domain? again, tanks a lot! :) – Elior Dec 14 '13 at 11:35
  • 1
    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). – pichenettes Dec 14 '13 at 12:33
  • excellent info for dsp beginner thanks :) – some_id Dec 18 '13 at 00:34
2

You can write the body of the function directly into Wolfram Alpha and it plots it:

enter image description here

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.

Olli Niemitalo
  • 13,491
  • 1
  • 33
  • 61
  • 1
    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? – Matt L. Feb 21 '16 at 19:54
  • 2
    @MattL.Yes that makes much more sense. The book's function is also descending which would cause an unwanted phase inversion. – Olli Niemitalo Feb 21 '16 at 20:07