IIR filter of cascade form using Biquad class for ST Nucleo F401RE.

Dependencies:   UITDSP_ADDA mbed

Committer:
MikamiUitOpen
Date:
Thu Oct 23 00:27:39 2014 +0000
Revision:
3:8f384ccbbe66
Parent:
1:ec14a181cab8
Child:
5:14bb865c7eee
4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 0:24c904911225 1 //--------------------------------------------------------------
MikamiUitOpen 0:24c904911225 2 // IIR フィルタ,縦続形,float で演算, Biquad クラス利用
MikamiUitOpen 0:24c904911225 3 // Analog Input : A0
MikamiUitOpen 0:24c904911225 4 // Analog Output: MCP4922 using SPI
MikamiUitOpen 3:8f384ccbbe66 5 // 2014/10/23, Copyright (c) 2014 MIKAMI, Naoki
MikamiUitOpen 0:24c904911225 6 //--------------------------------------------------------------
MikamiUitOpen 0:24c904911225 7
MikamiUitOpen 0:24c904911225 8 #include "mbed.h"
MikamiUitOpen 0:24c904911225 9
MikamiUitOpen 0:24c904911225 10 #include "ADC_Base.hpp" // for ADC not using interrupt
MikamiUitOpen 0:24c904911225 11 #include "DAC_MCP4922.hpp" // for DAC MCP4922
MikamiUitOpen 0:24c904911225 12 #include "ScfClockTim3.hpp" // for clock supplied to SCF
MikamiUitOpen 0:24c904911225 13 #include "Biquad.hpp" // 2nd-ordered IIR filter
MikamiUitOpen 0:24c904911225 14
MikamiUitOpen 0:24c904911225 15 using namespace Mikami;
MikamiUitOpen 0:24c904911225 16
MikamiUitOpen 0:24c904911225 17 const int ORDER_ = 6; // order
MikamiUitOpen 0:24c904911225 18
MikamiUitOpen 0:24c904911225 19 // 低域通過フィルタ
MikamiUitOpen 0:24c904911225 20 // 連立チェビシェフ特性
MikamiUitOpen 0:24c904911225 21 // 次数    :6 次
MikamiUitOpen 0:24c904911225 22 // 標本化周波数: 12.00 kHz
MikamiUitOpen 0:24c904911225 23 // 遮断周波数 : 0.40 kHz
MikamiUitOpen 0:24c904911225 24 // 通過域のリップル: 0.50 dB
MikamiUitOpen 0:24c904911225 25 // 阻止域の減衰量 :40.00 dB
MikamiUitOpen 0:24c904911225 26 const Biquad::Coefs ck_[ORDER_/2] = {
MikamiUitOpen 1:ec14a181cab8 27 { 1.837790E+00f, -8.507293E-01f, -1.603320E+00f, 1.0E+00f}, // 1段目
MikamiUitOpen 1:ec14a181cab8 28 { 1.904445E+00f, -9.382561E-01f, -1.919647E+00f, 1.0E+00f}, // 2段目
MikamiUitOpen 1:ec14a181cab8 29 { 1.942552E+00f, -9.865720E-01f, -1.942961E+00f, 1.0E+00f}}; // 3段目
MikamiUitOpen 0:24c904911225 30 const float g0_ = 1.000028E-02f; // 利得定数
MikamiUitOpen 0:24c904911225 31
MikamiUitOpen 3:8f384ccbbe66 32 const int FS_ = 12000; // Sampling frequency: 12 kHz
MikamiUitOpen 3:8f384ccbbe66 33 ADC_Base adc_(A0, FS_); // for AD
MikamiUitOpen 3:8f384ccbbe66 34 DAC_MCP4922 myDac_; // for DA
MikamiUitOpen 0:24c904911225 35
MikamiUitOpen 0:24c904911225 36 int main()
MikamiUitOpen 0:24c904911225 37 {
MikamiUitOpen 0:24c904911225 38 ScfClockTim3(500000); // cutoff frequency: 5 kHz
MikamiUitOpen 0:24c904911225 39
MikamiUitOpen 0:24c904911225 40 Biquad hn[ORDER_/2];
MikamiUitOpen 0:24c904911225 41 for (int k=0; k<ORDER_/2; k++)
MikamiUitOpen 0:24c904911225 42 hn[k] = Biquad(ck_[k]);
MikamiUitOpen 0:24c904911225 43
MikamiUitOpen 0:24c904911225 44 while (true)
MikamiUitOpen 0:24c904911225 45 {
MikamiUitOpen 0:24c904911225 46 float xn = adc_.Read(); // Read from A0
MikamiUitOpen 0:24c904911225 47 //-----------------------------------------------
MikamiUitOpen 0:24c904911225 48
MikamiUitOpen 0:24c904911225 49 float yn = g0_*xn;
MikamiUitOpen 0:24c904911225 50 for (int k=0; k<ORDER_/2; k++)
MikamiUitOpen 0:24c904911225 51 yn = hn[k].Execute(yn);
MikamiUitOpen 0:24c904911225 52
MikamiUitOpen 0:24c904911225 53 //-----------------------------------------------
MikamiUitOpen 0:24c904911225 54 myDac_.Write(yn); // Write to DAC
MikamiUitOpen 0:24c904911225 55 }
MikamiUitOpen 0:24c904911225 56 }
MikamiUitOpen 0:24c904911225 57
MikamiUitOpen 0:24c904911225 58