MATH arctan calculation

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
Hi,

The XMC_MATH_CORDIC_ArcTanNB function in xmc_math.c assumes XMC_MATH_Q8_15_t types for arguments. It means all operations in circular & vectoring mode
operations assumes XMC_MATH_Q8_15_t typed arguments?

Is this code correct to calculate arctan(1/1) ?


#include

#include "XMC1300.h"

int main(void)
{
int a;

XMC_MATH_Q8_15_t x = 1 << 16;
XMC_MATH_Q8_15_t y = 1 << 16;

XMC_MATH_CORDIC_ArcTanNB(x, y);

while(XMC_MATH_CORDIC_IsBusy()); // wait for calculation to end
a = 1; // set breakpoint here to check CORRZ

while(1);
}

0 Likes
5 Replies
User7282
Level 4
Level 4
Hello,

I think it should be 1 << 15 right? Because you have 15 fractional bits.
0 Likes
Not applicable
Ok, the result seems good this way, I have got 0x200000 (24bit) which is pi/4.
How to compute atan(-1,1) ? So what is -1 in Q8_15 ?
0 Likes
User7282
Level 4
Level 4
Following the same approach:

-1*2^15 = -32768 (0xffff8000). As the format only has 24 bits, just remove the 8 MSBs.
0 Likes
Not applicable
Can I use this macro? I'm not sure because this macro contains floating point constants.


#define XMC_MATH_Q8_15(x) ((XMC_MATH_Q8_15_t)(((x) >= 0) ? ((x) * (1 << 15) + 0.5) : ((x) * (1 << 15) - 0.5)))


I have found a similar macro (XMC_MATH_Q0_23) in xmc_math.h

The argument can be integer only?
0 Likes
User7282
Level 4
Level 4
Yes, I think that you can. This is basically the procedure to convert to fixed point:
- Multiply the floating point number by 2^(numberOffractionalBits)
- Round to the nearest integer

Considering this the argument can be float also.
Then it will be typecasted to int32 (XMC_MATH_Q8_15_t)
0 Likes