ガレスタさんのDIY日記

電子回路、Web、組み込み、自作エフェクターを語るblog

CMSIS DSP:FIRフィルタをやってみる

公開してるがかなり追記の予定



前回はIIRについてやったので今回はFIRについてやってみます。
今回もリアルテック氏(@realteck_KY )に協力してもらってます。


f:id:gsmcustomeffects:20171002044002p:plain

みればわかるとおりに移動平均の数を増やして言った形ですね。
タップ数を増やすとより特性が理想に近くなるというのも頷けますね。

窓関数法

今回は窓関数法で係数を出している

なんで窓関数を使うといいのかはFFTの項を調べれば載ってますので割愛


窓関数として用いているハミング窓は以下のような式です。

\displaystyle
w[m] = 0.54-0.46cos(\frac{2\pi m}{M})

ここはまだ書く余地あり_____________________________________________________________

CMSIS DSP

前回同様にCMSIS DSP APIを使っていく。

導入はここ
gsmcustomeffects.hatenablog.com

やる順序は前回と変わらず

  1. インスタンスの生成
  2. インスタンスの初期化
  3. APIコール

という感じ

インスタンスの生成

arm_fir_instance_f32 S3;

構造体のメンバはこんな感じ

  typedef struct
  {
    uint16_t numTaps;     /**< number of filter coefficients in the filter. */
    float32_t *pState;    /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
    float32_t *pCoeffs;   /**< points to the coefficient array. The array is of length numTaps. */
  } arm_fir_instance_f32;

インスタンスの初期化

フィルタはMATLABのフィルタ設計ツール
カットオフは1kHz、タップ数は300

float32_t buffer3[300] = {0};
float32_t pCoeffs3[300]=
{0.000112,0.000096,0.000077,0.000057,0.000035,0.000012,-0.000012,-0.000037,-0.000063,-0.000089,-0.000114,-0.000140,-0.000165,-0.000188,-0.000211,
 -0.000230,-0.000248,-0.000262,-0.000272,-0.000278,-0.000280,-0.000276,-0.000267,-0.000251,-0.000230,-0.000202,-0.000168,-0.000127,-0.000081,-0.000028,
0.000029,0.000091,0.000157,0.000225,0.000295,0.000365,0.000434,0.000500,0.000562,0.000618,0.000666,0.000705,0.000733,0.000750,0.000753,0.000741,0.000714,
0.000670,0.000611,0.000534,0.000441,0.000333,0.000209,0.000073,-0.000075,-0.000233,-0.000398,-0.000568,-0.000738,-0.000907,-0.001070,-0.001225,-0.001367,						  -0.001492,-0.001598,-0.001681,-0.001737,-0.001764,-0.001759,-0.001721,-0.001648,-0.001538,-0.001393,-0.001211,-0.000995,-0.000746,-0.000467,-0.000162,0.000167,						  0.000513,0.000871,0.001236,0.001600,0.001958,0.002301,0.002623,0.002917,0.003174,0.003388,0.003553,0.003662,0.003710,0.003692,0.003604,0.003444,0.003210,0.002902,						  0.002521,0.002070,0.001551,0.000971,0.000336,-0.000346,-0.001066,-0.001814,-0.002577,-0.003342,-0.004097,-0.004828,-0.005519,-0.006156,-0.006724,-0.007208,-0.007593,						  -0.007867,-0.008015,-0.008026,-0.007890,-0.007598,-0.007142,-0.006516,-0.005717,-0.004745,-0.003598,-0.002282,-0.000800,0.000839,0.002625,0.004547,0.006589,0.008737,						  0.010973,0.013276,0.015626,0.018001,0.020379,0.022736,0.025049,0.027294,0.029448,0.031488,0.033394,0.035143,0.036719,0.038103,0.039280,0.040238,0.040966,0.041456,0.041702,						  0.041702,0.041456,0.040966,0.040238,0.039280,0.038103,0.036719,0.035143,0.033394,0.031488,0.029448,0.027294,0.025049,0.022736,0.020379,0.018001,0.015626,0.013276,0.010973,						  0.008737,0.006589,0.004547,0.002625,0.000839,-0.000800,-0.002282,-0.003598,-0.004745,-0.005717,-0.006516,-0.007142,-0.007598,-0.007890,-0.008026,-0.008015,-0.007867,-0.007593,						  -0.007208,-0.006724,-0.006156,-0.005519,-0.004828,-0.004097,-0.003342,-0.002577,-0.001814,-0.001066,-0.000346,0.000336,0.000971,0.001551,0.002070,0.002521,0.002902,0.003210,						  0.003444,0.003604,0.003692,0.003710,0.003662,0.003553,0.003388,0.003174,0.002917,0.002623,0.002301,0.001958,0.001600,0.001236,0.000871,0.000513,0.000167,-0.000162,-0.000467,						  -0.000746,-0.000995,-0.001211,-0.001393,-0.001538,-0.001648,-0.001721,-0.001759,-0.001764,-0.001737,-0.001681,-0.001598,-0.001492,-0.001367,-0.001225,-0.001070,-0.000907,-0.000738,						  -0.000568,-0.000398,-0.000233,-0.000075,0.000073,0.000209,0.000333,0.000441,0.000534,0.000611,0.000670,0.000714,0.000741,0.000753,0.000750,0.000733,0.000705,0.000666,0.000618,0.000562,						  0.000500,0.000434,0.000365,0.000295,0.000225,0.000157,0.000091,0.000029,-0.000028,-0.000081,-0.000127,-0.000168,-0.000202,-0.000230,-0.000251,-0.000267,-0.000276,-0.000280,-0.000278,						  -0.000272,-0.000262,-0.000248,-0.000230-0.000211,-0.000188,-0.000165,-0.000140,-0.000114,-0.000089,-0.000063,-0.000037,-0.000012,0.000012,0.000035,0.000057,0.000077,0.000096,0.000112};

バッファと係数を代入してInitを呼びます。
係数がおおい・・・・・・・・・・

arm_fir_init_f32(&S3, 300, pCoeffs3, buffer3, 1);

APIコール

前回同様にin,outとブロック数を入れてやります。

arm_fir_f32(&S3, &in, &out, 1);

実際に動かしてみる。

FIRなのでかなり特性は急になる。
2kHzでほぼゼロwww
f:id:gsmcustomeffects:20171002072808p:plain

f:id:gsmcustomeffects:20171002072712p:plain

余談


タップ数が300にもなるので普通にやると48kHzサンプリングで破たんする。
f:id:gsmcustomeffects:20171002073035p:plain

そのためCubeでARTとデータキャッシュを有効にしてやる必要がある。

f:id:gsmcustomeffects:20171002073126p:plain

これについては別の記事でまた書きます。