Example FIR filter using the CMSIS DSP API

Dependencies:   mbed mbed-dsp

main.cpp

Committer:
emilmont
Date:
2012-03-12
Revision:
0:840b7af4eace
Child:
1:9442dff3bfdc

File content as of revision 0:840b7af4eace:

#include <stdio.h>
#include "arm_math.h" 
#include "math_helper.h" 

#define BLOCK_SIZE          32
#define NUM_BLOCKS          10
#define TEST_LENGTH_SAMPLES (BLOCK_SIZE * NUM_BLOCKS)

#define NUM_TAPS            29
#define SNR_THRESHOLD_F32   140.0f

// The input signal and reference output
extern float32_t          output[TEST_LENGTH_SAMPLES]; 
extern float32_t expected_output[TEST_LENGTH_SAMPLES]; 

// Declare FIR State buffer of size (numTaps + blockSize - 1)
float32_t fir_state[BLOCK_SIZE + NUM_TAPS - 1]; 

// FIR Coefficients
const float32_t fir_coeff[NUM_TAPS] = { 
    -0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f,
    +0.0085302217f, -0.0000000000f, -0.0173976984f, -0.0341458607f, -0.0333591565f,
    +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f,
    +0.2229246956f, +0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f,
    -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f, +0.0080754303f,
    +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f
};

int32_t main(void) { 
    // Call FIR init function to initialize the instance structure.
    arm_fir_instance_f32 fir;
    arm_fir_init_f32(&fir, NUM_TAPS, (float32_t *)fir_coeff, fir_state, BLOCK_SIZE); 
    
    // Call the FIR process function for every BLOCK_SIZE samples  
    for (uint32_t i=0; i < NUM_BLOCKS; i++) {
        float32_t* signal =  output + (i * BLOCK_SIZE);
        arm_fir_f32(&fir, signal, signal, BLOCK_SIZE);
    } 
    
    // Compare the generated output against the reference
    float32_t snr = arm_snr_f32(expected_output, output, TEST_LENGTH_SAMPLES);
    printf("snr: %f\n\r", snr);
    if (snr < SNR_THRESHOLD_F32) {
        printf("Failed\n\r");
    } else {
        printf("Success\n\r");
    }
}