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