# cordicsincos

CORDIC-based approximation of sine and cosine

## Syntax

``[y,x] = cordicsincos(theta)``
``[y,x] = cordicsincos(theta,niters)``

## Description

````[y,x] = cordicsincos(theta)` computes the sine and cosine of `theta` using a CORDIC algorithm approximation. `y` contains the approximated sine result, and `x` contains the approximated cosine result.```

example

````[y,x] = cordicsincos(theta,niters)` performs the number of CORDIC algorithm iterations specified by `niters`.```

## Examples

collapse all

View the effect of the number of iterations on the result of the CORDIC approximation of sine and cosine.

```wordlength = 8; theta = fi(pi/2,1,wordlength); fprintf('\n\nNITERS\t\tY (SIN)\t ERROR\t LSBs\t\tX (COS)\t ERROR\t LSBs\n'); fprintf('------\t\t-------\t ------\t ----\t\t-------\t ------\t ----\n'); for niters = 1:(wordlength - 1) [y,x] = cordicsincos(theta,niters); y_FL = y.FractionLength; y_dbl = double(y); x_dbl = double(x); y_err = abs(y_dbl - sin(double(theta))); x_err = abs(x_dbl - cos(double(theta))); fprintf(' %d\t\t%1.4f\t %1.4f\t %1.1f\t\t%1.4f\t %1.4f\t %1.1f\n', ... niters,y_dbl,y_err,(y_err*pow2(y_FL)),x_dbl,x_err, ... (x_err*pow2(y_FL))); end fprintf('\n'); ```
``` NITERS Y (SIN) ERROR LSBs X (COS) ERROR LSBs ------ ------- ------ ---- ------- ------ ---- 1 0.7031 0.2968 19.0 -0.7031 0.6958 44.5 2 0.9375 0.0625 4.0 -0.3125 0.3052 19.5 3 0.9688 0.0312 2.0 -0.0625 0.0552 3.5 4 0.9688 0.0312 2.0 0.0625 0.0698 4.5 5 0.9844 0.0156 1.0 0.0000 0.0073 0.5 6 0.9844 0.0156 1.0 0.0312 0.0386 2.5 7 1.0000 0.0000 0.0 0.0156 0.0230 1.5 ```

## Input Arguments

collapse all

Input angle in radians, specified as a scalar, vector, matrix, or multidimensional array. All `theta` values must be in the range [`-2*pi,2*pi`). When `theta` has a fixed-point data type, it must be signed.

Number of iterations the CORDIC algorithm performs, specified as a positive integer-valued scalar.

If you do not specify `niters` or if you specify a value that is too large, the algorithm uses a maximum value. For fixed-point operation, the maximum number of iterations is one less than the word length of `theta`. For floating-point operation, the maximum value is `52` for double or `23` for single.

Increasing the number of iterations can produce more accurate results but also increases the expense of the computation and adds latency.

## Output Arguments

collapse all

CORDIC-based approximated sine of `theta`, returned as a scalar, vector, matrix, or multidimensional array. When the input to the function is floating point, the output data type is the same as the input data type. When the input is fixed point, the output has the same word length as the input and a fraction length equal to the `Wordlength - 2`.

CORDIC-based approximated cosine of `theta`, returned as a scalar, vector, matrix, or multidimensional array. When the input to the function is floating point, the output data type is the same as the input data type. When the input is fixed point, the output has the same word length as the input and a fraction length equal to the `Wordlength - 2`.

collapse all

### CORDIC

CORDIC is an acronym for COordinate Rotation DIgital Computer. The Givens rotation-based CORDIC algorithm is one of the most hardware-efficient algorithms available because it requires only iterative shift-add operations (see References). The CORDIC algorithm eliminates the need for explicit multipliers. Using CORDIC, you can calculate various functions such as sine, cosine, arcsine, arccosine, arctangent, and vector magnitude. You can also use this algorithm for divide, square root, hyperbolic, and logarithmic functions.

Increasing the number of CORDIC iterations can produce more accurate results but also increases the expense of the computation and adds latency.

## Algorithms

collapse all

### Signal Flow Diagrams CORDIC Rotation Kernel X represents the sine, Y represents the cosine, and Z represents theta. The accuracy of the CORDIC rotation kernel depends on the choice of initial values for X, Y, and Z. This algorithm uses these initial values:

### `fimath` Propagation Rules

CORDIC functions discard any local `fimath` attached to the input.

The CORDIC functions use their own internal `fimath` when performing calculations:

• `OverflowAction``Wrap`

• `RoundingMethod``Floor`

The output has no attached `fimath`.

 Volder, Jack E. “The CORDIC Trigonometric Computing Technique.” IRE Transactions on Electronic Computers. EC-8, no. 3 (Sept. 1959): 330–334.

 Andraka, Ray. “A Survey of CORDIC Algorithm for FPGA Based Computers.” In Proceedings of the 1998 ACM/SIGDA Sixth International Symposium on Field Programmable Gate Arrays, 191-200. https://dl.acm.org/doi/10.1145/275107.275139.

 Walther, J.S. “A Unified Algorithm for Elementary Functions.” In Proceedings of the May 18-20, 1971 Spring Joint Computer Conference, 379-386. https://dl.acm.org/doi/10.1145/1478786.1478840.

 Schelin, Charles W. “Calculator Function Approximation.” The American Mathematical Monthly, no. 5 (May 1983): 317-325. https://doi.org/10.2307/2975781.