matthiasm@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ matthiasm@0: /* matthiasm@0: This file is Copyright (c) 2012 Chris Cannam matthiasm@0: matthiasm@0: Permission is hereby granted, free of charge, to any person matthiasm@0: obtaining a copy of this software and associated documentation matthiasm@0: files (the "Software"), to deal in the Software without matthiasm@0: restriction, including without limitation the rights to use, copy, matthiasm@0: modify, merge, publish, distribute, sublicense, and/or sell copies matthiasm@0: of the Software, and to permit persons to whom the Software is matthiasm@0: furnished to do so, subject to the following conditions: matthiasm@0: matthiasm@0: The above copyright notice and this permission notice shall be matthiasm@0: included in all copies or substantial portions of the Software. matthiasm@0: matthiasm@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, matthiasm@0: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF matthiasm@0: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND matthiasm@0: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR matthiasm@0: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF matthiasm@0: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION matthiasm@0: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. matthiasm@0: */ matthiasm@0: matthiasm@0: /* matthiasm@0: This unit test suite for the Vamp SDK FFT implementation is included matthiasm@0: here mostly for illustrative purposes! matthiasm@0: */ matthiasm@0: matthiasm@0: #include "vamp-sdk/FFT.h" matthiasm@0: matthiasm@0: #define BOOST_TEST_DYN_LINK matthiasm@0: #define BOOST_TEST_MAIN matthiasm@0: matthiasm@0: #include matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_SUITE(TestFFT) matthiasm@0: matthiasm@0: #define COMPARE_CONST(a, n) \ matthiasm@0: for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \ matthiasm@0: BOOST_CHECK_SMALL(a[cmp_i] - n, 1e-14); \ matthiasm@0: } matthiasm@0: matthiasm@0: #define COMPARE_ARRAY(a, b) \ matthiasm@0: for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \ matthiasm@0: BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-14); \ matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(dc) matthiasm@0: { matthiasm@0: // DC-only signal. The DC bin is purely real matthiasm@0: double in[] = { 1, 1, 1, 1 }; matthiasm@0: double re[4], im[4]; matthiasm@0: Vamp::FFT::forward(4, in, 0, re, im); matthiasm@0: BOOST_CHECK_EQUAL(re[0], 4.0); matthiasm@0: BOOST_CHECK_EQUAL(re[1], 0.0); matthiasm@0: BOOST_CHECK_EQUAL(re[2], 0.0); matthiasm@0: COMPARE_CONST(im, 0.0); matthiasm@0: double back[4]; matthiasm@0: double backim[4]; matthiasm@0: Vamp::FFT::inverse(4, re, im, back, backim); matthiasm@0: COMPARE_ARRAY(back, in); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(sine) matthiasm@0: { matthiasm@0: // Sine. Output is purely imaginary matthiasm@0: double in[] = { 0, 1, 0, -1 }; matthiasm@0: double re[4], im[4]; matthiasm@0: Vamp::FFT::forward(4, in, 0, re, im); matthiasm@0: COMPARE_CONST(re, 0.0); matthiasm@0: BOOST_CHECK_EQUAL(im[0], 0.0); matthiasm@0: BOOST_CHECK_EQUAL(im[1], -2.0); matthiasm@0: BOOST_CHECK_EQUAL(im[2], 0.0); matthiasm@0: double back[4]; matthiasm@0: double backim[4]; matthiasm@0: Vamp::FFT::inverse(4, re, im, back, backim); matthiasm@0: COMPARE_ARRAY(back, in); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(cosine) matthiasm@0: { matthiasm@0: // Cosine. Output is purely real matthiasm@0: double in[] = { 1, 0, -1, 0 }; matthiasm@0: double re[4], im[4]; matthiasm@0: Vamp::FFT::forward(4, in, 0, re, im); matthiasm@0: BOOST_CHECK_EQUAL(re[0], 0.0); matthiasm@0: BOOST_CHECK_EQUAL(re[1], 2.0); matthiasm@0: BOOST_CHECK_EQUAL(re[2], 0.0); matthiasm@0: COMPARE_CONST(im, 0.0); matthiasm@0: double back[4]; matthiasm@0: double backim[4]; matthiasm@0: Vamp::FFT::inverse(4, re, im, back, backim); matthiasm@0: COMPARE_ARRAY(back, in); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(sineCosine) matthiasm@0: { matthiasm@0: // Sine and cosine mixed matthiasm@0: double in[] = { 0.5, 1, -0.5, -1 }; matthiasm@0: double re[4], im[4]; matthiasm@0: Vamp::FFT::forward(4, in, 0, re, im); matthiasm@0: BOOST_CHECK_EQUAL(re[0], 0.0); matthiasm@0: BOOST_CHECK_CLOSE(re[1], 1.0, 1e-12); matthiasm@0: BOOST_CHECK_EQUAL(re[2], 0.0); matthiasm@0: BOOST_CHECK_EQUAL(im[0], 0.0); matthiasm@0: BOOST_CHECK_CLOSE(im[1], -2.0, 1e-12); matthiasm@0: BOOST_CHECK_EQUAL(im[2], 0.0); matthiasm@0: double back[4]; matthiasm@0: double backim[4]; matthiasm@0: Vamp::FFT::inverse(4, re, im, back, backim); matthiasm@0: COMPARE_ARRAY(back, in); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(nyquist) matthiasm@0: { matthiasm@0: double in[] = { 1, -1, 1, -1 }; matthiasm@0: double re[4], im[4]; matthiasm@0: Vamp::FFT::forward(4, in, 0, re, im); matthiasm@0: BOOST_CHECK_EQUAL(re[0], 0.0); matthiasm@0: BOOST_CHECK_EQUAL(re[1], 0.0); matthiasm@0: BOOST_CHECK_EQUAL(re[2], 4.0); matthiasm@0: COMPARE_CONST(im, 0.0); matthiasm@0: double back[4]; matthiasm@0: double backim[4]; matthiasm@0: Vamp::FFT::inverse(4, re, im, back, backim); matthiasm@0: COMPARE_ARRAY(back, in); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(dirac) matthiasm@0: { matthiasm@0: double in[] = { 1, 0, 0, 0 }; matthiasm@0: double re[4], im[4]; matthiasm@0: Vamp::FFT::forward(4, in, 0, re, im); matthiasm@0: BOOST_CHECK_EQUAL(re[0], 1.0); matthiasm@0: BOOST_CHECK_EQUAL(re[1], 1.0); matthiasm@0: BOOST_CHECK_EQUAL(re[2], 1.0); matthiasm@0: COMPARE_CONST(im, 0.0); matthiasm@0: double back[4]; matthiasm@0: double backim[4]; matthiasm@0: Vamp::FFT::inverse(4, re, im, back, backim); matthiasm@0: COMPARE_ARRAY(back, in); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(forwardArrayBounds) matthiasm@0: { matthiasm@0: // initialise bins to something recognisable, so we can tell matthiasm@0: // if they haven't been written matthiasm@0: double in[] = { 1, 1, -1, -1 }; matthiasm@0: double re[] = { 999, 999, 999, 999, 999, 999 }; matthiasm@0: double im[] = { 999, 999, 999, 999, 999, 999 }; matthiasm@0: Vamp::FFT::forward(4, in, 0, re+1, im+1); matthiasm@0: // And check we haven't overrun the arrays matthiasm@0: BOOST_CHECK_EQUAL(re[0], 999.0); matthiasm@0: BOOST_CHECK_EQUAL(im[0], 999.0); matthiasm@0: BOOST_CHECK_EQUAL(re[5], 999.0); matthiasm@0: BOOST_CHECK_EQUAL(im[5], 999.0); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_CASE(inverseArrayBounds) matthiasm@0: { matthiasm@0: // initialise bins to something recognisable, so we can tell matthiasm@0: // if they haven't been written matthiasm@0: double re[] = { 0, 1, 0 }; matthiasm@0: double im[] = { 0, -2, 0 }; matthiasm@0: double outre[] = { 999, 999, 999, 999, 999, 999 }; matthiasm@0: double outim[] = { 999, 999, 999, 999, 999, 999 }; matthiasm@0: Vamp::FFT::forward(4, re, im, outre+1, outim+1); matthiasm@0: // And check we haven't overrun the arrays matthiasm@0: BOOST_CHECK_EQUAL(outre[0], 999.0); matthiasm@0: BOOST_CHECK_EQUAL(outim[0], 999.0); matthiasm@0: BOOST_CHECK_EQUAL(outre[5], 999.0); matthiasm@0: BOOST_CHECK_EQUAL(outim[5], 999.0); matthiasm@0: } matthiasm@0: matthiasm@0: BOOST_AUTO_TEST_SUITE_END() matthiasm@0: