Main Content

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

ローパス フィルターの入力信号への適用

サンプル レートが 20 kHz であるという前提で、3 dB 周波数が 2.5 kHz の 4 次バタワース フィルターを作成します。butter のフィルター係数はコード生成の定数でなければなりません。

type ButterFilt
function output_data=ButterFilt(input_data) %#codegen
[b,a]=butter(4,0.25);
output_data=filter(b,a,input_data);
end

このバタワース フィルターを、ノイズを含む正弦波のローパス フィルターとして使用します。

t = transpose(linspace(0,pi,10000));
x = sin(t) + 0.03*randn(numel(t),1);

バタワース フィルターを使用してノイズを含む正弦波をフィルター処理します。フィルター処理された信号をプロットします。

fx = ButterFilt(x);
plot(fx)

Figure contains an axes object. The axes object contains an object of type line.

codegen コマンドを実行して、C ソース コード ButterFilt.c と MEX ファイルを取得します。

codegen ButterFilt -args {zeros(10000,1)} -o ButterFilt_mex -report
Code generation successful: To view the report, open('codegen/mex/ButterFilt/html/report.mldatx')

C ソース コードには、4 次バタワース フィルターの分子と分母の 5 つの係数が、静的定数として含まれています。MEX ファイルを使用してフィルターを適用します。フィルター処理された信号をプロットします。

output_data = ButterFilt_mex(x);
hold on
plot(output_data)
hold off

Figure contains an axes object. The axes object contains 2 objects of type line.