Emilio Monti
/
dsp_fir
Example FIR filter using the draft mbed DSP API
Revision 2:697afbb006f5, committed 2012-03-12
- Comitter:
- emilmont
- Date:
- Mon Mar 12 12:04:27 2012 +0000
- Parent:
- 1:0814c87942ca
- Child:
- 3:15d0df9ab05b
- Commit message:
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Mar 12 12:03:25 2012 +0000 +++ b/main.cpp Mon Mar 12 12:04:27 2012 +0000 @@ -1,53 +1,53 @@ -#include "mbed.h" -#include "dsp.h" - -#define BLOCK_SIZE (32) -#define NUM_BLOCKS (10) -#define TEST_LENGTH_SAMPLES (BLOCK_SIZE * NUM_BLOCKS) - -#define SAMPLE_RATE (48000) - -#define SNR_THRESHOLD_F32 (50.0f) - -float32_t expected_output[TEST_LENGTH_SAMPLES]; -float32_t signal[TEST_LENGTH_SAMPLES]; - -/* FIR Coefficients buffer generated using fir1() MATLAB function: fir1(28, 6/24) */ -#define NUM_TAPS 29 -const float32_t firCoeffs32[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 -}; -#define WARMUP (NUM_TAPS-1) -#define DELAY (WARMUP/2) - -int main() { - Sine_f32 sine_1K( 1000, SAMPLE_RATE, 1.0); - Sine_f32 sine_15K(15000, SAMPLE_RATE, 0.5); - FIR_f32<NUM_TAPS> fir(firCoeffs32); - - for (int i=0; i<NUM_BLOCKS; i++) { - float32_t *signal_buffer = signal + i*BLOCK_SIZE; - sine_1K.process(signal_buffer); - sine_15K.process(signal_buffer); - fir.process(signal_buffer); - } - - sine_1K.reset(); - for (int i=0; i<NUM_BLOCKS; i++) { - float32_t *signal_buffer = expected_output + i*BLOCK_SIZE; - sine_1K.process(signal_buffer); - } - - float snr = arm_snr_f32(&expected_output[DELAY-1], &signal[WARMUP-1], TEST_LENGTH_SAMPLES-WARMUP); - printf("snr: %f\n\r", snr); - if (snr < SNR_THRESHOLD_F32) { - printf("Failed\n\r"); - } else { - printf("Success\n\r"); - } -} +#include "mbed.h" +#include "dsp.h" + +#define BLOCK_SIZE (32) +#define NUM_BLOCKS (10) +#define TEST_LENGTH_SAMPLES (BLOCK_SIZE * NUM_BLOCKS) + +#define SAMPLE_RATE (48000) + +#define SNR_THRESHOLD_F32 (50.0f) + +float32_t expected_output[TEST_LENGTH_SAMPLES]; +float32_t signal[TEST_LENGTH_SAMPLES]; + +/* FIR Coefficients buffer generated using fir1() MATLAB function: fir1(28, 6/24) */ +#define NUM_TAPS 29 +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 +}; +#define WARMUP (NUM_TAPS-1) +#define DELAY (WARMUP/2) + +int main() { + Sine_f32 sine_1K( 1000, SAMPLE_RATE, 1.0); + Sine_f32 sine_15K(15000, SAMPLE_RATE, 0.5); + FIR_f32<NUM_TAPS> fir(fir_coeff); + + for (int i=0; i<NUM_BLOCKS; i++) { + float32_t *signal_buffer = signal + i*BLOCK_SIZE; + sine_1K.process(signal_buffer); + sine_15K.process(signal_buffer); + fir.process(signal_buffer); + } + + sine_1K.reset(); + for (int i=0; i<NUM_BLOCKS; i++) { + float32_t *signal_buffer = expected_output + i*BLOCK_SIZE; + sine_1K.process(signal_buffer); + } + + float snr = arm_snr_f32(&expected_output[DELAY-1], &signal[WARMUP-1], TEST_LENGTH_SAMPLES-WARMUP); + printf("snr: %f\n\r", snr); + if (snr < SNR_THRESHOLD_F32) { + printf("Failed\n\r"); + } else { + printf("Success\n\r"); + } +}