# HG changeset patch # User Chris Cannam # Date 1380556238 -3600 # Node ID 0a632ac709450599342545ffece4a505a5a41da2 # Parent f8c7ca4e56674b5bbb74a14301b6821685a270ac FFT and window tests diff -r f8c7ca4e5667 -r 0a632ac70945 tests/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/Makefile Mon Sep 30 16:50:38 2013 +0100 @@ -0,0 +1,21 @@ + +CFLAGS := -I.. $(CFLAGS) +CXXFLAGS := -I.. $(CXXFLAGS) + +LDFLAGS := $(LDFLAGS) -lboost_unit_test_framework +LIBS := ../libqm-dsp.a + +TESTS := test-window test-fft + +all: $(TESTS) + for t in $(TESTS); do echo "Running $$t"; ./"$$t" || exit 1; done + +test-window: TestWindow.o $(LIBS) + $(CXX) -o $@ $^ $(LDFLAGS) + +test-fft: TestFFT.o $(LIBS) + $(CXX) -o $@ $^ $(LDFLAGS) + +clean: + rm *.o $(TESTS) + diff -r f8c7ca4e5667 -r 0a632ac70945 tests/TestFFT.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/TestFFT.cpp Mon Sep 30 16:50:38 2013 +0100 @@ -0,0 +1,149 @@ + +#include "dsp/transforms/FFT.h" + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MAIN + +#include + +BOOST_AUTO_TEST_SUITE(TestFFT) + +#define COMPARE_CONST(a, n) \ + for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \ + BOOST_CHECK_SMALL(a[cmp_i] - n, 1e-14); \ + } + +#define COMPARE_ARRAY(a, b) \ + for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \ + BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-14); \ + } + +BOOST_AUTO_TEST_CASE(dc) +{ + // DC-only signal. The DC bin is purely real + double in[] = { 1, 1, 1, 1 }; + double re[4], im[4]; + FFT(4).process(false, in, 0, re, im); + BOOST_CHECK_EQUAL(re[0], 4.0); + BOOST_CHECK_EQUAL(re[1], 0.0); + BOOST_CHECK_EQUAL(re[2], 0.0); + COMPARE_CONST(im, 0.0); + double back[4]; + double backim[4]; + FFT(4).process(true, re, im, back, backim); + COMPARE_ARRAY(back, in); +} + +BOOST_AUTO_TEST_CASE(sine) +{ + // Sine. Output is purely imaginary + double in[] = { 0, 1, 0, -1 }; + double re[4], im[4]; + FFT(4).process(false, in, 0, re, im); + COMPARE_CONST(re, 0.0); + BOOST_CHECK_EQUAL(im[0], 0.0); + BOOST_CHECK_EQUAL(im[1], -2.0); + BOOST_CHECK_EQUAL(im[2], 0.0); + double back[4]; + double backim[4]; + FFT(4).process(true, re, im, back, backim); + COMPARE_ARRAY(back, in); +} + +BOOST_AUTO_TEST_CASE(cosine) +{ + // Cosine. Output is purely real + double in[] = { 1, 0, -1, 0 }; + double re[4], im[4]; + FFT(4).process(false, in, 0, re, im); + BOOST_CHECK_EQUAL(re[0], 0.0); + BOOST_CHECK_EQUAL(re[1], 2.0); + BOOST_CHECK_EQUAL(re[2], 0.0); + COMPARE_CONST(im, 0.0); + double back[4]; + double backim[4]; + FFT(4).process(true, re, im, back, backim); + COMPARE_ARRAY(back, in); +} + +BOOST_AUTO_TEST_CASE(sineCosine) +{ + // Sine and cosine mixed + double in[] = { 0.5, 1, -0.5, -1 }; + double re[4], im[4]; + FFT(4).process(false, in, 0, re, im); + BOOST_CHECK_EQUAL(re[0], 0.0); + BOOST_CHECK_CLOSE(re[1], 1.0, 1e-12); + BOOST_CHECK_EQUAL(re[2], 0.0); + BOOST_CHECK_EQUAL(im[0], 0.0); + BOOST_CHECK_CLOSE(im[1], -2.0, 1e-12); + BOOST_CHECK_EQUAL(im[2], 0.0); + double back[4]; + double backim[4]; + FFT(4).process(true, re, im, back, backim); + COMPARE_ARRAY(back, in); +} + +BOOST_AUTO_TEST_CASE(nyquist) +{ + double in[] = { 1, -1, 1, -1 }; + double re[4], im[4]; + FFT(4).process(false, in, 0, re, im); + BOOST_CHECK_EQUAL(re[0], 0.0); + BOOST_CHECK_EQUAL(re[1], 0.0); + BOOST_CHECK_EQUAL(re[2], 4.0); + COMPARE_CONST(im, 0.0); + double back[4]; + double backim[4]; + FFT(4).process(true, re, im, back, backim); + COMPARE_ARRAY(back, in); +} + +BOOST_AUTO_TEST_CASE(dirac) +{ + double in[] = { 1, 0, 0, 0 }; + double re[4], im[4]; + FFT(4).process(false, in, 0, re, im); + BOOST_CHECK_EQUAL(re[0], 1.0); + BOOST_CHECK_EQUAL(re[1], 1.0); + BOOST_CHECK_EQUAL(re[2], 1.0); + COMPARE_CONST(im, 0.0); + double back[4]; + double backim[4]; + FFT(4).process(true, re, im, back, backim); + COMPARE_ARRAY(back, in); +} + +BOOST_AUTO_TEST_CASE(forwardArrayBounds) +{ + // initialise bins to something recognisable, so we can tell + // if they haven't been written + double in[] = { 1, 1, -1, -1 }; + double re[] = { 999, 999, 999, 999, 999, 999 }; + double im[] = { 999, 999, 999, 999, 999, 999 }; + FFT(4).process(false, in, 0, re+1, im+1); + // And check we haven't overrun the arrays + BOOST_CHECK_EQUAL(re[0], 999.0); + BOOST_CHECK_EQUAL(im[0], 999.0); + BOOST_CHECK_EQUAL(re[5], 999.0); + BOOST_CHECK_EQUAL(im[5], 999.0); +} + +BOOST_AUTO_TEST_CASE(inverseArrayBounds) +{ + // initialise bins to something recognisable, so we can tell + // if they haven't been written + double re[] = { 0, 1, 0 }; + double im[] = { 0, -2, 0 }; + double outre[] = { 999, 999, 999, 999, 999, 999 }; + double outim[] = { 999, 999, 999, 999, 999, 999 }; + FFT(4).process(false, re, im, outre+1, outim+1); + // And check we haven't overrun the arrays + BOOST_CHECK_EQUAL(outre[0], 999.0); + BOOST_CHECK_EQUAL(outim[0], 999.0); + BOOST_CHECK_EQUAL(outre[5], 999.0); + BOOST_CHECK_EQUAL(outim[5], 999.0); +} + +BOOST_AUTO_TEST_SUITE_END() + diff -r f8c7ca4e5667 -r 0a632ac70945 tests/TestWindow.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/TestWindow.cpp Mon Sep 30 16:50:38 2013 +0100 @@ -0,0 +1,114 @@ + +#include "base/Window.h" + +#include + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MAIN + +#include + +BOOST_AUTO_TEST_SUITE(TestWindow) + +using std::cout; +using std::endl; + +#define COMPARE_ARRAY(a, b) \ + for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \ + BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-4); \ + } + +void +testSymmetric(double *d, int n) +{ + for (int i = 0; i <= n/2; ++i) { + BOOST_CHECK_CLOSE(d[i], d[n-i-1], 1e-10); + } +} + +BOOST_AUTO_TEST_CASE(periodic) +{ + // We can't actually test whether a function is periodic, given + // only one cycle of it! But we can make sure that all but the + // first sample is symmetric, which is what a symmetric window + // becomes when generated in periodic mode + double d[9]; + for (int n = 8; n <= 9; ++n) { + for (int wt = (int)FirstWindow; wt <= (int)LastWindow; ++wt) { + for (int i = 0; i < n; ++i) d[i] = 1.0; + Window w((WindowType)wt, n); + w.cut(d); + testSymmetric(d + 1, n - 1); + } + } +} + +template +void testWindow(WindowType type, const double expected[N]) +{ + double d[N]; + for (int i = 0; i < N; ++i) d[i] = 1.0; + Window w(type, N); + w.cut(d); + COMPARE_ARRAY(d, expected); +} + +BOOST_AUTO_TEST_CASE(bartlett) +{ + double e1[] = { 1 }; + testWindow<1>(BartlettWindow, e1); + + double e2[] = { 0, 0 }; + testWindow<2>(BartlettWindow, e2); + + double e3[] = { 0, 2./3., 2./3. }; + testWindow<3>(BartlettWindow, e3); + + double e4[] = { 0, 1./2., 1., 1./2. }; + testWindow<4>(BartlettWindow, e4); + + double e5[] = { 0, 1./2., 1., 1., 1./2. }; + testWindow<5>(BartlettWindow, e5); + + double e6[] = { 0, 1./3., 2./3., 1., 2./3., 1./3. }; + testWindow<6>(BartlettWindow, e6); +} + +BOOST_AUTO_TEST_CASE(hamming) +{ + double e1[] = { 1 }; + testWindow<1>(HammingWindow, e1); + + double e10[] = { + 0.0800, 0.1679, 0.3979, 0.6821, 0.9121, + 1.0000, 0.9121, 0.6821, 0.3979, 0.1679 + }; + testWindow<10>(HammingWindow, e10); +} + +BOOST_AUTO_TEST_CASE(hann) +{ + double e1[] = { 1 }; + testWindow<1>(HanningWindow, e1); + + double e10[] = { + 0, 0.0955, 0.3455, 0.6545, 0.9045, + 1.0000, 0.9045, 0.6545, 0.3455, 0.0955, + }; + testWindow<10>(HanningWindow, e10); +} + +BOOST_AUTO_TEST_CASE(blackman) +{ + double e1[] = { 1 }; + testWindow<1>(BlackmanWindow, e1); + + double e10[] = { + 0, 0.0402, 0.2008, 0.5098, 0.8492, + 1.0000, 0.8492, 0.5098, 0.2008, 0.0402, + }; + testWindow<10>(BlackmanWindow, e10); +} + +BOOST_AUTO_TEST_SUITE_END() +