fast integer based trigonometry for chdk and lua - General Discussion and Assistance - CHDK Forum

# fast integer based trigonometry for chdk and lua

• 17 Replies
• 10535 Views

#### rudi

• 129
• A590IS_101B, SX260HS_100B
##### fast integer based trigonometry for chdk and lua
« on: 16 / December / 2012, 12:02:01 »
Here is a package to gives chdk trigonometric functions without classic approximations. It used the integer based CORDIC algorithm.
This implementation need 17 steps for a precision of 5 decimal places. By reason of integer values there ist a maximal range from (double) -16383.99999 to 16383.99999. For better precision we have separate functions for angel in DEG and RAD, so it is not needed to transform between DEG and RAD. All functions see cordic_math.txt on attachment.
It will be useful eg. for rework on GPS part.

The second part is the integration to lua as library imath (integer mathematics). Specially is for lua, all values are mulipied by 1000 (imath.scale), so we have 3 virtual decimal places. We can use all functions also in hostlua. For more look at lua_imath.txt on attachment.

A very useful lua example with imath library is written by msl. Many thanks to msl!
The twilight_e.lua calculate some times for points of sun e.g. sunrise and sunset. In file geo_data.txt are some selected geographic data. Copy this file to A/CHDK/DATA/. Start script and use SET to select geo_data.txt.

Attachement: trunk2389_patch, MinGW example, lua example, hostlua executable, twilight_e.lua by msl

#### lapser

• 1093
##### Re: fast integer based trigonometry for chdk and lua
« Reply #1 on: 16 / December / 2012, 12:22:44 »
Wow, thanks! The sunset time part may be just what I need for my sunset time lapse obsession.

I'm working on adding lua integer functions for Apex96 conversion, using the 1000 multiplier for shutter time values. For shutter times less than 1 second (Tv<0), I have an option to return 1/shutter time, since shutter times can be less than 0.001 second.
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b

#### msl

• 1280
• A720 IS, SX220 HS 1.01a
##### Re: fast integer based trigonometry for chdk and lua
« Reply #2 on: 17 / December / 2012, 17:00:32 »
Cordic is a very clever algorithm. It works very efficiently. rudi's imath library complements CHDK Lua with all neccessary mathematical functions.

Code: [Select]
`Constants:pi*2pipi/2trigonometry:sincostanarcsinarccosarctanpoladditional:convert deg <-> radloglog2log10powsqrt`
An equation like this is now possible:
Code: [Select]
`timediff = 12*arccos((sin(h) - sin(lat)*sin(declination)) / (cos(lat)*cos(declination)))/Pi`
For a better demonstration of the efficiency I attach a screenshot of the example script. Remember, all calculations made ​​with integers.

msl

#### philmoz

• 3450
##### Re: fast integer based trigonometry for chdk and lua
« Reply #3 on: 18 / December / 2012, 05:12:11 »
Nice.

I've added this to the Lua module in my philmoz-reorg branch.

msl - not sure about where you live; but where I am sunrise is in the morning and sunset in the evening

Phil.
CHDK ports:
sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
g12 (1.00c, 1.00e, 1.00f & 1.00g)
sx130is (1.01d & 1.01f)
ixus310hs (1.00a & 1.01a)
sx40hs (1.00d, 1.00g & 1.00i)
g1x (1.00e, 1.00f & 1.00g)
g5x (1.00c, 1.01a, 1.01b)
g7x2 (1.01a, 1.01b, 1.10b)

#### msl

• 1280
• A720 IS, SX220 HS 1.01a
##### Re: fast integer based trigonometry for chdk and lua
« Reply #4 on: 18 / December / 2012, 06:18:16 »
... but where I am sunrise is in the morning and sunset in the evening

In old Germany also    A quick c/p translation is not always the best way.

Thanks for the note.

msl

#### rudi

• 129
• A590IS_101B, SX260HS_100B
##### Re: fast integer based trigonometry for chdk and lua
« Reply #5 on: 18 / December / 2012, 14:25:10 »
I've added this to the Lua module in my philmoz-reorg branch.
Fine, thank you.
Compilation of hostlua is missing cordic_math.h in imathlib.c. That was the reason for add cordic_math.h in makefile for hostlua. A second possibility is saving a copy of cordic_math.h in hostlua/include.

#### philmoz

• 3450
##### Re: fast integer based trigonometry for chdk and lua
« Reply #6 on: 19 / December / 2012, 02:29:11 »
I've added this to the Lua module in my philmoz-reorg branch.
Fine, thank you.
Compilation of hostlua is missing cordic_math.h in imathlib.c. That was the reason for add cordic_math.h in makefile for hostlua. A second possibility is saving a copy of cordic_math.h in hostlua/include.

Fixed now.

Phil.
CHDK ports:
sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
g12 (1.00c, 1.00e, 1.00f & 1.00g)
sx130is (1.01d & 1.01f)
ixus310hs (1.00a & 1.01a)
sx40hs (1.00d, 1.00g & 1.00i)
g1x (1.00e, 1.00f & 1.00g)
g5x (1.00c, 1.01a, 1.01b)
g7x2 (1.01a, 1.01b, 1.10b)

#### rudi

• 129
• A590IS_101B, SX260HS_100B
##### Re: fast integer based trigonometry for chdk and lua
« Reply #7 on: 27 / December / 2012, 07:05:55 »
Hi Phil,

I would like to rework gps mathematics, but I think cordic_math files (.c, .h, .o) are too much integrated in lua on philmoz-reorg branch. What can I do?
It is useful to make a gps module?

Thanks a lot, rudi

#### philmoz

• 3450
##### Re: fast integer based trigonometry for chdk and lua
« Reply #8 on: 27 / December / 2012, 20:39:16 »
Hi Phil,

I would like to rework gps mathematics, but I think cordic_math files (.c, .h, .o) are too much integrated in lua on philmoz-reorg branch. What can I do?
It is useful to make a gps module?

Thanks a lot, rudi

Hi Rudi,

You should be able to add cordic_math.o to OBJS in lib/core/makefile, then use the functions in the GPS code.
The linker will exclude cordic_math.o from the core build except for GPS enabled cameras that reference it.

This will mean non-GPS cams will have cordic_math only in Lua; but GPS cams will have an additional copy in the core CHDK code. This seems a reasonable compromise to me.

The alternative would be move cordic_math to the core code for all cams and change Lua to use the core version.

Phil.
CHDK ports:
sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
g12 (1.00c, 1.00e, 1.00f & 1.00g)
sx130is (1.01d & 1.01f)
ixus310hs (1.00a & 1.01a)
sx40hs (1.00d, 1.00g & 1.00i)
g1x (1.00e, 1.00f & 1.00g)
g5x (1.00c, 1.01a, 1.01b)
g7x2 (1.01a, 1.01b, 1.10b)

#### waterwingz

• 12138
##### Re: fast integer based trigonometry for chdk and lua
« Reply #9 on: 27 / March / 2013, 07:14:28 »
Here is a package to gives chdk trigonometric functions without classic approximations. It used the integer based CORDIC algorithm.

@rudi :  I've been using your integer math functions recently but noticed one that I think is missing?  Would it be possible to add a imath.floor(a) function ?   I know I can implement it in script code but it would be a nice one to have built-in.

TIA.
« Last Edit: 27 / March / 2013, 07:40:06 by waterwingz »
Ported :   A1200    SD940   G10    Powershot N    G16