Page 1 of 1

Gain compensation for asymmetrical waveshaping

Posted: Sun Mar 13, 2016 3:24 am
by Perfect Human Interface
I'm working on something where I'd like to compensate for the output level dropping when a parameter is turned up.

I've measured the curve to look about like this (using a sine wave input and peak detection on the output), where the x-axis is the parameter value and the y-axis is the output level.
transfer.PNG
transfer.PNG (2.1 KiB) Viewed 17771 times


I know you can invert a function like this with 1 - x but I don't know what the function actually is mathematically speaking.

Might anyone be able to help me figure it out? I'm looking for a close approximation.

Edit: If it helps, what I'm doing exactly is pushing a sine wave into a tanh transfer function with a DC offset. As the offset increases the sine gets squished. Then the offset is removed, and the resulting output level is reduced.

Re: Inverting an unknown function

Posted: Sun Mar 13, 2016 10:15 am
by adamszabo
if you post the formula you have, one can easily get the inverted function

Re: Inverting an unknown function

Posted: Sun Mar 13, 2016 3:21 pm
by martinvicanek
Hm, let me see, you are doing this:

output = tanh(input + DC)

correct? Then Taylor expansion to first order gives

output = tanh(DC) + input/cosh^2(DC)

so the output has some offset tanh(DC) and there is a gain reduction by the factor cosh^2(DC). So you want to remove the offset (I'd use a DC blocker filter for that) and multiply by the factor cosh^2(DC) to make up for the gain loss.
Something like this:
capture.png
capture.png (30.17 KiB) Viewed 17742 times

The resulting level is somewhat higher than the input level. This is because we are using only a first order Taylor expansion.

Re: Inverting an unknown function

Posted: Sun Mar 13, 2016 9:14 pm
by Perfect Human Interface
Wow, couldn't have asked for a more straightforward answer. Thanks Martin!

martinvicanek wrote:The resulting level is somewhat higher than the input level. This is because we are using only a first order Taylor expansion.


Is there more that can be done about that? I understand that the asymmetry will create higher peaks inherently but if I could get it closer to matching perceived volume that would be cool.

Re: Inverting an unknown function

Posted: Mon Mar 14, 2016 5:05 am
by martinvicanek
Perfect Human Interface wrote:[...]get it closer to matching perceived volume[...]
That's possible if you know the input signal amplitude. For instance if A denotes the input RMS, then third order Taylor expansion yields the following expressions for the output DC offset and the gain, respectively:

offset = tanh(DC)*[1 - A^2/cosh^2(DC)]

gain = [1 + A^2*(tanh^2(DC) - 1/3)]/cosh^2(DC)

Note that if you set A = 0 you recover the results of my first post above, so the terms with A are the correction to that.

Re: Inverting an unknown function

Posted: Tue Mar 15, 2016 6:50 pm
by Perfect Human Interface
Thank you for this. I'm glad there are people out there who've studied math. :lol:

It seems to me that with varying input and other parameters in play this is never going to be exact. Though with the fix you recommended earlier Martin the project is already functioning much better.

I was also considering adding some different waveshaping functions besides tanh, possibly even user-custom ones, which I'm afraid may just complicate this beyond hope.

If I can throw this out there, does anyone know if there's a "better" way to apply asymmetry to waveshaping? I know that there are custom waveshapers out there like this where you can edit the positive and negative independently, which is powerful, but I just wanted to use a knob.

Re: Inverting an unknown function

Posted: Tue Mar 15, 2016 7:32 pm
by noisenerd
Perfect Human Interface wrote:If I can throw this out there, does anyone know if there's a "better" way to apply asymmetry to waveshaping?


I don't know about "better" necessarily (depends what you're going for), but here's some links that both have parts that talk about asymmetrical distortion/waveshaping:

https://ccrma.stanford.edu/~dtyeh/papers/DavidYehThesissinglesided.pdf

http://www.music.mcgill.ca/~ich/classes/dafx_book.pdf

Re: Inverting an unknown function

Posted: Tue Mar 15, 2016 10:36 pm
by Perfect Human Interface
Thank you for that. Massive amounts of stuff there. The second link provides an example code for simulating (asymmetrical) tube distortion so that's cool. I am thinking I will want to use different waveshape functions though and keep the asymmetry control.

Is it possible to translate the function vertically instead of using DC offset on the audio?

I might try something else like logarithmically scaling positive and negative waveshape functions separately.