Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

Software Implementation of Trigonometric Functions Using CORDIC Algorithm


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.

References
1) 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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.