trigonometry and XS-L1
-
- Member++
- Posts: 26
- Joined: Thu Nov 04, 2010 1:18 pm
trigonometry and XS-L1
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.
-
- XCore Addict
- Posts: 228
- Joined: Thu Jan 27, 2011 3:30 pm
- Location: Argenteuil - France
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).
-
- Respected Member
- Posts: 259
- Joined: Thu Sep 16, 2010 9:15 am
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).
C supports FP math. (but XC does not as octal said).
-
- XCore Expert
- Posts: 546
- Joined: Thu Dec 10, 2009 10:41 pm
- Location: St. Leonards-on-Sea, E. Sussex, UK.
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.
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.
-
- Member++
- Posts: 26
- Joined: Thu Nov 04, 2010 1:18 pm
what i need is high speed processing and a quite accuracy.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).
-
- XCore Expert
- Posts: 956
- Joined: Fri Dec 11, 2009 3:53 am
- Location: Sweden, Eskilstuna
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.
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.
-
- Member++
- Posts: 26
- Joined: Thu Nov 04, 2010 1:18 pm
thank you for your help lilltroll.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.
Im going to do what you said but also use the Lagrange polynomial and see which one is better.
-
- XCore Expert
- Posts: 956
- Joined: Fri Dec 11, 2009 3:53 am
- Location: Sweden, Eskilstuna
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.
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.
-
- Member++
- Posts: 26
- Joined: Thu Nov 04, 2010 1:18 pm
Im working on my trig. module. I will post a project if it start working welllilltroll 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.
-
- XCore Addict
- Posts: 228
- Joined: Thu Jan 27, 2011 3:30 pm
- Location: Argenteuil - France
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 :)