主要内容

cordiccos

基于 CORDIC 的余弦逼近

说明

y = cordiccos(theta) 使用 CORDIC 算法逼近计算 theta 的余弦。

y = cordiccos(theta,niters) 指定 niters 次 CORDIC 算法迭代。

示例

示例

全部折叠

比较 cordiccos 算法的各种迭代产生的结果与双精度 cos 函数的结果。

创建 [0,2*pi) 之间的 1024 个点。

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

将定点类型设置为有符号 12 位定点数据类型。使用具有双精度输入的 cos 函数作为参考。

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

使用 12 位量化输入并将迭代次数从 2 变为 10。比较定点 CORDIC 结果与双精度三角函数结果。

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

绘制结果。

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

在经过 10 次迭代后,CORDIC 算法将 theta 的余弦逼近到与双精度余弦结果相差 0.005187 的范围内。

输入参数

全部折叠

以弧度为单位的输入角度,指定为有符号或无符号标量、向量、矩阵或多维数组。theta 的所有值必须为实数且在 [–2π 2π) 范围内。

CORDIC 算法执行的迭代次数,指定为正整数值标量。如果未指定 niters 或指定的值太大,算法将使用最大值。对于定点运算,最大迭代次数为 theta 的字长减 1。对于浮点运算,最大值是 52(双精度)或 23(单精度)。增加迭代次数可以得出更准确的结果,但也会增加计算开销和延迟。

输出参量

全部折叠

theta 的基于 CORDIC 的余弦逼近,以标量、向量、矩阵或多维数组形式返回。

当函数的输入是浮点数时,输出数据类型与输入数据类型相同。当输入为定点时,输出的字长与输入相同,小数长度等于 WordLength - 2

算法

全部折叠

参考

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

扩展功能

全部展开

版本历史记录

在 R2010a 中推出