Plug-ins, Hosts, Apps,
Hardware, Soundware
Developers
(Brands)
Videos Groups
Whats's in?
Banks & Patches
Download & Upload
Music Search
KVR
   
KVR Forum » DSP and Plug-in Development
Thread Read
Open distortion project
Goto page 1, 2, 3  Next
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Mon May 07, 2012 2:00 pm reply with quote
Hey folks!

Thought it would be nice to have a place share some thoughts and approximations on distortion - sigmoids and waveshapers and the like.

I'll start:


Tanh - continued fraction:
x2 = x*x;
a = (((x2+378)*x2+17325)*135135)*x;
b = ((28*x2+3150)*x2+62370)*x2+135135;
tanh_out = a/b;

Tanh - Cody & Waite's rational approximation:
x2 = x*x;
p0 = -1613.119023996228053;
p1 = -99.225929672236083313;
p2 = -0.96437492777225469787;
q0 = 4840.357071988688686;
q1 = 2233.7720718962312926;
q2 = 112.74474380534949335;

b = x2*((p2*x2+p1)*x2+p0)/(((x2+q2)*x2+q1)*x2+q0);
tanh_out = x+x*b;

Atan - rational approximation:
x2 = x*x;
x4 = x2*x2;
a = x*(105+55*x2);
b = 105+90*x2+9*x4;
atan_out = a/b;

Other useful functions:

Inverse sqrt:
out = x/sqrt((x*x)+1);

Cubic Soft Clipper: (Julius Smith)

a = clip(x,-1,1);
out = 1.5*a*(1-(a*a)*0.3333333);

My "non-boosting" sigmoid:
out = tanh(sinh(x));
//rewrite as -> out = 1-2/(exp(exp(x)-1/exp(x))+1)

Fast tanh-like 3rd term: (mentioned by C.Budde)
a = abs(x);
b = 6+a*(3+a);
out = (x*b)/(a*b+12);

Langevin Function: (mind your singularities here!)
out = coth(x) - (1/x);

My nasty symmetrical 'non-limiting' distortion: (based on asinh)
a = x*2;
out = (ln(a+sqrt(a*a)+1))*0.5;

My supressor dist:
a = sin(atan(x));
b = cos(tanh(x*x+0.4342944819));
out = atan(a/b);

Some Asym types:

My 'real' inv_sqrt clipper: (nasty)
a = clip(x,-1,1);
b = a+1;
c = a*1.8;
d = inv_sqrt(c);
out = a/(c-0.48);

My Asym nr7:
a = atan2(cosh(x),sinh(x)+1);
out = tanh(x*-a*0.32*-a)*0.56;

 


Anybody got more to share? Was also wondering about memory effects and what role they play in the analog world. Most technical papers seem to simplify most non-linear functions to a memoryless tanh(x). Was wondering if any more complex/analog examples exist. Have only seen a transistor differential amplifier on musicdsp, was wondering about JFet, OTA or just more static waveshapers in general...

I find distortion to be very interisting Smile

Andrew Ainslie
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
rootbear
KVRer
- profile
- pm
PostPosted: Mon May 07, 2012 5:47 pm reply with quote
Any hints on which of your functions you like for a particular purpose, ie soft clipping.

The only technique I have to share is a kind of approximation of some analog circuits that remain linear mostly, so you don't have to change the sample for those samples, and if it's above the range clip to clipmax (not 1 as that would be too high), otherwise apply a soft clip function and combine it with the linear portion. Another tip is to apply this kind of function twice but with a low gain, then a high gain one, there are a lot of further possibilities with that kind of thing.


   

if(samp > 1.0) samp = calcmax;
else if(samp > curvethresh)
{
   //vfloat curveamp = ;
   samp = function(samp) * calcexposcale + curvethresh;
}

^ Joined: 07 Feb 2011  Member: #249871  
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Tue May 08, 2012 5:05 am reply with quote
rootbear wrote:
Any hints on which of your functions you like for a particular purpose, ie soft clipping.

The only technique I have to share is a kind of approximation of some analog circuits that remain linear mostly, so you don't have to change the sample for those samples, and if it's above the range clip to clipmax (not 1 as that would be too high), otherwise apply a soft clip function and combine it with the linear portion. Another tip is to apply this kind of function twice but with a low gain, then a high gain one, there are a lot of further possibilities with that kind of thing.


   

