FIR filter using DSP instructions for ST Nucleo F401RE.
Dependencies: UITDSP_ADDA mbed
main.cpp@1:a2ca73edd772, 2014-10-22 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Wed Oct 22 05:17:24 2014 +0000
- Revision:
- 1:a2ca73edd772
- Parent:
- 0:797a9c10261e
- Child:
- 3:bfb6d1e6deee
2
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 | 1:a2ca73edd772 | 5 | // 2014/10/22, 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 | 0:797a9c10261e | 17 | const int FS_ = 12000; // Sampling frequency: 12 kHz |
MikamiUitOpen | 0:797a9c10261e | 18 | ADC_Base adc_(A0, FS_); // for AD |
MikamiUitOpen | 0:797a9c10261e | 19 | DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A); // for DA |
MikamiUitOpen | 0:797a9c10261e | 20 | |
MikamiUitOpen | 0:797a9c10261e | 21 | int main() |
MikamiUitOpen | 0:797a9c10261e | 22 | { |
MikamiUitOpen | 0:797a9c10261e | 23 | ScfClockTim3(500000); // cutoff frequency: 5 kHz |
MikamiUitOpen | 0:797a9c10261e | 24 | |
MikamiUitOpen | 0:797a9c10261e | 25 | int16_t xn[ORDER_+1]; |
MikamiUitOpen | 0:797a9c10261e | 26 | for (int n=0; n<=ORDER_; n++) |
MikamiUitOpen | 0:797a9c10261e | 27 | xn[n] = 0; |
MikamiUitOpen | 0:797a9c10261e | 28 | |
MikamiUitOpen | 0:797a9c10261e | 29 | while (true) |
MikamiUitOpen | 0:797a9c10261e | 30 | { |
MikamiUitOpen | 0:797a9c10261e | 31 | xn[0] = adc_.Read_u16() - 2047; // Read from A0 |
MikamiUitOpen | 0:797a9c10261e | 32 | //----------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 33 | |
MikamiUitOpen | 0:797a9c10261e | 34 | int32_t yn = hm_[ORDER_]*xn[ORDER_];; |
MikamiUitOpen | 0:797a9c10261e | 35 | for (int k=0; k<ORDER_; k+=2) |
MikamiUitOpen | 0:797a9c10261e | 36 | { |
MikamiUitOpen | 0:797a9c10261e | 37 | uint32_t x = __PKHBT(xn[k], xn[k+1], 16); |
MikamiUitOpen | 0:797a9c10261e | 38 | uint32_t h = __PKHBT(hm_[k], hm_[k+1], 16); |
MikamiUitOpen | 0:797a9c10261e | 39 | yn = __SMLAD(x, h, yn); // Dual multiply and accumulate |
MikamiUitOpen | 0:797a9c10261e | 40 | } |
MikamiUitOpen | 0:797a9c10261e | 41 | for (int k=ORDER_; k>0; k--) |
MikamiUitOpen | 0:797a9c10261e | 42 | xn[k] = xn[k-1]; // move input signals |
MikamiUitOpen | 0:797a9c10261e | 43 | |
MikamiUitOpen | 1:a2ca73edd772 | 44 | yn = (yn + 0x8000) >> 15; // rounding and devide by 32768 |
MikamiUitOpen | 0:797a9c10261e | 45 | //----------------------------------------------- |
MikamiUitOpen | 0:797a9c10261e | 46 | myDac_.Write((uint16_t)(yn + 2047)); // Write to DAC |
MikamiUitOpen | 0:797a9c10261e | 47 | } |
MikamiUitOpen | 0:797a9c10261e | 48 | } |