![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IIR filter of cascade form using float type operations and coefficients for ST Nucleo F401RE.
main.cpp@8:74c678577bb0, 2014-11-20 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Nov 20 07:21:07 2014 +0000
- Revision:
- 8:74c678577bb0
- Parent:
- 6:9b78c8c6ed27
9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:8c15b3d0523e | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 2:d4999d914eee | 2 | // IIR フィルタ,縦続形,float で演算 |
MikamiUitOpen | 0:8c15b3d0523e | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:8c15b3d0523e | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 8:74c678577bb0 | 5 | // 2014/11/20, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:8c15b3d0523e | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:8c15b3d0523e | 7 | |
MikamiUitOpen | 0:8c15b3d0523e | 8 | #include "mbed.h" |
MikamiUitOpen | 0:8c15b3d0523e | 9 | |
MikamiUitOpen | 2:d4999d914eee | 10 | #include "ADC_Base.hpp" // for ADC not using interrupt |
MikamiUitOpen | 0:8c15b3d0523e | 11 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:8c15b3d0523e | 12 | |
MikamiUitOpen | 0:8c15b3d0523e | 13 | using namespace Mikami; |
MikamiUitOpen | 0:8c15b3d0523e | 14 | |
MikamiUitOpen | 0:8c15b3d0523e | 15 | const int ORDER_ = 6; // order |
MikamiUitOpen | 0:8c15b3d0523e | 16 | struct Un { float u1, u2; }; |
MikamiUitOpen | 0:8c15b3d0523e | 17 | struct Coefs { float a1, a2, b1, b2; }; |
MikamiUitOpen | 0:8c15b3d0523e | 18 | |
MikamiUitOpen | 0:8c15b3d0523e | 19 | // 低域通過フィルタ |
MikamiUitOpen | 0:8c15b3d0523e | 20 | // 連立チェビシェフ特性 |
MikamiUitOpen | 0:8c15b3d0523e | 21 | // 次数 :6 次 |
MikamiUitOpen | 0:8c15b3d0523e | 22 | // 標本化周波数: 12.00 kHz |
MikamiUitOpen | 0:8c15b3d0523e | 23 | // 遮断周波数 : 0.40 kHz |
MikamiUitOpen | 0:8c15b3d0523e | 24 | // 通過域のリップル: 0.50 dB |
MikamiUitOpen | 0:8c15b3d0523e | 25 | // 阻止域の減衰量 :40.00 dB |
MikamiUitOpen | 0:8c15b3d0523e | 26 | const Coefs ck_[ORDER_/2] = { |
MikamiUitOpen | 0:8c15b3d0523e | 27 | { 1.837790E+00f, -8.507293E-01f, -1.603320E+00f, 1.000000E+00f}, // 1段目 |
MikamiUitOpen | 0:8c15b3d0523e | 28 | { 1.904445E+00f, -9.382561E-01f, -1.919647E+00f, 1.000000E+00f}, // 2段目 |
MikamiUitOpen | 0:8c15b3d0523e | 29 | { 1.942552E+00f, -9.865720E-01f, -1.942961E+00f, 1.000000E+00f}}; // 3段目 |
MikamiUitOpen | 0:8c15b3d0523e | 30 | const float g0_ = 1.000028E-02f; // 利得定数 |
MikamiUitOpen | 0:8c15b3d0523e | 31 | |
MikamiUitOpen | 4:83e5419d22bb | 32 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 4:83e5419d22bb | 33 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 4:83e5419d22bb | 34 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:8c15b3d0523e | 35 | |
MikamiUitOpen | 0:8c15b3d0523e | 36 | int main() |
MikamiUitOpen | 0:8c15b3d0523e | 37 | { |
MikamiUitOpen | 6:9b78c8c6ed27 | 38 | myDac_.ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:8c15b3d0523e | 39 | |
MikamiUitOpen | 8:74c678577bb0 | 40 | Un uk[ORDER_/2]; // 過去の計算結果を格納する配列 |
MikamiUitOpen | 0:8c15b3d0523e | 41 | // 過去の入力信号が格納される配列をクリア |
MikamiUitOpen | 0:8c15b3d0523e | 42 | for (int k=0; k<ORDER_/2; k++) |
MikamiUitOpen | 0:8c15b3d0523e | 43 | { |
MikamiUitOpen | 0:8c15b3d0523e | 44 | uk[k].u1 = 0; |
MikamiUitOpen | 0:8c15b3d0523e | 45 | uk[k].u2 = 0; |
MikamiUitOpen | 0:8c15b3d0523e | 46 | } |
MikamiUitOpen | 0:8c15b3d0523e | 47 | |
MikamiUitOpen | 0:8c15b3d0523e | 48 | while (true) |
MikamiUitOpen | 0:8c15b3d0523e | 49 | { |
MikamiUitOpen | 0:8c15b3d0523e | 50 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:8c15b3d0523e | 51 | //----------------------------------------------- |
MikamiUitOpen | 0:8c15b3d0523e | 52 | |
MikamiUitOpen | 2:d4999d914eee | 53 | float yn = g0_*xn; |
MikamiUitOpen | 0:8c15b3d0523e | 54 | for (int k=0; k<ORDER_/2; k++) |
MikamiUitOpen | 0:8c15b3d0523e | 55 | { |
MikamiUitOpen | 2:d4999d914eee | 56 | float un = ck_[k].a1*uk[k].u1 + ck_[k].a2*uk[k].u2 + yn; |
MikamiUitOpen | 8:74c678577bb0 | 57 | yn = un + ck_[k].b1*uk[k].u1 + ck_[k].b2*uk[k].u2; |
MikamiUitOpen | 0:8c15b3d0523e | 58 | // 計算結果の移動 |
MikamiUitOpen | 0:8c15b3d0523e | 59 | uk[k].u2 = uk[k].u1; |
MikamiUitOpen | 0:8c15b3d0523e | 60 | uk[k].u1 = un; |
MikamiUitOpen | 0:8c15b3d0523e | 61 | } |
MikamiUitOpen | 0:8c15b3d0523e | 62 | |
MikamiUitOpen | 0:8c15b3d0523e | 63 | //----------------------------------------------- |
MikamiUitOpen | 2:d4999d914eee | 64 | myDac_.Write(yn); // Write to DAC |
MikamiUitOpen | 0:8c15b3d0523e | 65 | } |
MikamiUitOpen | 0:8c15b3d0523e | 66 | } |
MikamiUitOpen | 0:8c15b3d0523e | 67 | |
MikamiUitOpen | 0:8c15b3d0523e | 68 |