mirror of
https://github.com/alexkay/spek.git
synced 2025-04-17 17:12:19 +03:00
tests: FFT of a sine wave
This commit is contained in:
parent
45dfa998fd
commit
fcd1463fbb
@ -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);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
#define TEST_H_
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user