Pseudo code.
Input vector Z is initialized to the desired angle, Y=0 and X=0.60725.The initialization of X specifies the constant 0.60725 which results from the Cos Θ term.
{ short L; short I, Q; short Z; short tmp_I; short sign; Q=0; I=0.60725; Z=P*pi; //denormalization of input If (Z<0) { sign =1; } //If X is in III/IV Quadrant (Extension of region of convergence) If (abs (Z)>0.5) { Z=1-abs (Z); } //CORDIC Rotation For (L = 0; L < 15; L++) { tmp_I = I; If (Z < 0.0) { I += Q >>L; Q -= tmp_I >>L; Z=Z+ tan-1 (2-L); // value of tan-1 (2-L) is stored in lookup table } else { I -= Q >>L; Q += tmp_I >>L; Z=Z- tan-1 (2-L); } } if (sign==0) { I=I; Q=Q; } else { I=-I; Q=-Q; } }
The Sine of the desired angle is now present in the variable I and the Cosine of the desired angle is in the variable Q. These outputs are within the integer range "32768 to +32767.
Table 5 Output |
Table 6 Cycle count |
Table 7 Code Size (Bytes) |
Numerical Error in CORDIC
The error in CORDIC is split to different factors as approximation error and truncation error (Equation 1). Theoretical realization of CORDIC has infinite iterations which produce the accurate result. But, the practical implementation of CORDIC has finite number of iterations.
This is the cause for approximation error. In general CORDIC Algorithm produces one additional bit of accuracy for each iteration. The truncation error is due to the finite word length effect. For example, consider a fixed-point representation of 5 bits, with the lower order 3 bits after the binary point. If xi = 1.2345678, then the approximate representation of this number is 01001. Hence Q[xi] = 0 * 2l + 1* 20 + 0 * 2-1 + 0*2-2 + 1 * 2-3 = 1 + 0.125 = 1.125.
Hence the quantization error due to finite word length is
Ei = 1.2345678 - 1.125 = 0.1095678 < 2-3 (0.125).
Due to these errors the precision of CORDIC is affected. A Sine wave of 21 samples with input frequency 50 Hz and sampling frequency of 4000 Hz is generated using Ideal CORDIC and the implemented CORDIC.
Figure 3: The Magnitude of the difference between Ideal CORDIC and implemented CORDIC |
The magnitude of the difference between the Ideal CORDIC and the implemented CORDIC is shown in Figure 3 above. The X axis represents the input angle which is normalized and the Y axis represents the approximation and the truncation error.
Acknowledgements
Thanks to Samuel Ginsberg and Richard Armstrong for helping me to understand the concept. Thanks to Manoj Palat, Raghunath lolur, and Sonali Nath for their valuable ideas and code review.
References1) Ray Andraka, "A Survey of CORDIC algorithms for FPGA based computers," International Symposium on Field Programmable Gate Arrays," Proceedings of the 1998 ACM/SIGDA sixth international symposium on Field programmable gate arrays, Pages: 191 - 200,Year 1998
2) Y. H. Hu, "The quantization effects of the CORDIC algorithm," IEEE Trans. Signal Processing, pp. 834-844, Apr. 1992.
3) Sang Yoon Park and Nam Ik Cho, "Fixed point error analysis of CORDIC processor based on the variance propagation," IEEE Transactions on Circuits and Systems I-Fundamental Theory and Applicat, vol. 51 no. 3 pp.573-584, Mar. 2004
4) Samuel Ginsberg, "Compact and Efficient Generation of Trigonometric Functions using a CORDIC algorithm"
5) CORDIC FAQ
6) Infineon Technologies, C166S V2 User manual, 16-Bit Microcontroller V 1.7
Christober Rayappan is a software engineer at Infineon Technologies, with a particular focus on the design and development of DSP Algorithms in embedded processors and FPGAs, and finite word length effects in signal processing. He is currently involved in the development of DSP libraries for Infineon 16-bit microcontrollers.