Fast sin/cos/tan math lib approximation recommendation

Discussion to talk about software related topics only.
Post Reply
zoanie
Posts: 13
Joined: Mon Apr 11, 2011 10:42 pm
Location: Thousand Oaks, CA

Fast sin/cos/tan math lib approximation recommendation

Post 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,
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

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

Post 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...?
zoanie
Posts: 13
Joined: Mon Apr 11, 2011 10:42 pm
Location: Thousand Oaks, CA

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

Post 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
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

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

Post 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...
Attachments
SinLookUp.zip
(492.16 KiB) Downloaded 428 times
zoanie
Posts: 13
Joined: Mon Apr 11, 2011 10:42 pm
Location: Thousand Oaks, CA

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

Post by zoanie »

Hi Paul,

Very nice! Exactly the kind of support I needed.

Cheers,

Carlos
joepasquariello
Posts: 85
Joined: Mon Apr 28, 2008 7:32 am

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

Post 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
Post Reply