Table Lookup for tanh() versus other solutions

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

mystran wrote: Fri Nov 19, 2021 12:17 pm Also I feel someone (Vadim?) posted some derivative matched approximation a while ago too.. but I don't have a link 'cos I just use either the super-cheap approximations or 2DaT's approx depending on how much I happen to care at any given moment.
viewtopic.php?f=33&t=519852

Post

2DaT wrote: Fri Nov 19, 2021 8:34 am
andy-cytomic wrote: Fri Nov 19, 2021 6:30 am If you don't want to use a fast reciprocal sqrt with 1 Newton Raphson step then a straight out rational approximation can also be good, but then you have to tidy up the endpoints by matching the derivative(s) at the endpoints to keep things smooth. If anyone is interested in some regular rational approximations please let me know.
Straightforward relative err. minmax is very viable strategy for tanh approximations. Check out this version:
viewtopic.php?p=7503081#p7503081

Derivatives are not as big of a concern for precise approximations - if function is approximated within few ulps, derivatives are going to be approximated decently aswell.

On modern processors single precision division is way better than rcpss+NR. In some cases it can be as cheap as usual arithmetics as long as you mix divs with other operations.
I was just pointing out the decent coefficients for the recip sqrt method of higher order, and that the relative error is better to minimise, I use a regular rational approximation with matched derivatives for my own stuff. Also minimax isn't good unless you have a high enough order of polynomial to get a high accuracy. For lower order polynomials a minimax solution will not have the correct derivative of 1 at x = 0, and there can be discontinuities at the endpoints as well.
The Glue, The Drop - www.cytomic.com

Post

Another resurrection:

I'm attempting to use antiderivative antialiasing (as described in https://dafx16.vutbr.cz/dafxpapers/20-D ... _41-PN.pdf and https://ccrma.stanford.edu/~jatin/Notebooks/adaa.html among others) to a less expensive approximation of tanh. Before going on a wild goose chase in hopes of finding a good approximation suitable for this purpose, has anyone here done this already?

Post

The method works with anything you can integrate.. the challenge in general is with finding something you can integrate with efficiency.

Post

x/(1+|x|) ? from art of va filter design not sure if its correct in this case but heard it could be substituted to drive filters.

edit: nvm just graphed it see what the issue is

Post

Marvinh wrote: Thu May 23, 2024 2:11 am x/(1+|x|) ? from art of va filter design not sure if its correct in this case but heard it could be substituted to drive filters.
If you just want "something" then x/sqrt(1+x^2) might be a better choice.

Post

1-(2/(1+exp(2x))) this one is really close

edit: a little too close maybe find fast exp instead?

Post

Thanks! Perhaps I should clarify: in particular I’m looking for not only a relatively close approximation on tanh but also one that is relatively fast, and whose antiderivatives are relatively easy to calculate and are also relatively fast.

Post

Got you new to this after a long break great paper btw

Post

Dogue wrote: Thu May 23, 2024 12:47 am Another resurrection:

I'm attempting to use antiderivative antialiasing (as described in https://dafx16.vutbr.cz/dafxpapers/20-D ... _41-PN.pdf and https://ccrma.stanford.edu/~jatin/Notebooks/adaa.html among others) to a less expensive approximation of tanh. Before going on a wild goose chase in hopes of finding a good approximation suitable for this purpose, has anyone here done this already?
Maybe not a big deal but, are couple of the plots showed in ~jatin's notebook (In[3], In[11]) done using wrong fs as it is set to 1920000 Hz (1.92MHz) which is not common sample-rate in audio
applications? With 192kHz plots shows closer to ADAA results.

tanhDistortion.png
FS = 192kHz

tanhDistortion_w_ADAA.png
You do not have the required permissions to view the files attached to this post.
Last edited by juha_p on Thu May 23, 2024 6:41 pm, edited 2 times in total.

Post

juha_p wrote: Thu May 23, 2024 6:18 pm Maybe not a big deal but, are couple of the plots showed in ~jatin's notebook (In[3], In[11]) done using wrong fs as it is set to 1920000 Hz (1.92MHz) which is not common sample-rate in audio applications? With 192kHz plots shows closer to ADAA results.
Yes, those are done intentionally to show what the respective techniques look like without aliasing.

Post

Dogue wrote: Thu May 23, 2024 12:47 am Another resurrection:

I'm attempting to use antiderivative antialiasing (as described in https://dafx16.vutbr.cz/dafxpapers/20-D ... _41-PN.pdf and https://ccrma.stanford.edu/~jatin/Notebooks/adaa.html among others) to a less expensive approximation of tanh. Before going on a wild goose chase in hopes of finding a good approximation suitable for this purpose, has anyone here done this already?
This should be of some use.

viewtopic.php?p=8744169#p8744169

It's pretty accurate. I didn't test very thoroughly, but it seems to be at the limit of what's possible to do with single precision calculations, this exact approach at least.

One thing to notice is that it still needs a tanh(x) if the antiderivative is ill-conditioned. That means if we process SIMD, in unlucky cases we need to compute both tanh and tanh antiderivative at the same time, which kinda defeats the purpose, as the method is barely more effective than just doing the 2x oversampling.

Maybe some magical method exists for stitching these ill-conditioned values, because calculation of antiderivatives (even of higher order) is not THAT expensive, it's a straightforward rational function with some limiting. Which is computed using rational minmax (relative weighted) algorithm pretty easily.

Post

juha_p wrote: Thu May 23, 2024 6:18 pm Maybe not a big deal but, are couple of the plots showed in ~jatin's notebook (In[3], In[11]) done using wrong fs as it is set to 1920000 Hz (1.92MHz) which is not common sample-rate in audio
applications? With 192kHz plots shows closer to ADAA results.
It doesn't matter one bit if something is a "common samplerate in audio" if the goal is just to bump the sampling rate "high enough" that aliasing is no longer an issue. Besides 1.92MHz is still an integer multiple of 48kHz so you don't even need to jump through any fancy hoops to synchronize buffers or anything like that.

Post

2DaT wrote: Thu May 23, 2024 6:53 pm One thing to notice is that it still needs a tanh(x) if the antiderivative is ill-conditioned.
No you don't.

viewtopic.php?p=8731260#p8731260

TL;DR: expand your integration range by "epsilon" in both directions and there are no more special cases, no more ill-conditioning (zero range becomes a finite difference approximation of a derivative) and it just works branchless.

Post

mystran wrote: Thu May 23, 2024 7:13 pm
2DaT wrote: Thu May 23, 2024 6:53 pm One thing to notice is that it still needs a tanh(x) if the antiderivative is ill-conditioned.
No you don't.

viewtopic.php?p=8731260#p8731260

TL;DR: expand your integration range by "epsilon" in both directions and there are no more special cases, no more ill-conditioning (zero range becomes a finite difference approximation of a derivative) and it just works branchless.
Then we can't reuse the antiderivative from the last sample and still need to do double the work? I don't see how 0/0 ill-conditioning is resolved in that case.

One spark of hope would be that in the case of 0/0 ill-conditioning we don't actually need to compute antiderivative compensation and can somehow reuse the value from the previous sample (skipping the tanh part), perhaps with some minor correction.

That should require some extra state though. Otherwise, on very slow ramps, where the difference between the samples is close to the epsilon, we will just integrate the numerical noise, which is wrong. But we don't need antialiasing at all in such a case.

Post Reply

Return to “DSP and Plugin Development”