Gain compensation for asymmetrical waveshaping

For general discussion related FlowStone
Post Reply
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Gain compensation for asymmetrical waveshaping

Post 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 17773 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.
Last edited by Perfect Human Interface on Tue Mar 15, 2016 10:37 pm, edited 1 time in total.
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Inverting an unknown function

Post by adamszabo »

if you post the formula you have, one can easily get the inverted function
User avatar
martinvicanek
Posts: 1334
Joined: Sat Jun 22, 2013 8:28 pm

Re: Inverting an unknown function

Post 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 17744 times

The resulting level is somewhat higher than the input level. This is because we are using only a first order Taylor expansion.
Attachments
RestoreLevel.fsm
(13.39 KiB) Downloaded 990 times
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Inverting an unknown function

Post 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.
User avatar
martinvicanek
Posts: 1334
Joined: Sat Jun 22, 2013 8:28 pm

Re: Inverting an unknown function

Post 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.
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Inverting an unknown function

Post 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.
noisenerd
Posts: 69
Joined: Sun Feb 14, 2016 11:31 pm

Re: Inverting an unknown function

Post 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
Perfect Human Interface
Posts: 643
Joined: Sun Mar 10, 2013 7:32 pm

Re: Inverting an unknown function

Post 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.
Post Reply