How do I add odd or even harmonics to a floating point signal?

Do I have to use tanh or sin?

What I'm trying to do is achieve some very simple distortion effects, but I'm having a hard time finding exact references. What I'd like is something similar to what the Culture Vulture does by adding odd and even harmonics in its pentode and triode settings. The float value is a single sample in a sample flow.

  • 3
    $\begingroup$ Why do you want to add harmonics? What is it that you are trying to accomplish? What kind of signal are you working with? $\endgroup$
    – Jim Clay
    Nov 10, 2012 at 21:39
  • $\begingroup$ What i'm trying to do it's achieve some very simple distortion effects, but i'm having a hard time finding exact references. What i'd like is something similar to what the culture vulture does by adding odd and even harmonics in it's pentode and triode settings, the float value it's a single sample in a sample flow. $\endgroup$ Nov 11, 2012 at 10:28
  • 1
    $\begingroup$ @CarlosBarbosa You should edit that information from the comments in to your question. Provide details -- the more interesting the question is for the community, the more answers you can expect, as well as answers of better quality. $\endgroup$
    – penelope
    Nov 16, 2012 at 9:33
  • $\begingroup$ why the odd harmonics are more danger than even harmonic on the power system $\endgroup$
    – user17307
    Sep 8, 2015 at 14:02

2 Answers 2


What your distortion box does is apply a non-linear transfer function to the signal: output = function(input) or y = f(x). You're just applying the same function to every individual input sample to get the corresponding output sample.

When your input signal is a sine wave, a specific type of distortion is produced called harmonic distortion. All of the new tones created by the distortion are perfect harmonics of the input signal:

  • If your transfer function has odd symmetry (can be rotated 180° about the origin), then it will produce only odd harmonics (1f, 3f, 5f, ...). An example of a system with odd symmetry is a symmetrically-clipping amplifier.
  • If your transfer function has even symmetry (can be reflected across the Y axis), then the harmonics produced will only be even-order harmonics (0f, 2f, 4f, 6f, ...) The fundamental 1f is an odd harmonic, and gets removed. An example of a system with even symmetry is a full-wave rectifier.

So yes, if you want to add odd harmonics, put your signal through an odd-symmetric transfer function like y = tanh(x) or y = x^3.

If you want to add only even harmonics, put your signal through a transfer function that's even symmetric plus an identity function, to keep the original fundamental. Something like y = x + x^4 or y = x + abs(x). The x + keeps the fundamental that would otherwise be destroyed, while the x^4 is even-symmetric and produces only even harmonics (including DC, which you probably want to remove afterwards with a high-pass filter).

Even symmetry:

Transfer function with even symmetry:

y = x^6 transfer function

Original signal in gray, with distorted signal in blue and spectrum of distorted signal showing only even harmonics and no fundamental:

y = x^6 spectrum

Odd symmetry:

Transfer function with odd symmetry:

y = x^7 transfer function

Original signal in gray, with distorted signal in blue and spectrum of distorted signal showing only odd harmonics, including fundamental:

y = x^7 spectrum

Even symmetry + fundamental:

Transfer function with even symmetry plus identity function:

y = x + x^4 transfer function

Original signal in gray, with distorted signal in blue and spectrum of distorted signal showing even harmonics plus fundamental:

y = x + x^4 spectrum

This is what people are talking about when they say that a distortion box "adds odd harmonics", but it's not really accurate. The problem is that harmonic distortion only exists for sine wave input. Most people play instruments, not sine waves, so their input signal has multiple sine wave components. In that case, you get intermodulation distortion, not harmonic distortion, and these rules about odd and even harmonics no longer apply. For instance, applying a full-wave rectifier (even symmetry) to the following signals:

  • sine wave (fundamental odd harmonic only) → full-wave rectified sine (even harmonics only)
  • square wave (odd harmonics only) → DC (even 0th harmonic only)
  • sawtooth wave (odd and even harmonics) → triangle wave (odd harmonics only)
  • triangle wave (odd harmonics only) → 2× triangle wave (odd harmonics only)

