spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit fcd1463fbb8751b409785100b01201b6dff95b40
parent 45dfa998fd78ea953bf172d50ed386a4f10804f9
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Tue, 16 Apr 2013 10:36:53 -0700

tests: FFT of a sine wave

Diffstat:
Mtests/test-fft.cc | 46+++++++++++++++++++++++++++++++++++++++-------
Mtests/test.h | 1+
2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/tests/test-fft.cc b/tests/test-fft.cc @@ -20,10 +20,14 @@ #include "test.h" +// FFT sizes that avfft can actually handle. +static const int FFT_BITS_MIN = 4; +static const int FFT_BITS_MAX = 15; + static void test_const() { FFT fft; - for (int nbits = 4; nbits < 16; ++nbits) { + for (int nbits = FFT_BITS_MIN; nbits <= FFT_BITS_MAX; ++nbits) { auto plan = fft.create(nbits); test("input size", 1 << nbits, plan->get_input_size()); test("output size", (1 << nbits) / 2 + 1, plan->get_output_size()); @@ -33,33 +37,61 @@ static void test_const() plan->set_input(i, 0.0f); } plan->execute(); - bool ok = true; + bool silence = true; for (int i = 0; i < plan->get_output_size(); ++i) { if (plan->get_output(i) > -1e12f) { - ok = false; + silence = false; break; } } - test("zero input", true, ok); + test("silence", true, silence); // Test DC input. for (int i = 0; i < plan->get_input_size(); ++i) { plan->set_input(i, 1.0f); } plan->execute(); - ok = true; test("dc component", 0.0f, plan->get_output(0)); + silence = true; for (int i = 1; i < plan->get_output_size(); ++i) { if (plan->get_output(i) > -1e12f) { - ok = false; + silence = false; break; } } - test("dc input", true, ok); + test("silence", true, silence); + } +} + +static void test_sine() +{ + FFT fft; + for (int nbits = FFT_BITS_MIN; nbits <= FFT_BITS_MAX; ++nbits) { + auto plan = fft.create(nbits); + int n = plan->get_input_size(); + for (int k = 1; k < n / 2; k *= 2) { + for (int i = 0; i < n; ++i) { + plan->set_input(i, sin(k * i * 2.0 * M_PI / n)); + } + plan->execute(); + test("sine", -602, static_cast<int>(plan->get_output(k) * 100)); + bool silence = true; + for (int i = 0; i < plan->get_output_size(); ++i) { + if (i == k) { + continue; + } + if (plan->get_output(i) > -150.0f) { + silence = false; + break; + } + } + test("silence", true, silence); + } } } void test_fft() { run("fft const", test_const); + run("fft sine", test_sine); } diff --git a/tests/test.h b/tests/test.h @@ -20,6 +20,7 @@ #define TEST_H_ #include <cmath> +#include <cstdio> #include <functional> #include <iostream> #include <string>