![](/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@4:83e5419d22bb, 2014-10-23 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Oct 23 00:25:44 2014 +0000
- Revision:
- 4:83e5419d22bb
- Parent:
- 2:d4999d914eee
- Child:
- 6:9b78c8c6ed27
5
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 | 4:83e5419d22bb | 5 | // 2014/10/23, 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 | #include "ScfClockTim3.hpp" // for clock supplied to SCF |
MikamiUitOpen | 0:8c15b3d0523e | 13 | |
MikamiUitOpen | 0:8c15b3d0523e | 14 | using namespace Mikami; |
MikamiUitOpen | 0:8c15b3d0523e | 15 | |
MikamiUitOpen | 0:8c15b3d0523e | 16 | const int ORDER_ = 6; // order |
MikamiUitOpen | 0:8c15b3d0523e | 17 | struct Un { float u1, u2; }; |
MikamiUitOpen | 0:8c15b3d0523e | 18 | struct Coefs { float a1, a2, b1, b2; }; |
MikamiUitOpen | 0:8c15b3d0523e | 19 | |
MikamiUitOpen | 0:8c15b3d0523e | 20 | // 低域通過フィルタ |
MikamiUitOpen | 0:8c15b3d0523e | 21 | // 連立チェビシェフ特性 |
MikamiUitOpen | 0:8c15b3d0523e | 22 | // 次数 :6 次 |
MikamiUitOpen | 0:8c15b3d0523e | 23 | // 標本化周波数: 12.00 kHz |
MikamiUitOpen | 0:8c15b3d0523e | 24 | // 遮断周波数 : 0.40 kHz |
MikamiUitOpen | 0:8c15b3d0523e | 25 | // 通過域のリップル: 0.50 dB |
MikamiUitOpen | 0:8c15b3d0523e | 26 | // 阻止域の減衰量 :40.00 dB |
MikamiUitOpen | 0:8c15b3d0523e | 27 | const Coefs ck_[ORDER_/2] = { |
MikamiUitOpen | 0:8c15b3d0523e | 28 | { 1.837790E+00f, -8.507293E-01f, -1.603320E+00f, 1.000000E+00f}, // 1段目 |
MikamiUitOpen | 0:8c15b3d0523e | 29 | { 1.904445E+00f, -9.382561E-01f, -1.919647E+00f, 1.000000E+00f}, // 2段目 |
MikamiUitOpen | 0:8c15b3d0523e | 30 | { 1.942552E+00f, -9.865720E-01f, -1.942961E+00f, 1.000000E+00f}}; // 3段目 |
MikamiUitOpen | 0:8c15b3d0523e | 31 | const float g0_ = 1.000028E-02f; // 利得定数 |
MikamiUitOpen | 0:8c15b3d0523e | 32 | |
MikamiUitOpen | 4:83e5419d22bb | 33 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 4:83e5419d22bb | 34 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 4:83e5419d22bb | 35 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:8c15b3d0523e | 36 | |
MikamiUitOpen | 0:8c15b3d0523e | 37 | int main() |
MikamiUitOpen | 0:8c15b3d0523e | 38 | { |
MikamiUitOpen | 0:8c15b3d0523e | 39 | ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:8c15b3d0523e | 40 | |
MikamiUitOpen | 1:b688b41d0ba1 | 41 | Un uk[ORDER_/2]; // 過去の計算結果を格納する配列 |
MikamiUitOpen | 0:8c15b3d0523e | 42 | // 過去の入力信号が格納される配列をクリア |
MikamiUitOpen | 0:8c15b3d0523e | 43 | for (int k=0; k<ORDER_/2; k++) |
MikamiUitOpen | 0:8c15b3d0523e | 44 | { |
MikamiUitOpen | 0:8c15b3d0523e | 45 | uk[k].u1 = 0; |
MikamiUitOpen | 0:8c15b3d0523e | 46 | uk[k].u2 = 0; |
MikamiUitOpen | 0:8c15b3d0523e | 47 | } |
MikamiUitOpen | 0:8c15b3d0523e | 48 | |
MikamiUitOpen | 0:8c15b3d0523e | 49 | while (true) |
MikamiUitOpen | 0:8c15b3d0523e | 50 | { |
MikamiUitOpen | 0:8c15b3d0523e | 51 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:8c15b3d0523e | 52 | //----------------------------------------------- |
MikamiUitOpen | 0:8c15b3d0523e | 53 | |
MikamiUitOpen | 2:d4999d914eee | 54 | float yn = g0_*xn; |
MikamiUitOpen | 0:8c15b3d0523e | 55 | for (int k=0; k<ORDER_/2; k++) |
MikamiUitOpen | 0:8c15b3d0523e | 56 | { |
MikamiUitOpen | 2:d4999d914eee | 57 | float un = ck_[k].a1*uk[k].u1 + ck_[k].a2*uk[k].u2 + yn; |
MikamiUitOpen | 2:d4999d914eee | 58 | yn = un + ck_[k].b1*uk[k].u1 + + ck_[k].b2*uk[k].u2; |
MikamiUitOpen | 0:8c15b3d0523e | 59 | // 計算結果の移動 |
MikamiUitOpen | 0:8c15b3d0523e | 60 | uk[k].u2 = uk[k].u1; |
MikamiUitOpen | 0:8c15b3d0523e | 61 | uk[k].u1 = un; |
MikamiUitOpen | 0:8c15b3d0523e | 62 | } |
MikamiUitOpen | 0:8c15b3d0523e | 63 | |
MikamiUitOpen | 0:8c15b3d0523e | 64 | //----------------------------------------------- |
MikamiUitOpen | 2:d4999d914eee | 65 | myDac_.Write(yn); // Write to DAC |
MikamiUitOpen | 0:8c15b3d0523e | 66 | } |
MikamiUitOpen | 0:8c15b3d0523e | 67 | } |
MikamiUitOpen | 0:8c15b3d0523e | 68 | |
MikamiUitOpen | 0:8c15b3d0523e | 69 |