メインコンテンツ

cordicsqrt

平方根の CORDIC ベースの近似

説明

y=cordicsqrt(u) は CORDIC アルゴリズムの実装を使用して u の平方根を計算します。

y=cordicsqrt(u, niters) は CORDIC アルゴリズムの niters 反復を実行して 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 カーネルの 3 回の反復により 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 ゲイン係数によって出力をスケーリングするかどうかを指定するブール値。ScaleOutputtrue または 1 に設定すると、出力値は定数で乗算され、余分な計算が必要になります。ScaleOutputfalse または 0 に設定した場合は、出力はスケーリングされません。

データ型: 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 で導入