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.

# Software Implementation of Trigonometric Functions Using CORDIC Algorithm

Trigonometric functions are often used in embedded systems. Motor drive control applications such as the Park Transform, Clarke Transform, and PWM generation use trigonometric functions extensively. Various methods exist to compute the trigonometric functions. These include Taylor series, Curve fitting algorithms, and the CORDIC algorithm.

This tutorial describes software implementation of the following fixed point trigonometric routines using the CORDIC Algorithm on Infineon's XC164CS Microcontroller with MAC unit. The implementation of the algorithm is examined concerning accuracy and efficiency:

• Complex Magnitude
• Sine
• Cosine

Routines are provided for signed two's complement arithmetic. First a brief description of the theory behind the algorithm is presented. Then the theory is extended to the implementation of algorithm in XC164CS Processor after which the numerical errors that occur in the fixed point implementation is discussed.

The CORDIC Arithmetic Technique
The Coordinate Rotation DIgital Computer (CORDIC) algorithm is an iterative technique proposed by Volder in 1956. This algorithm can be a very powerful tool in areas where arithmetic or trigonometric function evaluation is heavily utilized, such as digital signal processing, motor control.

The general vector rotational transform rotates a plane vector [X, Y] by an angle Θ to produce a new vector point [Xi+1, Yi+1] as in Equations (1) and (2). The CORDIC Rotation is achieved by the same principle. It rotates the point [X, Y] in series of steps, which are smaller than Θ.

This rotation may be in anti-clockwise direction (increase in Θ) or clockwise direction. Suppose if we wish to achieve a total rotation of 35o, we may rotate our point 30o anticlockwise, followed by 10o anticlockwise, followed by 5o clockwise.

The reason for this simplification is to break down the rotation (Θ) into many steps, each of decreasing size and has each step such that tan is the power of 2, where Θ is the rotational angle. The first seven steps of the set of rotations are shown in Table 1 below.

 Table 1

This would allow us to implement the multiplication by tan Θ as a simple bit shift operation (2-i). Hence Equations (3) and (4) reduce to

From Table 1 it is clear that the total rotation step is 99.88o. Since the rotation can be of clockwise or anticlockwise direction these steps are used to approximate angles between +99.88o to -99.88o. For mathematical simplicity the rotation angles are limited to "90o and +90o. For rotation angles greater than +/-90o additional rotation is required. The cos Θ term (K) is a constant which approaches to 0.6073 after 'n' iterations. The angle Θ is accumulated in Zi+1

The CORDIC Rotator is operated in one of the two modes

1) Rotation Mode
2) Vector mode

The rotation mode rotates the input vector to a specified angle. The vectoring mode rotates the input vector to x axis while recording the angle required to make the rotation (i.e. the direction of rotation is opposite in both the modes). The complex magnitude is computed using vectoring mode, the sine and cosine of the input angle is computed using rotation mode.

Rotation Mode.

The CORDIC equation for rotation mode is

(Equation 7)

Vectoring Mode.

The CORDIC equation for vectoring mode is

(Equation 8)

After 'n' Iterations

(Equation 9)

Computation of Complex magnitude

The Aim of the algorithm is to calculate the magnitude of a complex number C= X + jY. Magnitude of this complex number is given by

The Cordic rotator rotates the input vector to angle Zi for aligning the result vector with the x axis (Figure 1), the result of the operation is a rotation angle and the scaled magnitude of the original vector.

 Figure 1

To extend the region of convergence greater than +/-90o, the phase is rotated by -90o if Y is positive and it is rotated by +90o if Y is negative. The CORDIC Rotation is done with successively smaller values of Z, starting with Z = 45o. The sign of Y decides to add or subtract the phase. In the rotation process C is multiplied with the Inverse CORDIC gain (K) of 0.6073.

Implementation.

The Library is C-callable, hand coded assembly routine written for Infineon's XC164CS Microcontroller with MAC unit. A tasking tool is used for compilation. For the implementation we make the assumption that inputs are in 1Q15 format.

The input data's are scaled down by 2 to avoid the overflow. The rotational gain K needs to be compensated at some stage (i.e. compensation can be done before or after the iteration). In order to scale down the input further, the input X is multiplied by the inverse of gain before the iteration.

### More Insights

 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.