Main Content

cast

変数を別のデータ型にキャスト

説明

b = cast(a,'like',p) は、p と同じ numerictype、実数/複素数および fimatha を変換します。ap が両方とも実数の場合は、b も実数です。それ以外の場合、b は複素数です。

すべて折りたたむ

8 ビット整数のスカラーを定義します。

a = int8(5);

語長が 24 で小数部の長さが 12 である符号付き fi オブジェクトを作成します。

p = fi([],1,24,12);

指定された fi オブジェクト pnumerictype、実数/複素数および fimath をもつ固定小数点に a を変換します。

b = cast(a, 'like', p)
b = 
     5

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

1 の 2 行 3 列の行列を定義します。

A = ones(2,3);

語長が 16 で小数部の長さが 8 である符号付き fi オブジェクトを作成します。

p = fi([],1,16,8);

Ap と同じデータ型および実数/複素数に変換します。

B = cast(A,'like',p)
B = 
     1     1     1
     1     1     1

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 8

アルゴリズムそのものを変更しなくてもさまざまなデータ型で実行できるような MATLAB® アルゴリズムを作成します。アルゴリズムを再利用するには、アルゴリズムとは別にデータ型を定義します。

このアプローチに従うと、浮動小数点データ型でアルゴリズムを実行してベースラインを定義できます。その後、さまざまな固定小数点データ型でアルゴリズムをテストすることによって、元の MATLAB コードに変更を加えなくても、固定小数点の動作をベースラインと比較できるようになります。

MATLAB 関数 my_filter を作成します。この関数は、係数と入出力データのデータ型を定義する構造体である T を入力パラメーターとして受け入れます。

function [y,z] = my_filter(b,a,x,z,T)
    % Cast the coefficients to the coefficient type
    b = cast(b,'like',T.coeffs);
    a = cast(a,'like',T.coeffs);
    % Create the output using zeros with the data type
    y = zeros(size(x),'like',T.data);
    for i = 1:length(x)
        y(i) = b(1)*x(i) + z(1);
        z(1) = b(2)*x(i) + z(2) - a(2) * y(i);
        z(2) = b(3)*x(i)        - a(3) * y(i);
    end
end

MATLAB 関数 zeros_ones_cast_example を作成します。この関数は、浮動小数点ステップ入力と固定小数点ステップ入力をもつ my_filter を呼び出して、結果を比較します。

function zeros_ones_cast_example

    % Define coefficients for a filter with specification
    % [b,a] = butter(2,0.25)
    b = [0.097631072937818   0.195262145875635   0.097631072937818];
    a = [1.000000000000000  -0.942809041582063   0.333333333333333];

    % Define floating-point types
    T_float.coeffs = double([]);
    T_float.data   = double([]);

    % Create a step input using ones with the 
    % floating-point data type
    t = 0:20;
    x_float = ones(size(t),'like',T_float.data);

    % Initialize the states using zeros with the 
    % floating-point data type
    z_float = zeros(1,2,'like',T_float.data);

    % Run the floating-point algorithm
    y_float = my_filter(b,a,x_float,z_float,T_float);
     
    % Define fixed-point types
    T_fixed.coeffs = fi([],true,8,6);
    T_fixed.data   = fi([],true,8,6);

    % Create a step input using ones with the 
    % fixed-point data type
    x_fixed = ones(size(t),'like',T_fixed.data);

    % Initialize the states using zeros with the 
    % fixed-point data type
    z_fixed = zeros(1,2,'like',T_fixed.data);

    % Run the fixed-point algorithm
    y_fixed = my_filter(b,a,x_fixed,z_fixed,T_fixed);
     
    % Compare the results
    coder.extrinsic('clf','subplot','plot','legend')
    clf
    subplot(211)
    plot(t,y_float,'co-',t,y_fixed,'kx-')
    legend('Floating-point output','Fixed-point output')
    title('Step response')
    subplot(212)
    plot(t,y_float - double(y_fixed),'rs-')
    legend('Error')
    figure(gcf)
end

入力引数

すべて折りたたむ

fi オブジェクトまたは数値変数として指定される変数。

複素数のサポート: あり

fi オブジェクトまたは数値変数として指定されるプロトタイプ。プロトタイプを使用して複素数オブジェクトを指定するには、プロトタイプの値を指定しなければなりません。それ以外の場合は、値を指定する必要はありません。

複素数のサポート: あり

ヒント

b = cast(a,'like',p) 構文を使用してアルゴリズム コードとは別にデータ型を指定すると、以下のことができます。

  • 異なるデータ型でアルゴリズム コードを再利用

  • データ型指定を切り離してアルゴリズムを整理し、データ型ごとにステートメントを切り替え

  • アルゴリズム コードの可読性を向上

  • 固定小数点データ型と浮動小数点データ型を切り替えてベースラインを比較

  • アルゴリズム コードを変更しないで固定小数点設定のバリエーションを切り替え

バージョン履歴

R2013a で導入