By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

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

P: 10
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 #1
Share this Question
Share on Google+
12 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
astri-

I'm not sure if I can help with the logic of your program (that's a little over my head!), but it's more than likely that I or someone else browsing through would be able to help you with the error messages that you are getting, if you could post those.

sicarie
Feb 8 '07 #2

RedSon
Expert 5K+
P: 5,000
Can you give us an example of your input and output. What kind of error are you getting?
Feb 8 '07 #3

P: 10
i`m bolding the font ONLY for error messages(ignore the warning) in my program

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

[code]
[C++ errorー] Unit1.cpp(67): E2015 'Q15:: operator -(const Q15 &) const' and 'Q15:: operator -(const Q30 &) const' classification are ambiguous retrieval
[C++ errorー] Unit1.cpp(73): E2015 'Q15:: operator +(const Q15 &) const' and 'Q15:: operator +(const Q30 &) const' classification are ambigous retrieval
[C++ error ー] Unit1.cpp(76): E2015 'Q15::Q15(const short)' and 'Q15::Q15(const float)' classification are ambigous retrieval
[C++ warning] Unit1.cpp(84): W8070 return function value
[C++ warning] Unit1.cpp(95): W8030 'x0' parameter(cordic(Q15 &,Q15 &,Q15 &,Q15))is used as temporarily variables
[C++ error] Unit1.cpp(95): E2064 'Q15 &' cant initialized as 'float *'
[C++ error] Unit1.cpp(95): E2342 'x0'parameter is define as Q15 & so it cant be change to float *
[C++ warning] Unit1.cpp(95): W8030 'y0' parameter(cordic(Q15 &,Q15 &,Q15 &,Q15))is used as temporarily variables
[C++ error] Unit1.cpp(95): E2064 'Q15 &' cant initialized as 'float *'
[C++ error] Unit1.cpp(95): E2342 'y0'b parameter is Q15 & type and cant be change to float *
[C++ warning] Unit1.cpp(95): W8030 'z0' parameters(cordic(Q15 &,Q15 &,Q15 &,Q15))is used as temporarily variables
[C++ error] Unit1.cpp(95): E2064 'Q15 &' cant initialized as 'float *'
[C++ error] Unit1.cpp(95): E2342 'z0' parameter is Q15 & type and cant be change to float *
[C++ warning] Unit1.cpp(97): W8004 'z' subtituted value is not use
[C++ warning] Unit1.cpp(97): W8004 'y' subtituted value is not use [C++ warning] Unit1.cpp(104): W8030 'x0' parameter(cordic(Q15 &,Q15 &,Q15 &,Q15))is used as temporarily variables
[C++ error] Unit1.cpp(104): E2064 'Q15 &' is 'Q15 *' cant be initialized
[C++ error] Unit1.cpp(104): E2342 'x0' parameters is Q15 & type and cant be change to Q15 *
[C++ warning] Unit1.cpp(104): W8030 'y0' parameters(cordic(Q15 &,Q15 &,Q15 &,Q15))is used as temporarily variables
[C++ error] Unit1.cpp(104): E2064 'Q15 &' is 'Q15 *' cant be initialized
[C++ error] Unit1.cpp(104): E2342 'y0' parameters is Q15 & type and cant be change to Q15 *
[C++ warning] Unit1.cpp(104): W8030 'z0' パラメータ(cordic(Q15 &,Q15 &,Q15 &,Q15))is used as temporarily variables
[C++ warning] Unit1.cpp(104): E2064 'Q15 &' は 'Q15 *' cant be initialized
[C++ error] Unit1.cpp(104): E2342 'z0' parameters is Q15 & type and cant be change to Q15 *



i`m thinking the main problem in my program is because i`m trying to use fixed point (its about the data type). I dont really understand that much.



*ps i`m using c++builder for japanese so the error message output is in japanese , i`m translating by myself to english (so if it`s sound weird please correct it or ask me)
Feb 9 '07 #4

Expert 100+
P: 1,510
the function definition of cordic() sepcifies that the formal parameters and return type are Q15
Expand|Select|Wrap|Line Numbers
  1. Q15 cordic(Q15 &x0, Q15 &y0, Q15 &z0, Q15 vecmode)
  2.  
you call it with actual parameters of type float
Expand|Select|Wrap|Line Numbers
  1. static  float gain1Cordic()
  2. {
  3.     /* compute gain by evaluating cos(0) without inv gain */
  4.     float x, y, z;
  5.  
  6.     x = 1;
  7.     y = 0;
  8.     z = 0;
  9.     cordic(&x, &y, &z, 1.0f);-->THE MAIN PROBLEM, 
  10.     return x;                     the error messages repeated so much in this line
  11. }
  12.  
where did you get the Q15 fixed point library from?
Feb 9 '07 #5

P: 10
the function definition of cordic() sepcifies that the formal parameters and return type are Q15
Expand|Select|Wrap|Line Numbers
  1. Q15 cordic(Q15 &x0, Q15 &y0, Q15 &z0, Q15 vecmode)
  2.  
you call it with actual parameters of type float
Expand|Select|Wrap|Line Numbers
  1. static  float gain1Cordic()
  2. {
  3.     /* compute gain by evaluating cos(0) without inv gain */
  4.     float x, y, z;
  5.  
  6.     x = 1;
  7.     y = 0;
  8.     z = 0;
  9.     cordic(&x, &y, &z, 1.0f);-->THE MAIN PROBLEM, 
  10.     return x;                     the error messages repeated so much in this line
  11. }
  12.  
where did you get the Q15 fixed point library from?
my professor make it by himself the library, i included it in fixed_math.hpp
Feb 9 '07 #6

RedSon
Expert 5K+
P: 5,000
Can you post the .hpp file with your professors code in it? If it is large consider using the website mediafire.com to upload it and give us the link to it. Also, when you translate error messages, please try to translate word for word. What I mean is don't worry about changing the grammar and word order so that is makes sense in english. We can still get the meaning even though it may not look right, and that way there is less loss in translation. :)
Feb 9 '07 #7

MMcCarthy
Expert Mod 10K+
P: 14,534
Can you post the .hpp file with your professors code in it? If it is large consider using the website mediafire.com to upload it and give us the link to it. Also, when you translate error messages, please try to translate word for word. What I mean is don't worry about changing the grammar and word order so that is makes sense in english. We can still get the meaning even though it may not look right, and that way there is less loss in translation. :)
Sorry guys I've already told the OP they can't as the code is copyrighted.
Feb 12 '07 #8

RedSon
Expert 5K+
P: 5,000
The professor's code is copyrighted? Or the CORDIC code?
Feb 12 '07 #9

P: 10
The professor's code is copyrighted? Or the CORDIC code?
sorry my professor code is copyrighted
Feb 12 '07 #10

RedSon
Expert 5K+
P: 5,000
Would your professor not give limited permission under an academic free license for us to view his/her code?
Feb 12 '07 #11

MMcCarthy
Expert Mod 10K+
P: 14,534
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

P: 10
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

Post your reply

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