So the output spectrum depends strongly on the input signal, not the distortion device, and whenever someone says "our amplifier/effect produces more-musical even-order harmonics", you should take it with a grain of salt.

(There is some truth to the claim that sounds with even harmonics are "more musical" than sounds with only odd harmonics, but these spectra aren't actually being produced here, as explained above, and this claim is only valid in the context of Western scales anyway. Odd-harmonic sounds (square waves, clarinets, etc.) are more consonant on a Bohlen–Pierce musical scale based around the 3:1 ratio instead of the 2:1 octave.)

Another thing to remember is that digital non-linear processes can cause aliasing, which can be badly audible. See Is there such a thing as band-limited non-linear distortion?

  • $\begingroup$ Note that the example functions here make the math simple to understand, but are not typically used in audio stuff. With x^7, for instance, the signal becomes less distorted the more you crank up the gain. $\endgroup$
    – endolith
    Sep 12, 2018 at 21:50

What you trying to achieve is called distortion. This techniques used when you want to add some harmonics to given signal. You have 2 basic methods to do this: waveshaping and ring modulation.I'll try to explain first one.


Waveshaping allows you to make distortion via use of specially selected function. One of useful methods is Chebyshev polynomials. They have a very important property when filing through them harmonic signal with unit amplitude (for example, a sine wave), we obtain the same signal, only a few times higher. Frequency multiplier will depend on the order of the polynomial. All polynomials looks like this:

$$ \ y = f(x) =d_0 + d_1x + d_2x^2 + d_3x^3 +… + d_Nx^N; $$

In our case, each element generates a harmonica, and then they all add up. View of each member is determined by the following recurrence relation:

$$ T_{k+1}(x) = 2xT_k(x) – T_{k–1}(x); $$

In it, each member is determined based on the previous one, it all begins with a zero, in our case it is equal to one, and the first, which is equal to x( but you can change it, ofcourse) $$ T_0(x) = 1; $$

$$ T_1(x) = x; $$

Knowing them, you can determine the third and forth: $$ T_2(x) = 2x*x – 1 = 2x^2 – 1; $$

$$ T_3(x) = 2x(2x^2 – 1) – x = 4x^3 – 3x; $$

As you might guess, the second term - the first harmonic, and the third - the second and so on.

Another feature of the Chebyshev polynomials, when through them gives a signal whose amplitude is less than the unit, the output is less saturated sound with harmonics. This allows to create overdrive effect.

After all, your signal is an array of floating points, you can choose any part of your array and apply to them Chebyshev polynomials, which will create additional harmonics. And using $sin$ functions will be good enough for this.

  • $\begingroup$ Nice answer, learned something here. However, I don't agree with your usage of the term transfer function. Its common definition is the output to input relation of an linear time-invariant system in frequency domain. Your system is non-linear. I would rather call it characteristic or just function here. $\endgroup$
    – Deve
    Nov 11, 2012 at 14:44
  • $\begingroup$ @Deve Thank you. Yes indeed I used incorrect term, just function good enough. I was thinking to write example of linear system, but it's quite straightforward, so term remained in my thoughts $\endgroup$
    – sigrlami
    Nov 11, 2012 at 15:02
  • $\begingroup$ Wow, thanks for all this i will be reading though a lot it seems, any chance of some example c code? thanks once again $\endgroup$ Nov 11, 2012 at 23:09
  • $\begingroup$ Can you please expand on how exactly the equations with $T_0(x)$, $T_1(x)$ etc are relating back to the original equation with $y$?... $\endgroup$
    – Spacey
    Nov 11, 2012 at 23:22
  • $\begingroup$ @Mohammad they are not related exactly, it's just simple description of polynomial function if topic starter doesn't know it. $\endgroup$
    – sigrlami
    Nov 12, 2012 at 11:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.