我在
javascript中进行一些数字信号处理计算,我发现计算双曲正切(tanh)有点太贵了.这是我现在如何近似于tanh:
function tanh (arg) { // sinh(number)/cosh(number) return (Math.exp(arg) - Math.exp(-arg)) / (Math.exp(arg) + Math.exp(-arg)); }
任何人都知道更快的计算方法吗?
解决方法
从
here.
function rational_tanh(x) { if( x < -3 ) return -1; else if( x > 3 ) return 1; else return x * ( 27 + x * x ) / ( 27 + 9 * x * x ); }
This is a rational function to
approximate a tanh-like soft clipper.
It is based on the pade-approximation
of the tanh function with tweaked
coefficients.The function is in the range x=-3..3
and outputs the range y=-1..1. Beyond
this range the output must be clamped
to -1..1.The first to derivatives of the
function vanish at -3 and 3,so the
transition to the hard clipped region
is C2-continuous.
Padé近似值比泰勒扩展值更好.夹紧也可能是一个问题(取决于您的范围).