Main Content

cordiccos

CORDIC-based approximation of cosine

Description

y = cordiccos(theta) computes the cosine of theta using a CORDIC algorithm approximation.

y = cordiccos(theta,niters) specifies niters iterations of the CORDIC algorithm.

example

Examples

collapse all

Compare the results produced by various iterations of the cordiccos algorithm to the results of the double-precision cos function.

Create 1024 points between [0,2*pi).

stepSize = pi/512;
thRadDbl = 0:stepSize:(2*pi - stepSize);

Set the fixed-point type to be a signed, 12-bit fixed-point data type. Use the cos function with a double-precision input as reference.

thRadFxp = fi(thRadDbl,1,12);
cosThRef = cos(double(thRadFxp));

Use 12-bit quantized inputs and vary the number of iterations from 2 to 10. Compare the fixed-point CORDIC results to the double-precision trigonometric function results.

for niters = 2:2:10
    cdcCosTh  = cordiccos(thRadFxp,niters);
    errCdcRef = cosThRef - double(cdcCosTh);    
end

Plot the results.

figure
hold on
axis([0 2*pi -1.25 1.25]);
    plot(thRadFxp,cosThRef,'b');
    plot(thRadFxp,cdcCosTh,'g');
    plot(thRadFxp,errCdcRef,'r');
    ylabel('cos(\Theta)');
    gca.XTick = 0:pi/2:2*pi;
    gca.XTickLabel = {'0','pi/2','pi','3*pi/2','2*pi'};
    gca.YTick = -1:0.5:1;
    gca.YTickLabel = {'-1.0','-0.5','0','0.5','1.0'};
    ref_str = 'Reference: cos(double(\Theta))';
    cdc_str = sprintf('12-bit CORDIC cosine; N = %d',niters);
    err_str = sprintf('Error (max = %f)', max(abs(errCdcRef)));
    legend(ref_str,cdc_str,err_str);

Figure contains an axes object. The axes object with ylabel cos( Theta ) contains 3 objects of type line. These objects represent Reference: cos(double(\Theta)), 12-bit CORDIC cosine; N = 10, Error (max = 0.005047).

After 10 iterations, the CORDIC algorithm has approximated the cosine of theta to within 0.005187 of the double-precision cosine result.

Input Arguments

collapse all

Input angle in radians, specified as a signed or unsigned scalar, vector, matrix, or multidimensional array. All values of theta must be real and in the range [–2π 2π).

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 it also increases the expense of the computation and adds latency.

Output Arguments

collapse all

CORDIC-based approximation of the 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 WordLength2.

Algorithms

collapse all

References

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

[2] 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.

[3] 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.

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

Extended Capabilities

expand all

Version History

Introduced in R2010a