Thursday, November 21, 2019

audio - Add odd/even harmonics to signal?


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.



Answer



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?



No comments:

Post a Comment

digital communications - Understanding the Matched Filter

I have a question about matched filtering. Does the matched filter maximise the SNR at the moment of decision only? As far as I understand, ...