![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IIR filter of direct form using double type operations and coefficients for ST Nucleo F401RE.
main.cpp@7:3889dd456ffa, 2014-11-20 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Nov 20 07:54:20 2014 +0000
- Revision:
- 7:3889dd456ffa
- Parent:
- 5:41b9b4f4e6c0
8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:3f57c7540719 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:3f57c7540719 | 2 | // IIR フィルタ,直接形,double で演算 |
MikamiUitOpen | 0:3f57c7540719 | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:3f57c7540719 | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 7:3889dd456ffa | 5 | // 2014/11/20, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:3f57c7540719 | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:3f57c7540719 | 7 | |
MikamiUitOpen | 0:3f57c7540719 | 8 | #include "mbed.h" |
MikamiUitOpen | 0:3f57c7540719 | 9 | |
MikamiUitOpen | 1:5016cbf30cd0 | 10 | #include "ADC_Base.hpp" // for ADC not using interrupt |
MikamiUitOpen | 0:3f57c7540719 | 11 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:3f57c7540719 | 12 | |
MikamiUitOpen | 0:3f57c7540719 | 13 | using namespace Mikami; |
MikamiUitOpen | 0:3f57c7540719 | 14 | |
MikamiUitOpen | 0:3f57c7540719 | 15 | const int ORDER_ = 6; // order |
MikamiUitOpen | 0:3f57c7540719 | 16 | // 低域通過フィルタ |
MikamiUitOpen | 0:3f57c7540719 | 17 | // 連立チェビシェフ特性 |
MikamiUitOpen | 0:3f57c7540719 | 18 | // 次数 :6 次 |
MikamiUitOpen | 0:3f57c7540719 | 19 | // 標本化周波数: 12.00 kHz |
MikamiUitOpen | 0:3f57c7540719 | 20 | // 遮断周波数 : 0.40 kHz |
MikamiUitOpen | 0:3f57c7540719 | 21 | // 通過域のリップル: 0.50 dB |
MikamiUitOpen | 0:3f57c7540719 | 22 | // 阻止域の減衰量 :40.00 dB |
MikamiUitOpen | 0:3f57c7540719 | 23 | const double am_[ORDER_] = { |
MikamiUitOpen | 0:3f57c7540719 | 24 | 5.684787115202E+00, -1.354501388127E+01, 1.731054054746E+01, |
MikamiUitOpen | 0:3f57c7540719 | 25 | -1.251297336508E+01, 4.850124049778E+00, -7.874837248679E-01}; |
MikamiUitOpen | 0:3f57c7540719 | 26 | const double bm_[ORDER_+1] = { |
MikamiUitOpen | 0:3f57c7540719 | 27 | 1.000028097647E-02, -5.466081078904E-02, 1.292315717890E-01, |
MikamiUitOpen | 0:3f57c7540719 | 28 | -1.691239024945E-01, 1.292315717890E-01, -5.466081078904E-02, |
MikamiUitOpen | 0:3f57c7540719 | 29 | 1.000028097647E-02}; |
MikamiUitOpen | 0:3f57c7540719 | 30 | |
MikamiUitOpen | 3:cda783626037 | 31 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 3:cda783626037 | 32 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 3:cda783626037 | 33 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:3f57c7540719 | 34 | |
MikamiUitOpen | 0:3f57c7540719 | 35 | int main() |
MikamiUitOpen | 0:3f57c7540719 | 36 | { |
MikamiUitOpen | 5:41b9b4f4e6c0 | 37 | myDac_.ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:3f57c7540719 | 38 | |
MikamiUitOpen | 7:3889dd456ffa | 39 | double un[ORDER_+1]; // Buffer corresponding to delays |
MikamiUitOpen | 7:3889dd456ffa | 40 | for (int k=0; k<=ORDER_; k++) un[k] = 0; |
MikamiUitOpen | 0:3f57c7540719 | 41 | |
MikamiUitOpen | 0:3f57c7540719 | 42 | while (true) |
MikamiUitOpen | 0:3f57c7540719 | 43 | { |
MikamiUitOpen | 0:3f57c7540719 | 44 | float xn = adc_.Read(); // Read from A0 |
MikamiUitOpen | 0:3f57c7540719 | 45 | //----------------------------------------------- |
MikamiUitOpen | 0:3f57c7540719 | 46 | |
MikamiUitOpen | 7:3889dd456ffa | 47 | un[0] = xn; |
MikamiUitOpen | 7:3889dd456ffa | 48 | for (int k=0; k<ORDER_; k++) un[0] = un[0] + am_[k]*un[k+1]; |
MikamiUitOpen | 7:3889dd456ffa | 49 | double yn = 0; |
MikamiUitOpen | 7:3889dd456ffa | 50 | for (int k=0; k<=ORDER_; k++) yn = yn + bm_[k]*un[k]; |
MikamiUitOpen | 0:3f57c7540719 | 51 | |
MikamiUitOpen | 0:3f57c7540719 | 52 | // 計算結果の移動 |
MikamiUitOpen | 7:3889dd456ffa | 53 | for (int k=ORDER_; k>0; k--) un[k] = un[k-1]; |
MikamiUitOpen | 0:3f57c7540719 | 54 | |
MikamiUitOpen | 0:3f57c7540719 | 55 | //----------------------------------------------- |
MikamiUitOpen | 0:3f57c7540719 | 56 | myDac_.Write((float)yn); // Write to DAC |
MikamiUitOpen | 0:3f57c7540719 | 57 | } |
MikamiUitOpen | 0:3f57c7540719 | 58 | } |
MikamiUitOpen | 0:3f57c7540719 | 59 |