trigonometry and XS-L1

Technical questions regarding the XTC tools and programming with XMOS.
daleonpz
Member++
Posts: 26
Joined: Thu Nov 04, 2010 1:18 pm

trigonometry and XS-L1

Post by daleonpz »

i want to implement trigonometric functions as sine and cosine, but i havent found any kind of information about this. So then im asking for your help , and looking forward to your answer. Particularly, i want to implemente the inverse kinematic of stewart platform.


User avatar
octal
XCore Addict
Posts: 228
Joined: Thu Jan 27, 2011 3:30 pm
Location: Argenteuil - France

Post by octal »

I think that the main pb you'll be facing is the fact that XC does not handle floating point arithmetic right now. You need to do all your calculations with integers (maybe you can symply use precomputed sin tables).
ale500
Respected Member
Posts: 259
Joined: Thu Sep 16, 2010 9:15 am

Post by ale500 »

The math library contains implementations of both functions for float and double types. While they are a bit slow the results are ok. Why do you need your own implementation if I may ask ? Do you need faster/more accurate/smaller implementations ?

C supports FP math. (but XC does not as octal said).
User avatar
leon_heller
XCore Expert
Posts: 546
Joined: Thu Dec 10, 2009 10:41 pm
Location: St. Leonards-on-Sea, E. Sussex, UK.

Post by leon_heller »

This formula was used by Analog Devices for their 16-bit fixed-point DSPs:

sin(x) = 3.140625x + 0.02026367x^2 – 5.325196x^3 + 0.5446778x^4 + 1.800293x^5

for angles in the first quadrant.

The result can be calculated quite fast - 25 cycles on an ADSP-21xx.
daleonpz
Member++
Posts: 26
Joined: Thu Nov 04, 2010 1:18 pm

Post by daleonpz »

ale500 wrote:The math library contains implementations of both functions for float and double types. While they are a bit slow the results are ok. Why do you need your own implementation if I may ask ? Do you need faster/more accurate/smaller implementations ?

C supports FP math. (but XC does not as octal said).
what i need is high speed processing and a quite accuracy.
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Post by lilltroll »

There is many example of lookuptables and interpolation out there, e.g. the same way the "math-processor" in a x86 calculates sin (!?)
Can you descriebe the precision you need, and the amount of memory that you can allow the method to use ? Ex.
I would like to do an (Audio) sine-wave tonegenerator and the total signal error must be -96 dBFS (dB rel. Full Scale)


http://en.wikipedia.org/wiki/Lookup_tab ... ting_sines

In your case you will scale the angle so pi equals (2^n) , where n depends on the resolution that you need.


If the angle can become grater than pi, you may use
angle = angle%(2^(n+1)) e.g. angle = angle%2pi

You can choose between speed and memory-usage
You can also do a mix - e.g. calculate the first part of the polynomial, like sin(x) = ax+ bx^2 and only store the error/deviation in the lookuptable, which reduced the number of bits that needs to be stored for the error.
Since branching has no non-determenistic time-penalty on XMOS you can also use different polynomials, e.g. different taylor series for different angles. Like 0<x<90 degrees

If x< 30 degress

sin(x)=x + lookuptable

elseif x<60 degrees
sin(x)= 1 - (pi/2 - x)^2/2 + lookuptable //mac can calculate x*y+z
else

sin(x)=pi-x + lookuptable

If you use interpolation in the lookuptable you should probably have 3 different vectors with n value of overlap, where n is the interpolating order, typically n=1, to avoid the interpolation to go wrong at the boudaries.
Probably not the most confused programmer anymore on the XCORE forum.
daleonpz
Member++
Posts: 26
Joined: Thu Nov 04, 2010 1:18 pm

Post by daleonpz »

lilltroll wrote:There is many example of lookuptables and interpolation out there, e.g. the same way the "math-processor" in a x86 calculates sin (!?)
Can you descriebe the precision you need, and the amount of memory that you can allow the method to use ? Ex.
I would like to do an (Audio) sine-wave tonegenerator and the total signal error must be -96 dBFS (dB rel. Full Scale)


http://en.wikipedia.org/wiki/Lookup_tab ... ting_sines

In your case you will scale the angle so pi equals (2^n) , where n depends on the resolution that you need.


If the angle can become grater than pi, you may use
angle = angle%(2^(n+1)) e.g. angle = angle%2pi

You can choose between speed and memory-usage
You can also do a mix - e.g. calculate the first part of the polynomial, like sin(x) = ax+ bx^2 and only store the error/deviation in the lookuptable, which reduced the number of bits that needs to be stored for the error.
Since branching has no non-determenistic time-penalty on XMOS you can also use different polynomials, e.g. different taylor series for different angles. Like 0<x<90 degrees

If x< 30 degress

sin(x)=x + lookuptable

elseif x<60 degrees
sin(x)= 1 - (pi/2 - x)^2/2 + lookuptable //mac can calculate x*y+z
else

sin(x)=pi-x + lookuptable

If you use interpolation in the lookuptable you should probably have 3 different vectors with n value of overlap, where n is the interpolating order, typically n=1, to avoid the interpolation to go wrong at the boudaries.
thank you for your help lilltroll.

Im going to do what you said but also use the Lagrange polynomial and see which one is better.
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Post by lilltroll »

Please post a project around the trig. module so others can use it later.
I might improve it myself if I find it possible to do so.
The sin and cos has been on my low-prio todo list for a long time.
Probably not the most confused programmer anymore on the XCORE forum.
daleonpz
Member++
Posts: 26
Joined: Thu Nov 04, 2010 1:18 pm

Post by daleonpz »

lilltroll wrote:Please post a project around the trig. module so others can use it later.
I might improve it myself if I find it possible to do so.
The sin and cos has been on my low-prio todo list for a long time.
Im working on my trig. module. I will post a project if it start working well
User avatar
octal
XCore Addict
Posts: 228
Joined: Thu Jan 27, 2011 3:30 pm
Location: Argenteuil - France

Post by octal »

You can still post it even if it does not work ok, so that other people can contribute and correct and test it if needed :)