viewtopic.php?f=33&t=519852
Table Lookup for tanh() versus other solutions
- KVRAF
- 2646 posts since 3 Dec, 2008
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.2DaT wrote: ↑Fri Nov 19, 2021 8:34 amStraightforward relative err. minmax is very viable strategy for tanh approximations. Check out this version: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.
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.
The Glue, The Drop - www.cytomic.com
-
- KVRer
- 17 posts since 18 Jul, 2008
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?
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?
- KVRAF
- 7994 posts since 12 Feb, 2006 from Helsinki, Finland
The method works with anything you can integrate.. the challenge in general is with finding something you can integrate with efficiency.
-
- KVRist
- 83 posts since 6 Sep, 2015
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
edit: nvm just graphed it see what the issue is
- KVRAF
- 7994 posts since 12 Feb, 2006 from Helsinki, Finland
If you just want "something" then x/sqrt(1+x^2) might be a better choice.
-
- KVRer
- 17 posts since 18 Jul, 2008
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.
-
- KVRian
- 841 posts since 21 Feb, 2006 from FI
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 audioDogue 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?
applications? With 192kHz plots shows closer to ADAA results.
FS = 192kHz
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.
-
- KVRer
- 17 posts since 18 Jul, 2008
Yes, those are done intentionally to show what the respective techniques look like without aliasing.
-
- KVRian
- 634 posts since 21 Jun, 2013
This should be of some use.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?
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.
- KVRAF
- 7994 posts since 12 Feb, 2006 from Helsinki, Finland
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.
- KVRAF
- 7994 posts since 12 Feb, 2006 from Helsinki, Finland
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.
-
- KVRian
- 634 posts since 21 Jun, 2013
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.mystran wrote: ↑Thu May 23, 2024 7:13 pmNo 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.
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.