
How Distortion Works in Music - beeeeeeeeeeeeen
https://benmosheron.gitlab.io/blog/2020/04/26/distortion.html
======
stagas
That's cool, but it doesn't really explain _why_ the sound becomes more
interesting. It has to do with how the sound is played back, in short, there
are no "square" waves. Everything is a combination of sine waves. The more you
have it's as if there are more instruments playing on top of each other, in
music they're called "harmonics". A "squared" wave is a wave with many many
additional sine waves in different frequencies that overlap with each other,
if you were to plot it it wouldn't look square, rather it would be wavy. This
article shows visually how this effect takes place, if you are interested:
[http://www.jezzamon.com/fourier/](http://www.jezzamon.com/fourier/)

~~~
hwillis
> Everything is a combination of sine waves.

No, we _hear_ things as if they were a combination of sinusoidal waves
(mostly). That's only one way to represent them; you can just as easily
represent them as a series of impulses. Mathematically they're all equivalent
models, and none of the models accurately represent acoustic transfer.

You can say we perceive things as if they were a linear combination of
sinusoidal waves because we have a series of hair cells in the cochlea. Each
bundle of hairs responds to waves within a very narrow frequency range, so
that's what we mainly respond to. All of our audio perception is filtered
through this process first. Since the hair bundles have a specific audio
response, they don't respond to instantaneous frequencies- there has to be a
wave of a certain duration for us to perceive a frequency. That's even
independent of the mathematical fact that shorter wave pulses are more
indistinguishable from white noise. Also, all of this is ignoring the other
equipment of the ear, like the eardrum and the bone lever[1] that transmits
sound to the inner ear.

We have a lot of brain circuitry that picks out specific features, which means
we can recognize things like square waves or impulses. I don't know much about
this stuff but it's important for things like cochlear implants. AFAIK it's
similar to some optical illusion/visual perception phenomena- things like how
we perceive magenta as a single color, even though it's just two colors
together, or how we perceive orange as a distinct color from brown even though
it isn't[2].

> The more you have it's as if there are more instruments playing on top of
> each other, in music they're called "harmonics".

Symmetrical clipping and square waves both create odd-order harmonics (3x, 5x,
7x, 9x, etc) which are very unnatural. The brain is good at picking this kind
of thing out, and while I find it enjoyable (eg chiptunes), it's not really
comparable to normal harmonics in music. It's certainly not something you can
simplify down to just the component sinusoids. The perception of sounds has
way more to do with stuff happening in the brain than in the air.

[1]:
[https://en.wikipedia.org/wiki/Ossicles#/media/File:Slide1ghe...](https://en.wikipedia.org/wiki/Ossicles#/media/File:Slide1ghe.JPG)

[2]:
[https://en.wikipedia.org/wiki/Shades_of_orange](https://en.wikipedia.org/wiki/Shades_of_orange)

~~~
blincoln
> Symmetrical clipping and square waves both create odd-order harmonics (3x,
> 5x, 7x, 9x, etc) which are very unnatural.

What do you mean by "unnatural"? A clarinet has mostly odd harmonics[1], for
example. Clarinets don't occur in nature, I suppose, but they're not using
electronics or anything either.

[1]
[https://newt.phys.unsw.edu.au/jw/clarinetacoustics.html#harm...](https://newt.phys.unsw.edu.au/jw/clarinetacoustics.html#harmonics)

~~~
dsego
Probably even harmonics are more rounded or pleasing and less harsh. Compare
trumpet vs cornet.

------
korethr
So, that 2nd sound with the grouping of sine waves that looks like it's
starting to form a saw wave is actually rather close to the natural signal of
an electric guitar.

When installing pickups into one of my guitars several years ago, I decided I
wasn't going to be satisfied by adjusting the height of the pole pieces by
ear, nor was I going to be satisfied with with matching the height of the pole
pieces to the radius of the fret board, then adjusting by ear. No, I had a
hacker space membership and access to a 100Mhz oscilloscope, so I was going
match the pole piece height to the fretboard radius, and then use the
oscilloscope to tweak pole piece height until the output of each individual
string matched amplitude with the others.

So there I was, with my guitar running into the oscilloscope, doing my
damnedest to pick each string with equal force and watching the amplitude on
the scope. And I noticed a curious thing. The initial pluck was a squiggly,
kinda-saw-wave-shaped thing, and then as the note decayed, became something
more akin the 2nd graph in the OP, and then slowly turned into something
rather like a sine wave as the note faded.

What happens, is upon picking, there's a _lot_ of higher order harmonics, and
other high frequency content, and those higher frequencies quickly decay, and
soon you're dealing with mostly just the fundamental and its lower order
harmonics.

~~~
qmmmur
You just described spectral envelopes.

~~~
aldanor
What's so surprising? The initial pluck is dominated by the short percussive
sound of actually plucking the string, which quickly fades away so what's left
is the fundamental sine-like wave. As noted below, spectral envelopes.

------
vnorilo
Somewhat related, my colleague has studied the interplay between distortion
harmonics and chordal harmony in metal music:

[https://pdfs.semanticscholar.org/b2e2/c20f8fe3ad45d39bc8f562...](https://pdfs.semanticscholar.org/b2e2/c20f8fe3ad45d39bc8f5623b50100338e35c.pdf)

~~~
hrnnnnnn
This is really interesting!

One way to get a clearer sound with modern high-gain metal tone is to record
each note of a chord running through the amp individually. (I can't remember
exactly, but I think this avoids "intermodulation" distortion within the amp
itself.)

~~~
ubercow13
I wonder, has anyone tried making a guitar that outputs the signal from each
string's pickup coils separately, and then an amplifier that distorts each
signal separately?

edit: Looks like this might be exactly that:
[https://www.youtube.com/watch?v=9EUbO59OO_Y](https://www.youtube.com/watch?v=9EUbO59OO_Y)

~~~
hrnnnnnn
Martin from Wintergatan (of Marble Machine fame) has made a bass guitar with a
separate pickup and output for every string.

This lets him do things like mute one string when another starts playing in
software, as he won't always have his hands on the bass to be able to mute the
non-playing strings.

[https://www.youtube.com/watch?v=rvL83-iy-
EQ](https://www.youtube.com/watch?v=rvL83-iy-EQ)

~~~
kchr
Wow, thanks for sharing.

------
tuzemec
Dan Worrall's videos for FabFilter about distortion and saturation are great
too:

[https://www.youtube.com/watch?v=erv4lit4aWY](https://www.youtube.com/watch?v=erv4lit4aWY)

[https://www.youtube.com/watch?v=NO2OZ3UTy2k](https://www.youtube.com/watch?v=NO2OZ3UTy2k)

Actually all the videos of Dan are amazing.

------
smcameron
It's interesting to look at some circuits for guitar distortion stomp boxes
like the ProCo Rat[1] or Boss DS-1[2] or others on that eletrosmash site. Also
guitar cabinet frequency response drops _significantly_ after 10kHz so that
rounds high frequency sharp corners off a bit (try listening to the direct
signal from a distorted guitar preamp vs. the same signal through various
cabinet impulse responses sometime, or hook up an oscilloscope across the
output of a guitar multieffects box on various distortion settings. Nary a
square wave to be seen.

[1] [https://www.electrosmash.com/proco-
rat](https://www.electrosmash.com/proco-rat) [2]
[https://www.electrosmash.com/boss-
ds1-analysis](https://www.electrosmash.com/boss-ds1-analysis)

------
paramost
This is very interesting and a great interactive demo. Could you make it so
that when you press another sine wave, the other one stops playing? I
accidentally had a couple things playing at once.

------
blt
I'm not a DSP person, but I thought the term "transfer function" was more
typically used in reference to Laplace transforms of linear time-invariant
dynamical systems. Is it also common to use it for state-space mappings like
the ones in this article?

~~~
TheOtherHobbes
Yes, these are straight lookup tables/mappings, not transfer functions, which
use complex math - and s-plane to z-plane transforms for DSP - and can
implement various filter shapes.

This is a good entry level guide to distortion, but it misses a few points.

The first is that any non-linear mapping will introduce aliasing which folds
back below the sample rate. The more clipping, the more aliasing you get. In
commercial DSP you deal with this by oversampling, applying the distortion,
and then filtering and downsampling to remove the aliasing.

If you don't do that, the sound that comes out is nasty and crude with none of
the creamy warmth of the real thing.

The other is that high-quality amp distortion emulations - Kemper, etc - are
more like real transfer function emulations. They're also level-dependent, so
the mapping that comes out isn't one-size-fits all and responds to dynamics.

That doesn't mean delay/echo/reverb, it means a network of very short delays
characterised by a Volterra series and/or some form of convolution which
models the dynamic tone characteristics of a real guitar amp.

~~~
cochne
Would the non-linear mapping actually produce aliasing though, since it's in
the discrete time domain? My understanding is that you only get aliasing when
converting from analog to digital. While the discrete time sequence does map
to higher frequency components (imaging) I would think this would need be
filtered in the analog domain (the speaker's D2A converter)

------
myself248
Many many years ago, as my little sister was getting into rock and roll, I
mentioned my theory that one reason American teenagers gravitate towards it,
is that the sound of a distorted guitar resembles the sound of an engine, and
that cars symbolize freedom and coming-of-age in our society.

"Okay, so what exactly do you mean by 'distorted', anyway? I hear that term
thrown around..."

So we sat down in front of dad's stereo, turned it nice and up, and I played
her the intro to _Sorrow_:
[https://www.youtube.com/watch?v=AdKNlGfkyhc](https://www.youtube.com/watch?v=AdKNlGfkyhc)

2 minutes and 14 seconds later: "Oh. Okay. I think I get it now."

...

"You got any more of that?"

~~~
ilamont
That's an interesting theory. But wouldn't the same people who like distorted
guitar also like the sound of sports cars and motorcycles? I know there's some
overlap, or at least many motorcycle owners listen to rock, but among the
total population of rock fans are relatively few motorcycle owners.

(Anecdata: committed rock fan since getting Boston's "More than a Feeling" on
45 in the mid 70s, later played in several punk and hard rock bands ... but I
can't stand the sound of Harleys or loud engines)

------
bambax
So, can we think about distortion as a kind of machine harmonization?

And if yes, can we consider another kind of machine harmonization which would
be to add notes to create wanted harmonies, based on an incoming note
(modulating velocity, and maybe timing, so as not to overpower the original
note)?

And if yes, this could be called "MIDI distortion"... Are there MIDI plugins
that do this?

I recently started making (very simple) plugins in the Reaper DAW; it would be
possible to do such MIDI harmonization in JSFX. I'm wondering if it's been
done already.

~~~
ioseph
Within Midi you are limited to 128 notes. Within this range you could
certainly produce some interesting chordal effects (one thought would be
octaves and fifths phasing in and out).

And there are many patches out there to do this sort of thing.

But you are far better off processing the audio signal for any kind of
interesting modulations.

~~~
themarkn
Just a note With MIDI 2.0 we now gone from 7-bit to 32-bit which makes a
huuuge difference in terms of what can be achieved, no more 128-note limit!

~~~
kchr
We've been waiting 30+ years for this... At last!

------
TrackerFF
About the wave shaping, if some posters have a hard time visualizing how the
mapping looks like:

Imagine that you have a (pure) input sine-wave coming up on the y-axis. It
then hits some function on the x-y plane, and then gets "reflected" (mapped)
onto the x-axis.

This imagine explains it quite well, and in this case there's a linear
transfer characteristic / operating curve, with no distortion of input signal:
[https://www.tutorialspoint.com/amplifiers/images/input_cycle...](https://www.tutorialspoint.com/amplifiers/images/input_cycle.jpg)

Now - instead of imaging a perfect linear operating curve, think of how the
curve of a diode looks like, and how that would clip the upper curve of input.

Now put another diode for the reverse, and you're also clipping the bottom
curve of input - this would be a basic diode clipping circuit, which is the
fundamental drive-stage for distortion pedals like tube screamers - you'll
often see a pair of diodes going opposite directions.

(In electronics classes, you tend to learn how to NOT generate distortion - or
rather, how to amplify stuff with least amount of distortion possible.)

~~~
romwell
TL;DR: _Wave shaping_ is just _function composition_.

I don't know why a new term is needed, but if you can visualize function
composition for simple functions like _floor, min, max_ , you can visualize
wave shaping.

If the input signal is _f(x)_ , you obtain clipped signal _g(x)_ like this:

    
    
        g(_) = min(1, _) ○ max(-1, _) ○ (a * _) ○ f(_)
    

This can also be written as

    
    
        g(x) = min(1, max(-1, a * f(x)))
    
    

In the example in the article, _T(x) = min(1, max(-1, ax))_ is called a
_transfer function_.

I find it easier to understand function composition by breaking the functions
down into simple parts, though.

The _sonic_ effects of this are completely unexplained in the article. Other
comments here discuss the effects on _harmonics_ ; mathematically, the TL;DR
is that the Fourier series of _g = t ○ f_ are more interesting (the intuition
_why_ can be obtained by looking at the Fourier series for a square wave).

Without getting into math: a clipped signal retains some of the input, and
adds more of square wave characteristics. A square wave is the buzz you get by
flipping + and - very rapidly, which will force the speaker cone to go all the
way in/out very rapidly.

The reason why math above doesn't fully represent the effect is that an actual
speaker cone won't be able to teleport to min/max positions instantaneously;
and the unfaithfulness of the speaker cabinet creates pleasing tonal
characteristics.

------
ben7799
Cool article. Pretty straightforward but very cool to see the interactive
examples.

Interestingly I found the simple "clipped" and "clipped/boosted" functions by
far the most pleasing.

I doubt it's a coincidence that's what most analog FX are doing.

The distinctions between these are usually murky but in guitar FX:

Boost - Boost Overdrive - boost + soft clipping Distortion - boost + hard/soft
clipping Fuzz - boost + mostly hard clipping

All the effects are generally doing one or more of the following: \- Boost
signal through a transistor till the transistor saturates/cuts off and clips
\- Do the same with an OP-AMP \- Use diodes to do an extra clipping stage

In analog audio the signal is literally an alternating current... the "values"
in digital audio correspond to +/\- voltage levels. So his transfer functions
are trying to mimic what electronic components do when they hit their limits.
The transfer functions can also do things that are probably impossible in the
analog world.

There's a lot of weird non-linear stuff that seems to bring out harmonics as
well.

------
skrebbel
One kind of distortion that I loved to apply in my FastTracker II days, and
which I don't see used all that often, is to flip the sign.

In short, FT2's sample editor could handle a few common waveform formats, but
this was cross platform days and there were a _lot_ of wave formats. These
have differnt headers and different structures, but many of them ended up
being just a series of samples, either in 8-bit or 16-bit, and at some sample
frequency. To deal with this, FT2 added a button that let you reinterpret a
waveform as if it was stored signed or unsigned, and one that let you
reinterpret as 8/16 bit. This let it load a _huge_ amount of sample formats
well enough; you'd fiddle around with those buttons until it sounded right,
then you cut the first few millisecs of noise (the file format header) off the
front, and done!

But turns out that the "reinterpret as (un)signed" button (called "Conv" in
FT2) is pretty spectacular for nasty distortion too.

Imagine a sine wave. Loaded correctly, it'll look like this:

    
    
         +-----------------------------------------------------------------------------+
         |                                                                             |
         |                                                                            X|
         |                  XXXXX                                                   XXX|
         |               X       XX                                                X   |
         |              X          XX                                            XX    |
         |                                                                      X      |
         |           X               XX                                        XX      |
         |                             X                                      X        |
         |         X                   X                                    X          |
         |                              X                                 XX           |
         +-----------------------------------------------------------------------------+
         |      X                        X                              X              |
         |     X                          X                            X               |
         |    X                           X                           X                |
         |                                 X                        XX                 |
         |   X                             XX                      X                   |
         |                                  X                    X                     |
         |  X                                X                 X                       |
         | X                                 XX            X XX                        |
         | X                                  XXXXXX X  XXX                            |
         |                                                                             |
         +-----------------------------------------------------------------------------+
    
    

Imagine this was stored as 8-bit signed. Now, if you reinterpret those bytes
as unsigned, it'll look like this:

    
    
         +-----------------------------------------------------------------------------+
         |      XX                      XX                              XX             |
         |     X X                      X X                            X X             |
         |    X  X                      X X                           X  X             |
         |       X                      X  X                        XX   X             |
         |   X   X                      X  XX                      X     X             |
         |       X                      X   X                    X       X             |
         |  X    X                      X    X                 X         X             |
         | X     X                      X    XX            X XX          X             |
         | X     X                      X     XXXXXX X  XXX              X             |
         |       X                      X                                X             |
         +-----------------------------------------------------------------------------+
         |       X                      X                                X             |
         |       X                      X                                X            X|
         |       X          XXXXX       X                                X          XXX|
         |       X       X       XX     X                                X         X   |
         |       X      X          XX   X                                X       XX    |
         |       X                      X                                X      X      |
         |       X   X               XX X                                X     XX      |
         |       X                     XX                                X    X        |
         |       X X                   XX                                X  X          |
         |       X                      X                                XXX           |
         +-----------------------------------------------------------------------------+
    

Notice how the top half and the bottom half of the sine waves are swapped.

Now, I don't understand much about DSP and sound and the frequency domain.
Better programmers than me have told me that this makes no sense from an
audio/music perspective. But it sounds pretty cool; you keep a lot of the
essence of the original sound, and add a lot of nasty.

I once made a VST plugin that does this (it lets you slide the waveform up,
wrapping over to the bottom; its only parameter is by how much to slide), but
I don't think I have it anymore unfortunately.

For context, applied to a random bassdrum using 8bitbubsy's amazing Windows
clone of FT2[0]:

\- original:
[http://neuvostoliitto.nl/808_drum_kick_035_21338.wav](http://neuvostoliitto.nl/808_drum_kick_035_21338.wav)

\- distorted:
[http://neuvostoliitto.nl/808_drum_kick_fucked.wav](http://neuvostoliitto.nl/808_drum_kick_fucked.wav)

Just sharin' :-)

[0] [https://16-bits.org/ft2.php](https://16-bits.org/ft2.php)

~~~
im3w1l
> Now, I don't understand anything about DSP and sound and the frequency
> domain.

Without going into theory, just _look_ at it. It's the addition of a square
wave and a sine wave now. And a square wave has lots of harmonics, giving that
buzzy sound.

~~~
louthy
> And a square wave has lots of harmonics

A square wave is 100% distortion effectively. It's an infinite series of sine-
waves (although the infinite series isn't needed to generate a square wave,
just the sum of harmonics up to nyquist)

~~~
willis936
Well, no. In any finite range of frequencies there are infinitely more
harmonics that are in a noise signal than are in a square wave. In the audible
frequency and dynamic ranges, you realistically can’t perceive more than maybe
7 harmonics in a square wave and it sounds _very_ different than a noise
signal.

------
thekegsi
For a really cool and detailed break down of almost any audio effect checkout
the book DAFX: Digital Audio Effects

------
qrv3w
Fantastic article! I loved how the transfer functions are shown, with audio
examples! I'm going to use this for writing an effects patch for my
synthesizer :)

Speaking of synthesizers, my favorite line was "I’m building a synthesizer in
javascript at the moment". I'd love to see this!

~~~
beeeeeeeeeeeeen
That's awesome, thanks!

------
montebicyclelo
Great use of interactive visuals/audio to help explain things.

(I look forward to this becoming more common.)

------
dghughes
I recall something like this from a sound system engineering course I took in
1990. It was a course for people wanted a job of setting up speakers and
operating the mixing board. (It was supposed to be a course to be a recording
studio sound engineer... I'm still pissed)

The instructor would often mention odd transharmonic distortion. It sounds
pleasant to our ears but even transharmonic does not. That's about all we were
told about that.

Years later I was reading about brains and somehow I understood transharmonic
distortion. The article mentioned something that I now forget but it was clear
to me why we like to listen to loud rock music.

------
dkdbejwi383
It would be nice if both waveshaping graphs were side by side so I don't have
to keep scrolling up and down to change the function, and so I can see the
waveform at the same time as the transfer function.

------
allenu
Very cool demos to play around with there! I was hoping it would go into the
psychology of why distortion works, but this was still awesome.

I've found that the nice thing about distortion, especially in shoegaze music
like My Bloody Valentine, is that the noise makes what you hear sort of an
"audio illusion" in that you start picking up melodies that maybe weren't
really there. It tickles your ears. Ironically, you could say the noise
smooths out the wrinkles in the piece.

------
nydel
distortion is for pretending i meant to clip my recordings at a frequency i
didn't notice on studio monitors.

let's talk about bitcrunching! now that's fun. and definitely not the other
way i cover up poor recording!

------
traek
This is super cool. I really enjoyed the demos, and the explanation of what
distortion actually does to the waveform was very clear.

~~~
beeeeeeeeeeeeen
Thanks!

------
techbio
I enjoyed the "very noisy" setting, actually sounds like most of the radio
dial.

------
stevebmark
why are the final audio samples so terrible/quiet? did no one actually try
them out?

