Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

MATLAB Function ブロックを使用した固定小数点 FIR コードの生成

この例では、固定小数点のローパス直接型 FIR フィルターを Simulink® で作成する方法を説明します。FIR フィルターを作成するには、Fixed-Point Designer™ ソフトウェアと MATLAB Function ブロックを使用します。

モデルを開く

cgen_fi モデルを開きます。

open_system('cgen_fi')

モデルには、ローパス直接型 FIR フィルターを実装する MATLAB Function ブロックが含まれています。MATLAB Function ブロックをダブルクリックして関数 dffirdemo を確認します。

function [yout,zf] = dffirdemo(b,x,zi) %#codegen
%cgen_fi doc model example
%Initialize the output signal yout and the final conditions zf
Ty = numerictype(1,12,8);
yout = fi(zeros(size(x)),'numerictype',Ty);
zf = zi;

% FIR filter code
for k = 1:length(x)
  % Update the states: z = [x(k);z(1:end-1)]  
  zf(:) = [x(k);zf(1:end-1)];
  % Form the output: y(k) = b*z
  yout(k) = b*zf;
end

% Plot the outputs only in simulation.
% This does not generate C code.
figure;
subplot(211);plot(x); title('Noisy Signal');grid;
subplot(212);plot(yout); title('Filtered Signal');grid;function [yout,zf] = dffirdemo(b, x, zi) %#codegen
%codegen_fi doc model example
%Initialize the output signal yout and the final conditions zf
Ty = numerictype(1,12,8);
yout = fi(zeros(size(x)),'numerictype',Ty);
zf = zi;

% FIR filter code
for k=1:length(x);
  % Update the states: z = [x(k);z(1:end-1)]  
  zf(:) = [x(k);zf(1:end-1)];
  % Form the output: y(k) = b*z
  yout(k) = b*zf;
end

% Plot the outputs only in simulation.
% This does not generate C code.
figure;
subplot(211);plot(x); title('Noisy Signal');grid;
subplot(212);plot(yout); title('Filtered Signal');grid;

入力の準備

フィルター係数 b、ノイズ x、および初期状態 zi を定義します。

b = fi_fir_coefficients;
load mtlb
x = mtlb;
n = length(x);
noise = sin(2*pi*2140*(0:n-1)'./Fs);
x = x + noise;
zi = zeros(length(b),1);

プロパティ インスペクターを使用した fimath オブジェクトの定義

1. MATLAB Function ブロックを選択し、[プロパティ インスペクター] を開きます。

2. [MATLAB 関数 fimath] パラメーターで [それ以外を指定] を選択します。エディット ボックスで次の fimath オブジェクトを作成します。

fimath('RoundingMethod','Floor',...
    'OverflowAction','Wrap',...
    'ProductMode','KeepLSB',...
    'ProductWordLength',32,...
    'SumMode','KeepLSB',...
    'SumWordLength',32)

ここで定義する fimath オブジェクトは、ブロック内で作成する fi オブジェクトと、MATLAB Function ブロックへの固定小数点入力と関連付けられます。

[MATLAB Function ブロックの fimath][それ以外を指定] を選択すると、指定した fimath プロパティを常にモデルで使用できるようになります。

モデルのシミュレーション

モデルをシミュレートします。ノイズを含んだ信号とフィルター処理された信号のプロットが表示されます。

sim('cgen_fi')

Figure contains 2 axes objects. Axes object 1 with title Noisy Signal contains an object of type line. Axes object 2 with title Filtered Signal contains an object of type line.

ans = 
  Simulink.SimulationOutput:
                 noisyx: [4001x1 embedded.fi] 
                   tout: [1x1 double] 
                   yout: [4001x1 embedded.fi] 
                     zf: [13x1 embedded.fi] 

     SimulationMetadata: [1x1 Simulink.SimulationMetadata] 
           ErrorMessage: [0x0 char] 

コードの生成

モデルを選択し、"Ctrl+B" を押して、モデルの組み込み可能な C コードをビルドします。現在の作業ディレクトリに cgen_fi_grt_rtw というフォルダーが作成されます。

ファイル cgen_fi_grt_rtw > cgen_fi.c を検査し、モデルから生成されたコードを表示します。

参考

| | |

関連するトピック