CORDIC XMC_MATH_Q0_23 format concept

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

cross mob
Not applicable
Hi All,

sin(1°) = 0.017452
sin(2°) = 0.034899
sin(3°) = 0.052336
sin(4°) = 0.069756
sin(5°) = 0.087156

Above are the sine values for different angles.

If I want to generate the sine values using CORDIC coprocessor what needs to be done?

1) I have to change 1°, 2° ... so on into radians like

 
XMC_MATH_Q0_23_t Radians = 0;
XMC_MATH_Q0_23_t Result = 0;
uint32_t Angle = 1; /* Angles in degree, 1, 2, 3, 4, ... */

/* Utility macros */
Radians = XMC_MATH_Q0_23(Angle);


2) This Radian value I have to send to API :

 Result  = XMC_MATH_CORDIC_Sin(Radians); 


3) The above two steps are correct? What I grasp is correct?

4) If the above two steps are correct then ... how the Result value which is in XMC_MATH_Q0_23_t format can be converted normal format

like : value of sin(1°) is 0.017452

Please guide and reply me the desired concept.

Regards,
Tinchu
0 Likes
10 Replies
User12775
Level 5
Level 5
First solution authored First like received
See this demo:

/*********************************************************************************************************************
* HEADER FILES
********************************************************************************************************************/

#include //SFR declarations of the selected device
#include

int main(void)
{
/* variable initialization */
XMC_MATH_Q0_23_t angle = 0x2AAAAA; // (pi/3)*8388608/pi
XMC_MATH_Q0_23_t calculation_result;

/* cosine of angle calculation */
calculation_result = XMC_MATH_CORDIC_Cos(angle);

while(1)
{

}
return 0;
}
0 Likes
lock attach
Attachments are accessible only for community members.
User12775
Level 5
Level 5
First solution authored First like received
For your reference:



0 Likes
Not applicable
Hi Aurixuser!!!

As per the docs :
Input_angle = (angle_in_rad) * 8388608 / π

This can be achieved by lib macro : #define XMC_MATH_Q0_23(x)

1) Now Input_angle should be of type XMC_MATH_Q0_23_t

2) What should be the data type for angle_in_rad or x. Because this value will be in float i.e. decimal places.

So we should make this variable as float?

Below is the table for sine cosine and tan based on angel in degree and radian


Degrees Radian Measure Sin Cos Tan
0 0.00000 0.00000 1.00000 0.00000
1 0.01745 0.01745 0.99985 0.01746
2 0.03491 0.03490 0.99939 0.03492


3) The result of sine cosine API is of type XMC_MATH_Q0_23_t. How can we get the actual sine cosine value out of this?

Like for sin(2) or sin(0.03491) -> 0.03490

I am really stuck and as there is no examples showing all these concepts I am really confused.

Please guide me ASAP.

Regards,
Tinchu
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
Q is a fixed point number format where the number of fractional bits (and optionally the number of integer bits) is specified. For example, a Q15 number has 15 fractional bits; a Q1.14 number has 1 integer bit and 14 fractional bits. Q format is often used in hardware that does not have a floating-point unit and in applications that require constant resolution.
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
And have you read the application note I have attached in my last reply?
2564.attach
2565.attach
0 Likes
Not applicable
Hi,

@aurixuser , thanx alot for your kind reply and guidance.

As per your instructions below are the values which i get from sine math function of Cordic Coprocessor:


TIME(in us) Angle (in Q_0_23 format) Angle(in radians) Angle(in degree) Sine(in Q_0_23 format) Sine(Real)
===============================================================================================================================================================
100 83886.08 0.0314 1.8 263492 0.03139484
200 167772.16 0.0628 3.6 526724 0.062758729
300 251658.24 0.0942 5.4 789436 0.094060746
400 335544.32 0.1256 7.2 1051370 0.12527003

================================================================================================================================================================




As you can see sine values in Q_0_23 format and real.



  • Total samples per 100 us for full cycle of sine i.e. for 20 ms is 200 samples.

  • PWM is 10KHz and the period value is 3199 with center aligned mode

  • for 2% duty cycle the value of CRS should be : 3160

  • for 99% duty cycle the value of CRS should be : 35.


The problem now I am facing is for sin(90) the value is 8388606 and for sin(270) is -8388607.

So as per the above table the value of sine will be in the range : 8388606 to -8388607

How can I make the algo to change the value of duty cycle or equation.

for 8388606 the CRS value should be 35
for -8388607 the CRS value should be 3160

Could you please guide me in this regards?

Regards,
Tinchu
0 Likes
Not applicable
Hi,

@aurixuser , thanx alot for your kind reply and guidance.

As per your instructions below are the values which i get from sine math function of Cordic Coprocessor:


TIME(in us) Angle (in Q_0_23 format) Angle(in radians) Angle(in degree) Sine(in Q_0_23 format) Sine(Real)
===============================================================================================================================================================
100 83886.08 0.0314 1.8 263492 0.03139484
200 167772.16 0.0628 3.6 526724 0.062758729
300 251658.24 0.0942 5.4 789436 0.094060746
400 335544.32 0.1256 7.2 1051370 0.12527003

================================================================================================================================================================




As you can see sine values in Q_0_23 format and real.



  • Total samples per 100 us for full cycle of sine i.e. for 20 ms is 200 samples.

  • PWM is 10KHz and the period value is 3199 with center aligned mode

  • for 2% duty cycle the value of CRS should be : 3160

  • for 99% duty cycle the value of CRS should be : 35.


The problem now I am facing is for sin(90) the value is 8388606 and for sin(270) is -8388607.

So as per the above table the value of sine will be in the range : 8388606 to -8388607

How can I make the algo to change the value of duty cycle or equation.

for 8388606 the CRS value should be 35
for -8388607 the CRS value should be 3160

Could you please guide me in this regards?

Regards,
Tinchu
0 Likes
Not applicable
Hi,

@aurixuser , thanx alot for your kind reply and guidance.

As per your instructions below are the values which i get from sine math function of Cordic Coprocessor:


TIME(in us) Angle (in Q_0_23 format) Angle(in radians) Angle(in degree) Sine(in Q_0_23 format) Sine(Real)
===============================================================================================================================================================
100 83886.08 0.0314 1.8 263492 0.03139484
200 167772.16 0.0628 3.6 526724 0.062758729
300 251658.24 0.0942 5.4 789436 0.094060746
400 335544.32 0.1256 7.2 1051370 0.12527003

================================================================================================================================================================




As you can see sine values in Q_0_23 format and real.



  • Total samples per 100 us for full cycle of sine i.e. for 20 ms is 200 samples.

  • PWM is 10KHz and the period value is 3199 with center aligned mode

  • for 2% duty cycle the value of CRS should be : 3160

  • for 99% duty cycle the value of CRS should be : 35.


The problem now I am facing is for sin(90) the value is 8388606 and for sin(270) is -8388607.

So as per the above table the value of sine will be in the range : 8388606 to -8388607

How can I make the algo to change the value of duty cycle or equation.

for 8388606 the CRS value should be 35
for -8388607 the CRS value should be 3160

Could you please guide me in this regards?

Regards,
Tinchu
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
Build a table and look up it.
0 Likes