Main Content

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

生成した C コードを使用した修正ピリオドグラムの計算

ウィンドウを使用して、入力信号の修正ピリオドグラム パワー スペクトル密度 (PSD) 推定を返す関数 periodogram_data.m を作成します。この関数は、入力信号の長さと等しい離散フーリエ変換点の数を指定します。

type periodogram_data
function [pxx,f] = periodogram_data(inputData,window)
%#codegen
nfft = length(inputData);
[pxx,f] = periodogram(inputData,window,nfft);
end

codegen (MATLAB Coder)を使用して MEX 関数を生成します。

  • この関数の %#codegen 命令は、MATLAB® コードがコード生成用であることを指定します。

  • -args オプションは、MEX ファイルへの入力のサイズ、クラス、および実数/複素数を定義する引数の例を指定します。この例の場合、inputData を 1024 行 1 列の倍精度のランダム ベクトルとして指定し、window を長さ 1024 のハミング ウィンドウとして指定します。続く MEX 関数の呼び出しでは、1024 サンプルの入力信号とウィンドウを使用します。

  • MEX 関数の名前を変更する場合は、-o オプションを使用します。

  • コード生成レポートを表示するには、codegen コマンドの最後に -report オプションを追加します。

codegen periodogram_data -args {randn(1024,1),hamming(1024)}
Code generation successful.

関数 periodogram と生成した MEX 関数を使用して、1024 サンプルのノイズを含んだ正弦波の PSD 推定を計算します。2π/5 ラジアン/サンプルの正弦波正規化周波数とハン ウィンドウを指定します。2 つの推定をプロットして、両者が一致することを確認します。

N = 1024;
x = 2*cos(2*pi/5*(0:N-1)') + randn(N,1);
periodogram(x,hann(N))
[pxMex,fMex] = periodogram_data(x,hann(N));
hold on
plot(fMex/pi,pow2db(pxMex),':','Color',[0 0.4 0])
hold off
grid on
legend('periodogram','MEX function')

Figure contains an axes object. The axes object with title Periodogram Power Spectral Density Estimate, xlabel Normalized Frequency ( times pi blank rad/sample), ylabel Power/frequency (dB/(rad/sample)) contains 2 objects of type line. These objects represent periodogram, MEX function.