主要内容

cordicsqrt

基于 CORDIC 的平方根逼近

说明

y=cordicsqrt(u) 使用 CORDIC 算法实现计算 u 的平方根。

示例

y=cordicsqrt(u, niters) 通过执行 niters 次 CORDIC 算法迭代来计算 u 的平方根。

示例

y=cordicsqrt(___, 'ScaleOutput', B) 根据 B 的布尔值缩放输出。

示例

示例

全部折叠

使用 CORDIC 实现找到 fi 对象 x 的平方根。

x = fi(1.6,1,12);
y = cordicsqrt(x)
y = 
    1.2646

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 12
        FractionLength: 10

由于您没有指定 niters,函数执行最大迭代次数,即 x.WordLength - 1

计算 cordicsqrt 函数结果与双精度 sqrt 函数结果之间的差值。

err = abs(sqrt(double(x))-double(y))
err = 
1.0821e-04

使用 CORDIC 核的三次迭代计算 x 的平方根。

x = fi(1.6,1,12);
y = cordicsqrt(x,3)
y = 
    1.2646

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 12
        FractionLength: 10

计算 cordicsqrt 函数结果与双精度 sqrt 函数结果之间的差值。

err = abs(sqrt(double(x))-double(y))
err = 
1.0821e-04
x = fi(1.6,1,12);
y = cordicsqrt(x, 'ScaleOutput', 0)
y = 
    1.0479

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 12
        FractionLength: 10

输出 y 未按逆 CORDIC 增益因子缩放。

比较 cordicsqrt 算法的 10 次迭代产生的结果与双精度 sqrt 函数的结果。

创建在区间 [0, 2) 内的 500 个点。

stepSize = 2/500;
XDbl = 0:stepSize:2;

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

XFxp = fi(XDbl,1,12);
sqrtXRef = sqrt(double(XFxp));

将 CORDIC 迭代次数设置为 10。

niters = 10;

将定点 CORDIC 结果与双精度 sqrt 函数进行比较。

cdcSqrtX  = cordicsqrt(XFxp,  niters);
errCdcRef = sqrtXRef - double(cdcSqrtX);

绘制结果。

figure
hold on
axis([0 2 -.5 1.5])
plot(XFxp, sqrtXRef,  'b')
plot(XFxp, cdcSqrtX,  'g')
plot(XFxp, errCdcRef, 'r')
ylabel('Sqrt(x)')
gca.XTick = 0:0.25:2;
gca.XTickLabel = {'0','0.25','0.5','0.75','1','1.25','1.5','1.75','2'};
gca.YTick = -.5:.25:1.5;
gca.YTickLabel = {'-0.5','-0.25','0','0.25','0.5','0.75','1','1.25','1.5'};
ref_str = 'Reference: sqrt(double(X))';
cdc_str = sprintf('12-bit CORDIC square root; N = %d', niters);
err_str = sprintf('Error (max = %f)', max(abs(errCdcRef)));
legend(ref_str, cdc_str, err_str, 'Location', 'southeast')

Figure contains an axes object. The axes object with ylabel Sqrt(x) contains 3 objects of type line. These objects represent Reference: sqrt(double(X)), 12-bit CORDIC square root; N = 10, Error (max = 0.002009).

输入参数

全部折叠

数据输入数组,指定为定点或内置数据类型的正标量、向量、矩阵或多维数组。当输入数组包含 0.5 到 2 之间的值时,算法最准确。对超出此范围的输入值执行预归一化和后归一化过程。有关此过程的详细信息,请参阅预归一化和后归一化

数据类型fi|single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

CORDIC 算法执行的迭代次数,指定为正整数值标量。如果您不指定 niters,算法将使用默认值。对于定点输入,niters 的默认值为 u.WordLength - 1。对于浮点输入,niters 的默认值对于双精度为 52;对于单精度为 23。

数据类型fi|single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

布尔值,指定是否按逆 CORDIC 增益因子缩放输出。如果您将 ScaleOutput 设置为 true1,输出值将乘以常量,这会产生额外的计算量。如果您将 ScaleOutput 设置为 false0,输出不会缩放。

数据类型logical

输出参量

全部折叠

输出数组,以标量、向量、矩阵或多维数组形式返回。

算法

全部折叠

参考

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

扩展功能

全部展开

版本历史记录

在 R2014a 中推出