FIR filter using DSP instructions for ST Nucleo F401RE.
Dependencies: UITDSP_ADDA mbed
Diff: main.cpp
- Revision:
- 0:797a9c10261e
- Child:
- 1:a2ca73edd772
diff -r 000000000000 -r 797a9c10261e main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Oct 22 02:11:49 2014 +0000 @@ -0,0 +1,48 @@ +//-------------------------------------------------------------- +// FIR フィルタ,基本的な構造, 固定小数点演算用 DSP 命令を使う +// Analog Input : A0 +// Analog Output: MCP4922 using SPI +// 2014/10/21, Copyright (c) 2014 MIKAMI, Naoki +//-------------------------------------------------------------- + +#include "mbed.h" + +#include "ADC_Base.hpp" // for ADC not using interrupt +#include "DAC_MCP4922.hpp" // for DAC MCP4922 +#include "ScfClockTim3.hpp" // for clock supplied to SCF +#include "Coefficients_200_LPF_Fixed.hpp" + +using namespace Mikami; + +const int FS_ = 12000; // Sampling frequency: 12 kHz +ADC_Base adc_(A0, FS_); // for AD +DAC_MCP4922 myDac_(DAC_MCP4922::DAC_A); // for DA + +int main() +{ + ScfClockTim3(500000); // cutoff frequency: 5 kHz + + int16_t xn[ORDER_+1]; + for (int n=0; n<=ORDER_; n++) + xn[n] = 0; + + while (true) + { + xn[0] = adc_.Read_u16() - 2047; // Read from A0 + //----------------------------------------------- + + int32_t yn = hm_[ORDER_]*xn[ORDER_];; + for (int k=0; k<ORDER_; k+=2) + { + uint32_t x = __PKHBT(xn[k], xn[k+1], 16); + uint32_t h = __PKHBT(hm_[k], hm_[k+1], 16); + yn = __SMLAD(x, h, yn); // Dual multiply and accumulate + } + for (int k=ORDER_; k>0; k--) + xn[k] = xn[k-1]; // move input signals + + yn = (yn + 0x8000) >> 16; // rounding and devide by 32768 + //----------------------------------------------- + myDac_.Write((uint16_t)(yn + 2047)); // Write to DAC + } +}