]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/dsp/mbed/fir_f32/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / dsp / mbed / fir_f32 / main.cpp
1 #include "mbed.h"
2 #include "dsp.h"
3
4 #define BLOCK_SIZE              (32)
5 #define NUM_BLOCKS              (10)
6 #define TEST_LENGTH_SAMPLES     (BLOCK_SIZE * NUM_BLOCKS)
7
8 #define SAMPLE_RATE             (48000)
9
10 #define SNR_THRESHOLD_F32       (50.0f)
11
12 float32_t expected_output[TEST_LENGTH_SAMPLES];
13 float32_t          output[TEST_LENGTH_SAMPLES];
14
15 /* FIR Coefficients buffer generated using fir1() MATLAB function: fir1(28, 6/24) */
16 #define NUM_TAPS            29
17 const float32_t firCoeffs32[NUM_TAPS] = {
18     -0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f,
19     +0.0085302217f, -0.0000000000f, -0.0173976984f, -0.0341458607f, -0.0333591565f,
20     +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f,
21     +0.2229246956f, +0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f,
22     -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f, +0.0080754303f,
23     +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f
24 };
25 #define WARMUP    (NUM_TAPS-1)
26 #define DELAY     (WARMUP/2)
27
28 int main() {
29     Sine_f32 sine_1KHz(  1000, SAMPLE_RATE, 1.0);
30     Sine_f32 sine_15KHz(15000, SAMPLE_RATE, 0.5);
31     FIR_f32<NUM_TAPS> fir(firCoeffs32);
32
33     float32_t buffer_a[BLOCK_SIZE];
34     float32_t buffer_b[BLOCK_SIZE];
35     for (float32_t *sgn=output; sgn<(output+TEST_LENGTH_SAMPLES); sgn += BLOCK_SIZE) {
36         sine_1KHz.generate(buffer_a);           // Generate a 1KHz sine wave
37         sine_15KHz.process(buffer_a, buffer_b); // Add a 15KHz sine wave
38         fir.process(buffer_b, sgn);             // FIR low pass filter: 6KHz cutoff
39     }
40
41     sine_1KHz.reset();
42     for (float32_t *sgn=expected_output; sgn<(expected_output+TEST_LENGTH_SAMPLES); sgn += BLOCK_SIZE) {
43         sine_1KHz.generate(sgn);        // Generate a 1KHz sine wave
44     }
45
46     float snr = arm_snr_f32(&expected_output[DELAY-1], &output[WARMUP-1], TEST_LENGTH_SAMPLES-WARMUP);
47     printf("snr: %f\n\r", snr);
48     if (snr < SNR_THRESHOLD_F32) {
49         printf("Failed\n\r");
50     } else {
51         printf("Success\n\r");
52     }
53 }