Hi, i want to implement the tanh function in PD Vanilla. I use the formula y(x) = (1 - b*e^-(a*x + lnb))/(1/b + e^-(a*x + lnb)) where (a) is a steepness parameter and b controls the amplitude range. You can see my implementation which seems to work, but when I incorporate it to larger patches, I get a core dumped...
Any clue why is this happening or any suggestions for a better implementation?
Thanks, Kosmas
hi, epxr~ (which is vanilla) has a tanh function. I updated the expr help file to include all its functions for the 0.48-0 release, in case people missed it.
so an easy vanilla tanh~ implementation can just be: [expr~ tanh($v1)]
cheers
2017-08-15 17:41 GMT-03:00 Kosmas Giannoutakis < giannoutakiskosmas@hotmail.com>:
Hi, i want to implement the tanh function in PD Vanilla. I use the formula y(x) = (1 - b*e^-(a*x + lnb))/(1/b + e^-(a*x + lnb)) where (a) is a steepness parameter and b controls the amplitude range. You can see my implementation which seems to work, but when I incorporate it to larger patches, I get a core dumped...
Any clue why is this happening or any suggestions for a better implementation?
Thanks, Kosmas
Pd-list@lists.iem.at mailing list UNSUBSCRIBE and account-management -> https://lists.puredata.info/ listinfo/pd-list
Hi, Kosmas.
I'm using (1 - e^-2x)/(1 + e^-2x) and it seems to work fine though you wouldn't have the parameters you mentioned.
Cheers, D
On 15 August 2017 at 13:41, Kosmas Giannoutakis < giannoutakiskosmas@hotmail.com> wrote:
Hi, i want to implement the tanh function in PD Vanilla. I use the formula y(x) = (1 - b*e^-(a*x + lnb))/(1/b + e^-(a*x + lnb)) where (a) is a steepness parameter and b controls the amplitude range. You can see my implementation which seems to work, but when I incorporate it to larger patches, I get a core dumped...
Any clue why is this happening or any suggestions for a better implementation?
Thanks, Kosmas
Pd-list@lists.iem.at mailing list UNSUBSCRIBE and account-management -> https://lists.puredata.info/ listinfo/pd-list
I've always used:
tanh(x) ~= x*(27+x*x) / (27+9*x*x)
works well and is pretty CPU cheap
On 15/08/17 23:46, Matt Davey wrote to pd-list@lists.iem.at:
I've always used:
tanh(x) ~= x*(27+x*x) / (27+9*x*x)
works well and is pretty CPU cheap
Nice.
That seems accurate to about 5 or 6 bits in my tests (over the range -4 to 4). A slightly more accurate (about 7 or 8 bits) variant is:
x * (27.2837670294674 + 1.22070209109864 * x * x)
/ (27.2837670294674 + 9.86205301460732 * x * x)
A higher degree version is accurate to about 16 or 17 bits:
x * (44220389.1067293 + 4672204.74255456 * x * x
+ 34604.8550273853 * x * x * x * x)
/ (44220389.1067293 + 19409074.7654446 * x * x
+ 612890.122925575 * x * x * x * x)
(Though I realize now, later, that there is some redundancy and they could be normalized to have a leading "1 +" instead of these large values...)
I found these using gnuplot's fit functionality:
F(x) = x * (a + b * x * x) / (a + c * x * x)
fit F(x) 'tanh.dat' via a,b,c
and similarly for the higher degree version. 'tanh.dat' contains 1024 equally spaced samples of x,tanh(x) in [-4,4], calculated in double precision with a small C program, similar to the input generation in the help patches.
I evaluated the accuracy visually in gnuplot:
bits(x,y) = log(abs(y - tanh(x))+2**(-53)) / log(2.0)
plot [-4:4] bits(x,F(x))
I don't know if single-precision rounding will affect the results much. I put the full precision coefficients into the patches using a text editor, so double precision Pd can benefit just in case.
It's probably also best to use [clip~ -4 4] before these functions, to avoid possible Inf or NaN explosions, or maybe [clip~ -1 1] afterwards would be enough to be safe?
I haven't benchmarked yet.
Consider the patches placed into the Public Domain. Message-rate versions are left as an exercise.
On 01/09/17 06:42, Claude Heiland-Allen wrote:
On 15/08/17 23:46, Matt Davey wrote to pd-list@lists.iem.at:
I've always used:
tanh(x) ~= x*(27+x*x) / (27+9*x*x)
works well and is pretty CPU cheap
Nice.
I also see it's smooth if you clip the input to -3..3: http://www.musicdsp.org/showone.php?id=238 found via a comment in https://github.com/enzienaudio/heavylib/blob/master/hv.tanh.pd