FIR filter using fixed point arithmetic operations for ST Nucleo F401RE.
Dependencies: UITDSP_ADDA mbed
main.cpp@3:5c085430972b, 2014-10-23 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Oct 23 00:17:58 2014 +0000
- Revision:
- 3:5c085430972b
- Parent:
- 1:152ee39babce
- Child:
- 4:a4b03c9a371c
4
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 | 3:5c085430972b | 5 | // 2014/10/23, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:da34e90f15a3 | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:da34e90f15a3 | 7 | |
MikamiUitOpen | 0:da34e90f15a3 | 8 | #include "mbed.h" |
MikamiUitOpen | 0:da34e90f15a3 | 9 | |
MikamiUitOpen | 0:da34e90f15a3 | 10 | #include "ADC_Base.hpp" // for ADC not using interrupt |
MikamiUitOpen | 0:da34e90f15a3 | 11 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:da34e90f15a3 | 12 | #include "ScfClockTim3.hpp" // for clock supplied to SCF |
MikamiUitOpen | 0:da34e90f15a3 | 13 | #include "Coefficients_200_LPF_Fixed.hpp" |
MikamiUitOpen | 0:da34e90f15a3 | 14 | |
MikamiUitOpen | 0:da34e90f15a3 | 15 | using namespace Mikami; |
MikamiUitOpen | 0:da34e90f15a3 | 16 | |
MikamiUitOpen | 0:da34e90f15a3 | 17 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 0:da34e90f15a3 | 18 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 0:da34e90f15a3 | 19 | DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A); // for DA |
MikamiUitOpen | 0:da34e90f15a3 | 20 | |
MikamiUitOpen | 0:da34e90f15a3 | 21 | int main() |
MikamiUitOpen | 0:da34e90f15a3 | 22 | { |
MikamiUitOpen | 0:da34e90f15a3 | 23 | ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:da34e90f15a3 | 24 | |
MikamiUitOpen | 0:da34e90f15a3 | 25 | int16_t xn[ORDER_+1]; |
MikamiUitOpen | 0:da34e90f15a3 | 26 | for (int n=0; n<=ORDER_; n++) |
MikamiUitOpen | 0:da34e90f15a3 | 27 | xn[n] = 0; |
MikamiUitOpen | 0:da34e90f15a3 | 28 | |
MikamiUitOpen | 0:da34e90f15a3 | 29 | while (true) |
MikamiUitOpen | 0:da34e90f15a3 | 30 | { |
MikamiUitOpen | 0:da34e90f15a3 | 31 | xn[0] = adc_.Read_u16() - 2047; // Read from A0 |
MikamiUitOpen | 0:da34e90f15a3 | 32 | //----------------------------------------------- |
MikamiUitOpen | 0:da34e90f15a3 | 33 | |
MikamiUitOpen | 0:da34e90f15a3 | 34 | int32_t yn = 0; |
MikamiUitOpen | 0:da34e90f15a3 | 35 | for (int k=0; k<=ORDER_; k++) |
MikamiUitOpen | 0:da34e90f15a3 | 36 | yn = yn + hm_[k]*xn[k]; |
MikamiUitOpen | 0:da34e90f15a3 | 37 | |
MikamiUitOpen | 0:da34e90f15a3 | 38 | for (int k=ORDER_; k>0; k--) |
MikamiUitOpen | 0:da34e90f15a3 | 39 | xn[k] = xn[k-1]; // move input signals |
MikamiUitOpen | 0:da34e90f15a3 | 40 | |
MikamiUitOpen | 1:152ee39babce | 41 | yn = (yn + 0x8000) >> 15; // rounding and devide by 32768 |
MikamiUitOpen | 0:da34e90f15a3 | 42 | //----------------------------------------------- |
MikamiUitOpen | 0:da34e90f15a3 | 43 | myDac_.Write((uint16_t)(yn + 2047)); // Write to DAC |
MikamiUitOpen | 0:da34e90f15a3 | 44 | } |
MikamiUitOpen | 0:da34e90f15a3 | 45 | } |