if(samp > 1.0) samp = calcmax;
else if(samp > curvethresh)
{
   //vfloat curveamp = ;
   samp = function(samp) * calcexposcale + curvethresh;
}



Yeah, gain staging open alot of posibilities aswell as parralel processing. To give an example of distortions that intrigue me: let us take the audio transformer for an example - as far as I've read(and can be wrong) that they cause 1: A Highpass effect. 2: Low frequency phase-shift,(probably due to the HP) 3: Low frequencies are slightly distorted.

Now try and model that...?
Firsly - Just HP - that takes care of 1 & 2. But what about 3?
If you use a crossover then distort the low - you'll mess up the phase at the crossover frequency!

Other things that come to mind is power supply sag in tube guitar amps...

Asymmetrical Transient Distortion, Intermodulation Distortion.
JFET, MOSFET, OTA, tape, Germanium transitor distortion, triode vs pentode etc etc.

Which have the highest bleed-though, power supply sag, frequency deviation, which has memory effects and which don't?

So many Q's, anybody?
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Wed May 09, 2012 1:51 am reply with quote
Was thinking about the tansformer emulation again - maybe I could do a HP Filter @10Hz with hi-resonance - distortion in FB path only(so that mainly the res_frequency get distorted) and then EQ the resonace peak back to unity and adjust gain if need be.

Think it might work. Any thoughts?

Regards
Andrew
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
Richard_Synapse
KVRist
- profile
- pm
PostPosted: Wed May 09, 2012 4:52 am reply with quote
Ichad.c wrote:
Yeah, gain staging open alot of posibilities aswell as parralel processing. To give an example of distortions that intrigue me: let us take the audio transformer for an example - as far as I've read(and can be wrong) that they cause 1: A Highpass effect. 2: Low frequency phase-shift,(probably due to the HP) 3: Low frequencies are slightly distorted.


It depends on the transformer. The cheap ones yield a ton of artefarcts, the most prominent one might be the high-pass effect. The high-end transformers should be closer to linear performance (I haven't measured them, however, so I'm judging from data sheets here).

Richard
----
Synapse Audio Software - www.synapse-audio.com
^ Joined: 19 Dec 2010  Member: #245936  
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Wed May 09, 2012 10:28 am reply with quote
Quote:
It depends on the transformer. The cheap ones yield a ton of artefarcts, the most prominent one might be the high-pass effect. The high-end transformers should be closer to linear performance (I haven't measured them, however, so I'm judging from data sheets here).

Richard


Yip, good ones are quite linear under normal operation, but guitar-amp builders purposely drive the output transformer into core-saturation and it's confirmed - the low frequencies get distorted more than the highs. Now I wonder if there is any memory effect? DC Leakage?

A random read:
http://milbert.com/articles/tubes_vs_transistors

Andrew
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Wed May 09, 2012 1:25 pm reply with quote
Random Note:

You could also make the Inv_Sqrt dist asymmetrical, oh and the name(inv_sqrt) is pretty misleading: x/sqrt((x*x)+1) = sin(atan(x));

Inanyway - here's a bit of variation:

//Asym variation
a = ((0.0515*x+0.03899)*x)+x;
a2 = a*a;
out = x/sqrt(a2+0.6211);


Andrew
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
MadBrain
KVRist
- profile
- pm
PostPosted: Wed May 09, 2012 7:21 pm reply with quote
Afaik any memoryless nonlinearity will tend to give you quite different behavior on even vs odd harmonics. Symmetrical ones are of course the worst for this (no even harmonics!) but even asymetrical ones won't give you the same behavior on even vs odd.

As far as I can tell, only way to beat this is to go into complex domain - ie use something like a dome filter (filter that shifts all audible frequencies 1/4 waveform) and then use a 2d distortion function instead of a 1d one.
^ Joined: 01 Dec 2004  Member: #49995  
Richard_Synapse
KVRist
- profile
- pm
PostPosted: Wed May 09, 2012 9:43 pm reply with quote
Ichad.c wrote:
Yip, good ones are quite linear under normal operation, but guitar-amp builders purposely drive the output transformer into core-saturation and it's confirmed - the low frequencies get distorted more than the highs. Now I wonder if there is any memory effect? DC Leakage?


See the DAFX'10 paper about tube amp modelling, which covers some memory and DC effects. I don't recall though to what extent the output transformer was modelled.

