# Step by step saturation/distortion effect math notation to reasonable c++ code

Almost every DSP resource for making saturation/distortion effects I can find shows the mathematical representation of the algorithm, or some kind of a diagram but isn’t nearly as intuitive for writing pseudocode and then going into reasonable C++. Assuming that I am using JUCE’s stock filters, can someone show me an example of the math notation being broken down into code? I know that for example the DFT can’t really be done by a computer, and concepts like summation, differentiation, and integration work much differently on discrete values.

2 Likes

For a start, try to ask more precise questions. Edit: The original post was edited/replaced, which makes my comment now appear inappropriately snarky. For context, the original version (which also had a different title) is available via the pencil icon on the OP.

2 Likes

i struggle with that too. however i just googled “how to block diagram” and found some pages like this one https://www.tutorialspoint.com/control_systems/control_systems_block_diagrams.htm

seems like we should check that out soon. it’s so annoying not to be able to read all those great resources

A simple (and quite nice-sounding) saturation effect can be created with the hyperbolic tangent function:

`output = tanh(gain * input)`

But any function like this which maps an unbounded input signal to an output in the range [-1,1] will produce some sort of saturation/distortion. To name a few others:

`output = mod(gain*input+1,2)-1`

`output = abs(mod(2*gain*input+2,4)-2)-1`

`output = sin(gain*input)`

In general, “smoother” functions sound softer, more like saturation, and rough/discontinuous functions sound harsher, like distortion.

Note: `mod` is the modulo function, equivalent to the following in C/C++:

``````float mod (float n, float d) {
n = fmod(n, d);
if (n<0) n += d;
return n;
}
``````

1 Like

Taking the cube of each input sample gives a nice overdrive effect.

`output = input * input * input;`

Matt

There were some good starting points mentioned above which all will give you more or less pleasing sounding distortion. Go and implement them, all the examples above should be very easy to implement sample-wise and listen to their results. I would also suggest to put a scope and spectral analyzer plugin after your distortion, feed a sine wave into the distortion and look at the results.

However after having done so, I would strongly suggest you to really learn some DSP math basics, to get an idea why you are hearing and seeing those results. In my opinion, if you really understood the link between continuous and discrete time signals, a math notation of any DSP algorithm will immediately become clearer. If you understand the sampling theorem, signal representation in the time and frequency domain and things like aliasing, then you’ll sooner or later get an idea of why most good sounding distortion effects use oversampling. If you understand how filters work, you’ll get a better idea of how to use them to shape your distortion sound. And so on… I think you’ll get the point: Understanding the math helps you to get creative in the end and make up your unique, complex distortion algorithm instead of repeating simple (pseudo) code examples.

And one last point:

This is simply not true – sorry  In fact a Discrete Fourier Transformation is the kind of Fourier Transformation that CAN be done by a computer (in most cases you’ll use an FFT algorithm to compute it). So again, I strongly advise you to have a basic read on continuous vs. discrete time signals.

By the way, I learned those thing “the hard way” at university, however I think there are a lot of people here with a different background that learnt all those stuff at some point, maybe they have some resources for you that explain all these things in a less theoretical way?