Math Cordic Square root API

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

cross mob
Not applicable
Hello All,

I want to use square root api provided with math library :

int16_t XMC_MATH_CORDIC_Q15_Sqrt(int16_t x) and
int32_t XMC_MATH_CORDIC_Q31_Sqrt(int32_t x)

But I am not getting the desired results.

Is there any changes has to be made for getting the result, like some sort of number format?

Suppose I want to find the square root of 144 and 34567. what steps I have to follow to get the desired result?

Argument x has to be converted in which data format and result will be in which format?

Please guide me!

Regards,
Tinchu
0 Likes
15 Replies
User12775
Level 5
Level 5
First solution authored First like received
The function names say all.

Q15 and Q31 format.
0 Likes
Not applicable
Q15 and Q31 format for argument or return value or for both?

Have you used these API?

If yes then could you please share some code
0 Likes
Not applicable
Any one could please tell me how to use sqrt api in math.h

If I want to find the square root of

144
200
1000
50000

which API should I use and how to interpret the result
0 Likes
jferreira
Employee
Employee
10 sign-ins 5 sign-ins First like received
Hi,

The functions as explained receive and return Q31 or Q15. In these format you can represent numbers in the range (1.0,-1.0). So you will need to use scaling factors.

#include 
#include "xmc_math.h"

__STATIC_INLINE float q15_to_float(int16_t a) { return (float)a / 0x8000; }
__STATIC_INLINE float q31_to_float(int32_t a) { return (float)a / 0x80000000; }
__STATIC_INLINE int32_t float_to_q15(float a) { return (int16_t)((a * 0x8000) + 0.5); }
__STATIC_INLINE int32_t float_to_q31(float a) { return (int32_t)((a * 0x80000000) + 0.5); }

int main(void)
{
int32_t a;
float b;

a = XMC_MATH_CORDIC_Q31_Sqrt(0x40000000);
b = q31_to_float(a);

...



If you want to use the sqrt from the standard library:

#include
int main(void)
{
float b;
b = sqrtf(0.5F);
...


Here the result should be the same as in the first example 0x40000000 is quivalent to 0.5F in signed Q31.

Regards,
Jesus
0 Likes
jferreira
Employee
Employee
10 sign-ins 5 sign-ins First like received
BTW: If you want to use floating point in cortex M0, you may want to give a try at https://www.quinapalus.com/qfplib.html
0 Likes
Not applicable
@jferreira : Thanks for your kind reply


float f;
int32_t x, y;

f = 144.0;
y = float_to_q15(f);
x = XMC_MATH_CORDIC_Q15_Sqrt((int16_t)y);
y = q15_to_float((int16_t)x);

f = 34567.0;
y = float_to_q31(f);
x = XMC_MATH_CORDIC_Q31_Sqrt(y);
y = q31_to_float((int32_t)x);



In both cases I am getting result zero.

You can try any number and see the result.

For finding the square root of 144 and 34567, Am i following the correct rule shown above?

Could you please guide me for the above two values
0 Likes
Not applicable
Any one to help and guide me?
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
You don't understand Q Format.

It could only represent [-1,1) range.

You will need to normalize the input and output.
0 Likes
Not applicable
So for 144 and 34567, I have to use

float_to_Q15(144) and float_to_Q31(34567) first before calling sqrt function?

How will I get values -1 To 1 for 144 and 34567?

I couldnt understand this logic.

Please guide me what need to be done for any value.
0 Likes
Not applicable
Any one who can tell me how can I convert any number like 200, 1000 ... to Q15 or Q31 format?
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
Conversion
Float to Q

To convert a number from floating point to Qm.n format:

Multiply the floating point number by 2n
Round to the nearest integer

Q to float

To convert a number from Qm.n format to floating point:

Convert the number to floating point as if it were an integer, in other words remove the binary point
Multiply by 2?n
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
0 Likes
Not applicable
@aurixuser previously as per you

You don't understand Q Format.

It could only represent [-1,1) range.

You will need to normalize the input and output.


Now if I try to change 200 to Q15

200 * 2^15 = 200 * 32768 = 6553600


But this value is greater than 1. Moreover 200 is not a floating point number, so is it required to change it to Q format?

Its a request can you please give me an example for just one value like 200, 1500 to find the square root using API defined in math.h

So that not only me but others can also understand the concept.
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
For example.

//First , normalization
float v1_f = 200/32767.0;
float v2_f = 32767/2147483647.0;

//second, convert the normalized results into Q Format
v1_q15 = float_to_q15(v1_f);
v2_q31 = float_to_q31(v2_f);

//Third, do the Q format math
......
....
//Finally, convert the result of Q Format math into the final float result if you need, it is optional
float result_1_f = q15_to_float(result_1_q15);
float result_2_f = q31_to_float(result_2_q31);
0 Likes
User15589
Level 1
Level 1
First like given 5 replies posted Welcome!
It is incredible, how a beginner user couldn't receive any descent support in this forum. I am very dissapointed with the kind of answers from an advanced user.
0 Likes