Page 1 of 1

Fast sin/cos/tan math lib approximation recommendation

Posted: Tue Mar 22, 2016 11:35 am
by zoanie
Hello,

I have an application on the MOD5441X that is using quite a bit of cpu time calling the math library functions sin(), cos() & tan(). I would like to find out if anyone has found a faster math library. I am willing to accept a drop in precision for an increase in speed. I need better than a 50% reduction in cpu time. I understand there are some trade-offs between using a lookup table (memory access speed is an issue) versus an approximation algorithm. My application has plenty of memory available, at least 2Mb could be made for a table. I can also swap the use of double precision variable for a 32-bit float result.

Thank you for reading and your responses,

Re: Fast sin/cos/tan math lib approximation recommendation

Posted: Tue Mar 22, 2016 3:04 pm
by pbreed
I've made a lookup table with doubles in the past and it is much faster....
If you can handle the boot delay you can even populate it at startup time...

Exactly what functins do you need?
Any idea on the level of precision? Absolute or relative tolerance?

Is this scaled from some sensor so the input resolution might be known?

You need Sin and Cos, Tan do you need any inverse functions?
IE Asin, ACos, Atan Atan2 etc...?

Re: Fast sin/cos/tan math lib approximation recommendation

Posted: Tue Mar 22, 2016 5:00 pm
by zoanie
Hi Paul,

Thanks for your response. I primarily need sin() & cos(), although I also have a few instances of the atan2() function. I need a precision to 0.001 deg. I can use symmetry in the solution to reduce the lookup table size by a factor of 4. I should only need a table for [0 - pi/2], with the other quadrants mapping to the first. I am thinking a lookup table will be faster than an approximation algorithm primarily because the MOD5441X cpu clock speed is not significantly faster than the DDR2 memory access speed (I could be wrong here, I'm not certain of the MOD5441X memory speed).

Best regards,

zoanie

Re: Fast sin/cos/tan math lib approximation recommendation

Posted: Wed Mar 23, 2016 10:18 am
by pbreed
The attached file should do what you want accurate to better ~0.001373291 deg or 2.39684E-05 radians
and biggest error from 0 to 360 degrees at 0.0001 deg was :0.00002396520127838364
As you would expect the biggest error was at points where first derivative was greatest, ie most slope..

Used an old trick to wrap the 0 to 2 pi range into range where you can use simple AND to war to 2pi and to determine quadrant...

Re: Fast sin/cos/tan math lib approximation recommendation

Posted: Wed Mar 23, 2016 1:00 pm
by zoanie
Hi Paul,

Very nice! Exactly the kind of support I needed.

Cheers,

Carlos

Re: Fast sin/cos/tan math lib approximation recommendation

Posted: Wed Apr 06, 2016 9:08 pm
by joepasquariello
In case anyone else is interested, here is the switch statement from Paul's code with comments regarding the trig identities used for angles in quadrants 1-4.

switch(index & 0x30000)
{
case 0x00000: return SinQuadTable[index]; // 0- 90 sin(x)
case 0x10000: return SinQuadTable[0x1FFFF-index]; // 90-180 sin(pi-x)
case 0x20000: return -SinQuadTable[index & 0xFFFF]; // 180-270 -sin(x-pi)
case 0x30000: return -SinQuadTable[0x1FFFF-(index &0x1FFFF)]; // 270-360 -sin(2*pi-x)
}

Joe