FIR filter using DSP instructions for ST Nucleo F401RE.
Dependencies: UITDSP_ADDA mbed
main.cpp@4:0b3b70b0af0b, 2014-10-30 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Oct 30 02:56:50 2014 +0000
- Revision:
- 4:0b3b70b0af0b
- Parent:
- 3:bfb6d1e6deee
- Child:
- 6:f005cc78b9aa
5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:797a9c10261e | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 2 | // FIR フィルタ,基本的な構造, 固定小数点演算用 DSP 命令を使う |
MikamiUitOpen | 0:797a9c10261e | 3 | // Analog Input : A0 |
MikamiUitOpen | 0:797a9c10261e | 4 | // Analog Output: MCP4922 using SPI |
MikamiUitOpen | 4:0b3b70b0af0b | 5 | // 2014/10/29, Copyright (c) 2014 MIKAMI, Naoki |
MikamiUitOpen | 0:797a9c10261e | 6 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 7 | |
MikamiUitOpen | 0:797a9c10261e | 8 | #include "mbed.h" |
MikamiUitOpen | 0:797a9c10261e | 9 | |
MikamiUitOpen | 0:797a9c10261e | 10 | #include "ADC_Base.hpp" // for ADC not using interrupt |
MikamiUitOpen | 0:797a9c10261e | 11 | #include "DAC_MCP4922.hpp" // for DAC MCP4922 |
MikamiUitOpen | 0:797a9c10261e | 12 | #include "ScfClockTim3.hpp" // for clock supplied to SCF |
MikamiUitOpen | 0:797a9c10261e | 13 | #include "Coefficients_200_LPF_Fixed.hpp" |
MikamiUitOpen | 0:797a9c10261e | 14 | |
MikamiUitOpen | 0:797a9c10261e | 15 | using namespace Mikami; |
MikamiUitOpen | 0:797a9c10261e | 16 | |
MikamiUitOpen | 3:bfb6d1e6deee | 17 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 4:0b3b70b0af0b | 18 | const uint16_t OFFSET = 2047; // Correspond to "0" |
MikamiUitOpen | 3:bfb6d1e6deee | 19 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 3:bfb6d1e6deee | 20 | DAC_MCP4922 myDac_; // for DA |
MikamiUitOpen | 0:797a9c10261e | 21 | |
MikamiUitOpen | 0:797a9c10261e | 22 | int main() |
MikamiUitOpen | 0:797a9c10261e | 23 | { |
MikamiUitOpen | 0:797a9c10261e | 24 | ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:797a9c10261e | 25 | |
MikamiUitOpen | 0:797a9c10261e | 26 | int16_t xn[ORDER_+1]; |
MikamiUitOpen | 0:797a9c10261e | 27 | for (int n=0; n<=ORDER_; n++) |
MikamiUitOpen | 0:797a9c10261e | 28 | xn[n] = 0; |
MikamiUitOpen | 0:797a9c10261e | 29 | |
MikamiUitOpen | 0:797a9c10261e | 30 | while (true) |
MikamiUitOpen | 0:797a9c10261e | 31 | { |
MikamiUitOpen | 4:0b3b70b0af0b | 32 | xn[0] = adc_.Read_u16() - OFFSET; // Read from A0 |
MikamiUitOpen | 0:797a9c10261e | 33 | //----------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 34 | |
MikamiUitOpen | 0:797a9c10261e | 35 | int32_t yn = hm_[ORDER_]*xn[ORDER_];; |
MikamiUitOpen | 0:797a9c10261e | 36 | for (int k=0; k<ORDER_; k+=2) |
MikamiUitOpen | 0:797a9c10261e | 37 | { |
MikamiUitOpen | 0:797a9c10261e | 38 | uint32_t x = __PKHBT(xn[k], xn[k+1], 16); |
MikamiUitOpen | 0:797a9c10261e | 39 | uint32_t h = __PKHBT(hm_[k], hm_[k+1], 16); |
MikamiUitOpen | 0:797a9c10261e | 40 | yn = __SMLAD(x, h, yn); // Dual multiply and accumulate |
MikamiUitOpen | 0:797a9c10261e | 41 | } |
MikamiUitOpen | 0:797a9c10261e | 42 | for (int k=ORDER_; k>0; k--) |
MikamiUitOpen | 0:797a9c10261e | 43 | xn[k] = xn[k-1]; // move input signals |
MikamiUitOpen | 0:797a9c10261e | 44 | |
MikamiUitOpen | 4:0b3b70b0af0b | 45 | // rounding and devide by 32768 + DC offset |
MikamiUitOpen | 4:0b3b70b0af0b | 46 | uint16_t yOut = ((yn + 0x8000) >> 15) + OFFSET; |
MikamiUitOpen | 0:797a9c10261e | 47 | //----------------------------------------------- |
MikamiUitOpen | 4:0b3b70b0af0b | 48 | myDac_.Write(yOut); // Write to DAC |
MikamiUitOpen | 0:797a9c10261e | 49 | } |
MikamiUitOpen | 0:797a9c10261e | 50 | } |