Richard
----
Synapse Audio Software - www.synapse-audio.com
^ Joined: 19 Dec 2010  Member: #245936  
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Thu May 10, 2012 2:51 am reply with quote
MadBrain wrote:
Afaik any memoryless nonlinearity will tend to give you quite different behavior on even vs odd harmonics. Symmetrical ones are of course the worst for this (no even harmonics!) but even asymetrical ones won't give you the same behavior on even vs odd.

As far as I can tell, only way to beat this is to go into complex domain - ie use something like a dome filter (filter that shifts all audible frequencies 1/4 waveform) and then use a 2d distortion function instead of a 1d one.


For interist's sake - I tried your idea - with a simple IIR Hilbert Transformer, the results where interisting, but I don't think the solution would be applicable to the real analog sense. Cool idea though! Would be fun in something like a "additive" bass synth.

Regards
Andrew
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Thu May 10, 2012 3:12 am reply with quote
Quote:
See the DAFX'10 paper about tube amp modelling, which covers some memory and DC effects. I don't recall though to what extent the output transformer was modelled.

Richard


Thanks for the link - there are two papers actually - looks like the one uses a non-linear delayless feedback solver, unfortunately my math ain't that good Embarassed Still a good read though.

If I take the "tubes vs. transistors" article on face value - could I assume that only op-amps create true symmetrical distortion, and all other topologies create a mixture of odd and even harmonics?

Anybody here that has a waveshaping-transfer curve to share?

Would you guys here say that this approximation is accurate? (from musicdsp.org - under the comments of the Karlsen filter)

Final version, Stenseth, 17. february, 2006.

// Fast differential amplifier approximation

    double b_inr = b_in * b_filterdrive;
    if (b_inr < 0) {b_inr = -b_inr;}
    double b_inrns = b_inr;
    if (b_inr > 1) {b_inr = 1;}
    double b_dax = b_inr - ((b_inr * b_inr) * 0.5);
    b_dax = b_dax - b_inr;
    b_inr = b_inr + b_dax;

    b_inr = b_inr * 0.24;

    if (b_inr > 1) {b_inr = 1;}
    b_dax = b_inr - ((b_inr * 0.33333333) * (b_inr * b_inr));
    b_dax = b_dax - b_inr;
    b_inr = b_inr + b_dax;

    b_inr = b_inr / 0.24;

    double b_mul = b_inrns / b_inr; // beware of zero
    b_sbuf1 = ((b_sbuf1 - (b_sbuf1 * 0.4300)) + (b_mul * 0.4300));

    b_mul = b_sbuf1 + ((b_mul - b_sbuf1) * 0.6910);
    b_in = b_in / b_mul;


Regards
Andrew
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
rootbear
KVRer
- profile
- pm
PostPosted: Thu May 10, 2012 5:53 am reply with quote
could I assume that only op-amps create true symmetrical distortion

In my tests with op amps they distorted on one side more than the other when overdriven, but that may only apply to particular circuits and models.
^ Joined: 07 Feb 2011  Member: #249871  
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Thu May 10, 2012 6:38 am reply with quote
rootbear wrote:

In my tests with op amps they distorted on one side more than the other when overdriven, but that may only apply to particular circuits and models.


Could you give an example of what you tested that gave the op-amp that characteristic?

Andrew
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
Ichad.c
KVRist
- profile
- pm
- e-mail
- www
PostPosted: Thu May 10, 2012 6:59 am reply with quote
Is the static waveshaper equations at least helping anybody?

Andrew Question
^ Joined: 08 Feb 2012  Member: #274678  Location: South - Africa
rootbear
KVRer
- profile
- pm
PostPosted: Thu May 10, 2012 8:11 am reply with quote
Ichad.c wrote:
rootbear wrote:

In my tests with op amps they distorted on one side more than the other when overdriven, but that may only apply to particular circuits and models.


Could you give an example of what you tested that gave the op-amp that characteristic?

Andrew


Not really since it has been a while. With the ones I know of, when they swing to maximum and minimum voltage they aren't centered. I don't think you normally notice the difference in sound since the offset has to be extreme to sound like it is asymmetrical.
^ Joined: 07 Feb 2011  Member: #249871  
All times are GMT - 8 Hours

Printable version
Page 1 of 3
Goto page 1, 2, 3  Next
Display posts from previous:   
ReplyNew TopicPrevious TopicNext Topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Username: Password:  
KVR Developer Challenge 2012