FIR filter using DSP instructions for ST Nucleo F401RE.

Dependencies:   UITDSP_ADDA mbed

main.cpp

Committer:
MikamiUitOpen
Date:
2015-11-16
Revision:
8:d226de4c3951
Parent:
6:f005cc78b9aa

File content as of revision 8:d226de4c3951:

//--------------------------------------------------------------
// FIR フィルタ,基本的な構造, 固定小数点演算用 DSP 命令を使う
//      Analog Input : A0
//      Analog Output: MCP4922 using SPI
// 2015/11/16, Copyright (c) 2015 MIKAMI, Naoki
//--------------------------------------------------------------

#include "ADC_BuiltIn.hpp"      // for ADC not using interrupt
#include "DAC_MCP4921.hpp"      // for DAC MCP4921, MCP4922
#include "Coefficients_200_LPF_Fixed.hpp"

using namespace Mikami;

const int FS_ = 12000;          // Sampling frequency: 12 kHz
const uint16_t OFFSET = 2047;   // Correspond to "0"
ADC_BuiltIn adc_(A0, FS_);      // for AD
DAC_MCP4921 myDac_;             // for DA

int main()
{
    myDac_.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() - OFFSET;     // 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

        // rounding and devide by 32768 + DC offset
        uint16_t yOut = ((yn + 0x4000) >> 15) + OFFSET;
        //-----------------------------------------------
        myDac_.Write(yOut);         // Write to DAC
    }
}