473,772 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CORDIC arctan function with fixed point (i`m using Q15 format)

10 New Member
i`m doing my thesis comparing CORDIC with polynomial in counting arctan with fixed point. I`m using Q15 format now. I`m using this site CORDIC arctan as a referenced when making with floating point. The problem there`s a lot of error when i try to make it with fixed point.

this is my program

Expand|Select|Wrap|Line Numbers
  1. #include "Unit1.h"
  2. #include "math.h"
  3. #include "fixed_math.hpp"
  4. #define MAXBITS 15
  5. static  float invGain1;
  6. static  float atanTable[MAXBITS];
  7. static  float gain1Cordic();
  8.  
  9.  
  10. void initCordic()
  11. {
  12.     /* must call this first to initialise the constants.
  13.      * of course, here i use the maths library, but the
  14.      * values would be precomputed.
  15.      */
  16.     float t = 1.0f;
  17.     int i;
  18.     for (i = 0; i < MAXBITS; ++i)
  19.     {
  20.         atanTable[i] = atan(t);
  21.         t /= 2;
  22.     }
  23.  
  24.     /* set constants */
  25.     invGain1 = 1/gain1Cordic();
  26. }
  27.  
  28. /* CORDIC m=1, y-->0 */
  29.  
  30. Q15 cordic(Q15 &x0, Q15 &y0, Q15 &z0, Q15 vecmode)
  31. {
  32.  
  33.     short t;
  34.     Q15 x, y, z;
  35.     int i;
  36.  
  37.     t = 1.0f;
  38.     x = x0; y = y0; z = z0;
  39.  
  40.     for (i = 0; i < MAXBITS; ++i)
  41.     {
  42.  
  43.         double x1;
  44.  
  45.         if (vecmode >= 0.0f && y < vecmode || vecmode<0.0f  && z >= 0.0f)
  46.         {
  47.             Q15 x1 = x - y>>t;
  48.             y = y + x>>t;
  49.             z = z - atanTable[i];
  50.         }
  51.         else
  52.         {
  53.             Q15 x1 = x + y>>t;
  54.             y = y - x>>t;
  55.             z = z + atanTable[i];
  56.         }
  57.  
  58.          x = x1;
  59.         t /= 2;
  60.     }
  61.  
  62.     x0 = x;
  63.     y0 = y;
  64.     z0 = z;
  65.  
  66. }
  67.  
  68.  
  69. static  float gain1Cordic()
  70. {
  71.     /* compute gain by evaluating cos(0) without inv gain */
  72.     float x, y, z;
  73.  
  74.     x = 1;
  75.     y = 0;
  76.     z = 0;
  77.     cordic(&x, &y, &z, 1.0f);
  78.     return x;
  79. }
  80.  
  81. Q15 atanCordic(Q15 a)
  82. {
  83.     /* domain: all a */
  84.     Q15 x = 1.0f;
  85.     Q15 z = 0.0f;
  86.     cordic(&x, &a, &z, 0.0f);
  87.     return z;
  88. }
  89.  
  90. void __fastcall TForm1::Button1Click(TObject *Sender)
  91. {
  92.     AnsiString buf;
  93.     float x;
  94.     float v,c;
  95.  
  96.  
  97.     initCordic();
  98.  
  99.     for (x=0.0;x<=1.0;x=x+0.1)
  100.     {
  101.  
  102.        Q15 y = atanCordic(x);
  103.         v = atan(x);
  104.  
  105.  
  106.         Memo1->Lines->Add(buf.sprintf("%5.2f\n", x));
  107.         Memo2->Lines->Add(buf.sprintf("%10.6f\n",Q15ToShort(v)/32768.0));
  108.         Memo3->Lines->Add(buf.sprintf("%10.6f\n",Q15ToShort(y)/32768.0));
  109.  
  110.     }
  111.  
  112.  
  113. }
  114.  
  115.  
anyone can help?

if you have suggestion about making it with others rather than Q15 format, as long it is fixed point, it is very usefull to
Feb 7 '07
12 9601
RedSon
5,000 Recognized Expert Expert
Would your professor not give limited permission under an academic free license for us to view his/her code?
Feb 12 '07 #11
MMcCarthy
14,534 Recognized Expert Moderator MVP
Would your professor not give limited permission under an academic free license for us to view his/her code?
It would have to be by PM if he/she does as it cannot be publicly published on the site.

Mary
Feb 12 '07 #12
astri
10 New Member
finally i can solved the errors in my program , but the counting result is wrong.Take a look in my program , where is my mistakes?

*ps i had count arctan using CORDIC in floating point and i use the same method when counting it in fixed point only in here i use Q15 class. i`m bold-ing the font which is different from my floating point program and Italic font is what i use for the floating point.

Expand|Select|Wrap|Line Numbers
  1. #include <vcl.h>
  2. #pragma hdrstop
  3.  
  4.  
  5. #include "Unit1.h"
  6. #include "math.h"
  7. #include "fixed_math.hpp"
  8. #define MAXBITS 15
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma resource "*.dfm"
  12. TForm1 *Form1;
  13. //---------------------------------------------------------------------------
  14. __fastcall TForm1::TForm1(TComponent* Owner)
  15.         : TForm(Owner)
  16. {
  17. }
  18. //---------------------------------------------------------------------------
  19. static float atanTable[MAXBITS];
  20. static double atanTable[MAXBITS]; 
  21.  
  22.  
  23. void initCordic()
  24. {
  25.     /* must call this first to initialise the constants.
  26.      * of course, here i use the maths library, but the
  27.      * values would be precomputed.
  28.      */
  29.  
  30.     float t = 1.0f;
  31.    double t = 1;
  32.     int s;
  33.     for (s = 0; s < MAXBITS; ++s)
  34.     {
  35.         atanTable[s] = atan(t);
  36.         t /= 2;
  37.     }
  38.  
  39. }
  40.  
  41.  
  42. /* CORDIC m=1, y-->0 */
  43.  
  44.  
  45.  
  46. Q15 cordic(Q15* x0, Q15* y0, Q15* z0, Q15 vecmode)
  47.  void cordic(double* x0, double* y0, double* z0, double vecmode)
  48. {
  49.  
  50.     short t;
  51.    Q15 x, y, z;
  52.    double t;
  53.    double x, y, z;
  54.  
  55.     int s;
  56.  
  57.     t = 1.0f;
  58.     x = *x0; y = *y0; z = *z0;
  59.  
  60.     for (s = 0; s < MAXBITS; ++s)
  61.     {
  62.  
  63.         Q15 x1;
  64.         double x1;        
  65.  
  66.  
  67.         if (vecmode >= 0.0f && y < vecmode || vecmode<0.0f  && z >= 0.0f)
  68.         {
  69.             Q15 x1 = x - y>>t;
  70.             y = y + x>>t;
  71.             z = z - Q15(atanTable[s]);
  72.         }
  73.         else
  74.         {
  75.             Q15 x1 = x + y>>t;
  76.             y = y - x>>t;
  77.             z = z + Q15(atanTable[s]);
  78.         }
  79.  
  80.         x=x1;
  81.         t /= 2;
  82.     }
  83.  
  84.     *x0 = x;
  85.     *y0 = y;
  86.     *z0 = z;
  87.  
  88.     return z;             
  89. }
  90.  
  91.  
  92. Q15 atanCordic(Q15 a)
  93. double atanCordic(double a)
  94. {
  95.     /* domain: all a */
  96. Q15 x = 1.0f;
  97.     Q15 z = 0.0f;
  98.     double x = 1;
  99.     double z = 0;
  100.     cordic(&x, &a, &z, 0.0f);              
  101.     return z;
  102. }
  103.  
  104. void __fastcall TForm1::Button1Click(TObject *Sender)
  105. {
  106.     AnsiString buf;
  107.     float x;
  108.     float y;
  109.     double v,y,r;
  110.     double x;
  111.  
  112.  
  113.     initCordic();
  114.  
  115.     for (x=0.0;x<=1.0;x=x+0.1)
  116.     {
  117.  
  118.        Q15 v = atanCordic(x);
  119.        v = atanCordic(x);
  120.         y = atan(x);
  121.  
  122.         Memo1->Lines->Add(buf.sprintf("%5.2f\n", x));
  123.         Memo2->Lines->Add(buf.sprintf("%10.6f\n",Q15ToShort(v)/32768.0));
  124.         Memo3->Lines->Add(buf.sprintf("%10.6f\n",Q15ToShort(y)/32768.0));
  125.  
  126.     }
  127.  
  128.  
  129. }
  130. //---------------------------------------------------------------------------
  131.  
  132.  
  133.  
  134.  
Feb 19 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

26
9700
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " 4". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with the exception of strTotal). The left most positions of the string object are being padded with one...
4
7852
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
8
6445
by: Shi Mu | last post by:
any python module to calculate sin, cos, arctan?
9
4195
by: pout | last post by:
What are the purposes of fixed-point? When should it be used? I read: #define Int2Fixed(x) (((long)(short)x) << 16) and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to integer or decimal part? For example, if in 8.24, should the macro be: #define Int2Fixed(x) (((long)(short)x) << 24)?
5
10695
by: Davidlohr Bueso | last post by:
Hi, I've been trying to write some trigonometric functions of the math library (sin, cos, tan, etc, etc.) in C and I've learned that instead of using series, the best way to go is by using the CORDIC algorithm. The problem is that I haven't been able to find the algorithm itself, just brief descriptions of it. Does anyone have any experience with this? Thanks! Dave
5
16919
by: Gerald | last post by:
Recently, my program need to be run in embeded enviroment, and I cann't use standard library. But I need to use arctan(x), so I implement it like the following: inline double pow(double x, size_t n) { if (n == 0) return 1; else if (n % 2 == 0) return pow(x * x, n >> 1); else
4
5826
by: riya1012 | last post by:
hello guys, I need some help from you. I am doing a DSP project and for that I need to do some C coding for the conversion of sample data which is in floating point representation to fixed point representation. the sample data is in floating point like 0.224128 2.299965 0.448350 -1.779926
11
4839
by: Dave Townsend | last post by:
Hi, I'm working on an implementation of the CORDIC algorithm for a library of functions. One of the benefits of the CORDIC algorithm is that you can implement it with simple additions of numbers and division of (doubles) by powers of two. However, there doesn't seem to be any mechanism in the C/C++ language to direct the compiler to do this efficient form of division,
4
9371
by: astri | last post by:
i`m doing thesis about comparing calculation of arctan by polynomial and CORDIC. I`ve read a lot of journal and books about CORDIC and this is what i understand. 1. make x and y 2. make iterations 3. process with cordic calculation 4. the arctan result is calculating by arctan(y/x). what i m confused that in cordic equation there`s Zi=Zo-arctan(2^-i)
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.