How about this? It can be 3x as fast!
sbright33:
How about this? It can be 3x as fast!
ChipKIT Uno32: First Impressions And Benchmarks | Hackaday
That's pretty cool!
What is this agricultural peasant woman's organisation that everybody keeps refering to ?
If your compiler doesn't optimize (PI/2), you should throw it away and get a new compiler. This is not a subtle distinction between space and time optimization, this is simple "constant folding." (however, from the C specification, the compiler is not required to evaluate this at compile time.)
[CORDIC has] 430us for a 27bit resolution version...
The existing avr-libc trig functions are pretty heavily optimized (although: some for size, rather than runtime.)
Most of the trig functions should be taking less than 200us on a 16MHz ATmega328p:
http://www.nongnu.org/avr-libc/user-manual/benchmarks.html
Don't forget that a floating point divide on AVR is pretty close in speed to a 32bit integer divide (slight faster, I think. Only 24 bits get divided.) (Multiply is somewhat more assisted by the HW multiplier.)
(which means, BTW, that one thing you can look for is replacing division by multiplications. I don't know if the compiler will do that for constant expressions (you should check!) (Though most of that will already be done inside the trig functions.))
nithesh26:
After uploading the code to the board, when we see the result through serial monitor it doesn't show anything. We should connect the output to any external device to see the output of Cordic ?
Perhaps you didn't see this line:
Serial.begin (57600) ;
cos(x) = sin(x+90); so angle = 90 degree
robtillaart:
cos(x) = sin(x+90); so angle = 90 degree
I did not get you. What actually are you trying to say.?
MarkT:
robtillaart:
Hasn't anyone mentioned CORDIC algorithms yet? CORDIC - Wikipedia
CORDIC no not mentioned before, might be interesting to see the timing of those on Arduino....
430us for a 27bit resolution version...
long cordic_lookup [] =
{
0x20000000L,
0x12E4051EL,
0x09FB385BL,
0x051111D4L,
0x028B0D43L,
0x0145D7E1L,
0x00A2F61EL,
0x00517C55L,
0x0028BE53L,
0x00145F2FL,
0x000A2F98L,
0x000517CCL,
0x00028BE6L,
0x000145F3L,
0x0000A2FAL,
0x0000517DL,
0x000028BEL,
0x0000145FL,
0x00000A30L,
0x00000518L,
0x0000028CL,
0x00000146L,
0x000000A3L,
0x00000051L,
0x00000029L,
0x00000014L,
0x0000000AL,
0x00000005L
};
#define ITERS 10000
void setup ()
{
Serial.begin (57600) ;
long elapsed = micros () ;
for (long i = 0 ; i < ITERS ; i++)
test_cordic (i << 16, false) ;
elapsed = micros () - elapsed ;
Serial.print ("time taken for ") ; Serial.print (ITERS) ;
Serial.print (" iterations = ") ; Serial.print (elapsed) ; Serial.println ("us") ;
Serial.print (elapsed / ITERS) ; Serial.println (" us/iter") ;
test_cordic (0x15555555L, true) ;
test_cordic (0x95555555L, true) ;
}
void test_cordic (long aa, boolean printres)
{
long xx = 607252935L ;
long yy = 0L ;
if ((aa ^ (aa<<1)) < 0L)
{
aa += 0x80000000L ;
xx = -xx ;
yy = -yy ;
}
for (int i = 0 ; i <= 27 ; i++)
{
long da = cordic_lookup [i] ;
long dx = yy >> i ;
long dy = -xx >> i ;
if (aa < 0L)
{
aa += da ;
xx += dx ;
yy += dy ;
}
else
{
aa -= da ;
xx -= dx ;
yy -= dy ;
}
}
if (!printres)
return ;
Serial.print ("end angle=") ; Serial.print (aa) ;
Serial.print (" end x = 0.") ; Serial.print (xx) ;
Serial.print (" end y = 0.") ; Serial.println (yy) ;
}
void loop ()
{
}
How can I get the value of sin(anyangle) & cos (any angle) through the code provided.?
If I execute the code I'm getting values of x and y, What do those values refer to?
You can use one algorithm to evaluate both the sine and the cosine function, although that is not the most efficient way to do it.
nithesh26:
So, Initially it is rotated by a certain angle. May I know where the angle is initialized and what about arctan2^-i ?
As I said: CORDIC - Wikipedia
Suppose if I want to calculate sin(40). Then how can I do that by using the code.?
I've tried many ways but couldn't get it. Can any one help me.?
You calculate sin(40) by using sin(40.0)
but I would guess that you don't really mean that. I will guess that your 40 is really 40 degrees, but sin(...) works in radians. Thus, you probably want to calculate
sin(40.0*3.14159/180.0)
[the multiplication by 3.14159/180.0 converts from degrees to radians]
If it were up to me, I would write a function to do this conversion, or perhaps write a function that calculates sin(...) based on an input in degrees. However, I wanted to keep this post simple.
See http://arduino.cc/en/Reference/Sin for more information.
vaj4088:
You calculate sin(40) by using sin(40.0)but I would guess that you don't really mean that. I will guess that your 40 is really 40 degrees, but sin(...) works in radians. Thus, you probably want to calculate
sin(40.0*3.14159/180.0)[the multiplication by 3.14159/180.0 converts from degrees to radians]
If it were up to me, I would write a function to do this conversion, or perhaps write a function that calculates sin(...) based on an input in degrees. However, I wanted to keep this post simple.
See http://arduino.cc/en/Reference/Sin for more information.
I don't want to calculate sin(40.0) directly. I wanted to know how it is implemented through cordic algorithm provided above.
MarkT:
nithesh26:
So, Initially it is rotated by a certain angle. May I know where the angle is initialized and what about arctan2^-i ?As I said: CORDIC - Wikipedia
Suppose if I want to calculate sin(40). Then how can I do that by using the code.?
I've tried many ways but couldn't get it. Can any one help me.?
I wanted to know how it is implemented through cordic algorithm provided above.
Is Wikipedia banned in your country ?
michinyon:
I wanted to know how it is implemented through cordic algorithm provided above.
Is Wikipedia banned in your country ?
No but I'm not able to understand how to calculate any particular angle through the cordic algorithm code.
Do you really expect someone to sit here and spend an hour typing out an explanation, just for you? And without an easy facility to write detailed equations or diagrams here ?
There are half a dozen good descriptions on the internet, including wikipedia, with useful links to more information.
The prerequisite to understanding CORDIC is to understand how multiplication in the complex
plane represents rotation and scaling.
MarkT:
The prerequisite to understanding CORDIC is to understand how multiplication in the complex
plane represents rotation and scaling.
I uploaded the code and when I look over serial monitor it is displaying end angle of -2. So what should be done in order to get the different angle ?