spek

Acoustic spectrum analyser https://github.com/alexkay/spek spek.cc
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

test-fft.cc (2274B)


      1 #include "spek-fft.h"
      2 
      3 #include "test.h"
      4 
      5 // FFT sizes that avfft can actually handle.
      6 static const int FFT_BITS_MIN = 4;
      7 static const int FFT_BITS_MAX = 15;
      8 
      9 static void test_const()
     10 {
     11     FFT fft;
     12     for (int nbits = FFT_BITS_MIN; nbits <= FFT_BITS_MAX; ++nbits) {
     13         auto plan = fft.create(nbits);
     14         test("input size", 1 << nbits, plan->get_input_size());
     15         test("output size", (1 << nbits) / 2 + 1, plan->get_output_size());
     16 
     17         // Test zero input.
     18         for (int i = 0; i < plan->get_input_size(); ++i) {
     19             plan->set_input(i, 0.0f);
     20         }
     21         plan->execute();
     22         bool silence = true;
     23         for (int i = 0; i < plan->get_output_size(); ++i) {
     24             if (plan->get_output(i) > -1e12f) {
     25                 silence = false;
     26                 break;
     27             }
     28         }
     29         test("silence", true, silence);
     30 
     31         // Test DC input.
     32         for (int i = 0; i < plan->get_input_size(); ++i) {
     33             plan->set_input(i, 1.0f);
     34         }
     35         plan->execute();
     36         test("dc component", 0.0f, plan->get_output(0));
     37         silence = true;
     38         for (int i = 1; i < plan->get_output_size(); ++i) {
     39             if (plan->get_output(i) > -1e12f) {
     40                 silence = false;
     41                 break;
     42             }
     43         }
     44         test("silence", true, silence);
     45     }
     46 }
     47 
     48 static void test_sine()
     49 {
     50     FFT fft;
     51     for (int nbits = FFT_BITS_MIN; nbits <= FFT_BITS_MAX; ++nbits) {
     52         auto plan = fft.create(nbits);
     53         int n = plan->get_input_size();
     54         for (int k = 1; k < n / 2; k *= 2) {
     55             for (int i = 0; i < n; ++i) {
     56                 plan->set_input(i, sin(k * i * 2.0 * M_PI / n));
     57             }
     58             plan->execute();
     59             test("sine", -602, static_cast<int>(plan->get_output(k) * 100));
     60             bool silence = true;
     61             for (int i = 0; i < plan->get_output_size(); ++i) {
     62                 if (i == k) {
     63                     continue;
     64                 }
     65                 if (plan->get_output(i) > -150.0f) {
     66                     silence = false;
     67                     break;
     68                 }
     69             }
     70             test("silence", true, silence);
     71         }
     72     }
     73 }
     74 
     75 void test_fft()
     76 {
     77     run("fft const", test_const);
     78     run("fft sine", test_sine);
     79 }