![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
FIR filter using fixed point arithmetic operations for ST Nucleo F401RE.
Dependencies: UITDSP_ADDA mbed
main.cpp@8:abd11816e480, 2015-11-16 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Nov 16 02:23:51 2015 +0000
- Revision:
- 8:abd11816e480
- Parent:
- 6:bb0e5ef9befe
9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:da34e90f15a3 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:da34e90f15a3 | 2 | // FIR フィルタ,基本的な構造, 固定小数点演算を使う |
MikamiUitOpen | 0:da34e90f15a3 | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:da34e90f15a3 | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 8:abd11816e480 | 5 | // 2015/11/16, Copyright (c) 2015 MIKAMI, Naoki |
MikamiUitOpen | 0:da34e90f15a3 | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:da34e90f15a3 | 7 | |
MikamiUitOpen | 8:abd11816e480 | 8 | #include "ADC_BuiltIn.hpp" // for ADC not using interrupt |
MikamiUitOpen | 8:abd11816e480 | 9 | #include "DAC_MCP4921.hpp" // for DAC MCP4921, MCP4922 |
MikamiUitOpen | 0:da34e90f15a3 | 10 | #include "Coefficients_200_LPF_Fixed.hpp" |
MikamiUitOpen | 0:da34e90f15a3 | 11 | |
MikamiUitOpen | 0:da34e90f15a3 | 12 | using namespace Mikami; |
MikamiUitOpen | 0:da34e90f15a3 | 13 | |
MikamiUitOpen | 0:da34e90f15a3 | 14 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 4:a4b03c9a371c | 15 | const uint16_t OFFSET = 2047; // Correspond to "0" |
MikamiUitOpen | 8:abd11816e480 | 16 | ADC_BuiltIn adc_(A0, FS_); // for AD |
MikamiUitOpen | 8:abd11816e480 | 17 | DAC_MCP4921 myDac_; // for DA |
MikamiUitOpen | 0:da34e90f15a3 | 18 | |
MikamiUitOpen | 0:da34e90f15a3 | 19 | int main() |
MikamiUitOpen | 0:da34e90f15a3 | 20 | { |
MikamiUitOpen | 6:bb0e5ef9befe | 21 | myDac_.ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:da34e90f15a3 | 22 | |
MikamiUitOpen | 0:da34e90f15a3 | 23 | int16_t xn[ORDER_+1]; |
MikamiUitOpen | 0:da34e90f15a3 | 24 | for (int n=0; n<=ORDER_; n++) |
MikamiUitOpen | 0:da34e90f15a3 | 25 | xn[n] = 0; |
MikamiUitOpen | 0:da34e90f15a3 | 26 | |
MikamiUitOpen | 0:da34e90f15a3 | 27 | while (true) |
MikamiUitOpen | 0:da34e90f15a3 | 28 | { |
MikamiUitOpen | 4:a4b03c9a371c | 29 | xn[0] = adc_.Read_u16() - OFFSET; // Read from A0 |
MikamiUitOpen | 0:da34e90f15a3 | 30 | //----------------------------------------------- |
MikamiUitOpen | 0:da34e90f15a3 | 31 | |
MikamiUitOpen | 0:da34e90f15a3 | 32 | int32_t yn = 0; |
MikamiUitOpen | 0:da34e90f15a3 | 33 | for (int k=0; k<=ORDER_; k++) |
MikamiUitOpen | 0:da34e90f15a3 | 34 | yn = yn + hm_[k]*xn[k]; |
MikamiUitOpen | 0:da34e90f15a3 | 35 | |
MikamiUitOpen | 0:da34e90f15a3 | 36 | for (int k=ORDER_; k>0; k--) |
MikamiUitOpen | 0:da34e90f15a3 | 37 | xn[k] = xn[k-1]; // move input signals |
MikamiUitOpen | 0:da34e90f15a3 | 38 | |
MikamiUitOpen | 4:a4b03c9a371c | 39 | // rounding and devide by 32768 + DC offset |
MikamiUitOpen | 8:abd11816e480 | 40 | uint16_t yOut = ((yn + 0x4000) >> 15) + OFFSET; |
MikamiUitOpen | 0:da34e90f15a3 | 41 | //----------------------------------------------- |
MikamiUitOpen | 4:a4b03c9a371c | 42 | myDac_.Write(yOut); // Write to DAC |
MikamiUitOpen | 0:da34e90f15a3 | 43 | } |
MikamiUitOpen | 0:da34e90f15a3 | 44 | } |