diff --git a/src/backend/nsl/nsl_dft_test.c b/src/backend/nsl/nsl_dft_test.c index b98c6ee3b..b56442ec6 100644 --- a/src/backend/nsl/nsl_dft_test.c +++ b/src/backend/nsl/nsl_dft_test.c @@ -1,146 +1,146 @@ /*************************************************************************** File : nsl_dft_test.c Project : LabPlot Description : NSL discrete Fourier transform functions -------------------------------------------------------------------- Copyright : (C) 2016 by Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include #include "nsl_dft.h" #define NN 10 -#define TWOSIDED 1 /* 0,1 */ +#define TWOSIDED 0 /* 0,1 */ int main() { double data[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data[]={1, 2, 3, 3, 1};*/ const int N=10; #if TWOSIDED == 1 int size=N; #else int size=N/2; #endif /* input */ int i; for(i=0; i < N; i++) printf("%g ", data[i]); puts("\nraw:"); // 'raw' result nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_raw); /* output */ for(i=0; i < size; i++) printf("%g ", data[i]); puts("\nreal:"); double data2[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data2[]={1, 2, 3, 3, 1};*/ nsl_dft_transform(data2, 1, N, TWOSIDED, nsl_dft_result_real); /* output */ for(i=0; i < size; i++) printf("%g ", data2[i]); puts("\nimag:"); double data3[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data3[]={1, 2, 3, 3, 1};*/ nsl_dft_transform(data3, 1, N, TWOSIDED, nsl_dft_result_imag); /* output */ for(i=0; i < size; i++) printf("%g ", data3[i]); puts("\nmagnitude:"); double data4[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data4[]={1, 2, 3, 3, 1};*/ nsl_dft_transform(data4, 1, N, TWOSIDED, nsl_dft_result_magnitude); /* output */ for(i=0; i < size; i++) printf("%g ", data4[i]); puts("\namplitude:"); double data5[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data5[]={1, 2, 3, 3, 1};*/ nsl_dft_transform(data5, 1, N, TWOSIDED, nsl_dft_result_amplitude); /* output */ for(i=0; i < size; i++) printf("%g ", data5[i]); puts("\npower:"); double data6[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data6[]={1, 2, 3, 3, 1};*/ nsl_dft_transform(data6, 1, N, TWOSIDED, nsl_dft_result_power); /* output */ for(i=0; i < size; i++) printf("%g ", data6[i]); puts("\nphase:"); double data7[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data7[]={1, 2, 3, 3, 1};*/ nsl_dft_transform(data7, 1, N, TWOSIDED, nsl_dft_result_phase); /* output */ for(i=0; i < size; i++) printf("%g ", data7[i]); puts("\ndB:"); double data8[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; /*double data8[]={1, 2, 3, 3, 1};*/ nsl_dft_transform(data8, 1, N, TWOSIDED, nsl_dft_result_dB); /* output */ for(i=0; i < size; i++) printf("%g ", data8[i]); puts("\nsquare magnitude:"); double data9[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; nsl_dft_transform(data9, 1, N, TWOSIDED, nsl_dft_result_squaremagnitude); /* output */ for(i=0; i < size; i++) printf("%g ", data9[i]); puts("\nsquare amplitude:"); double data10[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; nsl_dft_transform(data10, 1, N, TWOSIDED, nsl_dft_result_squareamplitude); /* output */ for(i=0; i < size; i++) printf("%g ", data10[i]); puts("\nnormdB:"); double data11[]={1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; nsl_dft_transform(data11, 1, N, TWOSIDED, nsl_dft_result_normdB); /* output */ for(i=0; i < size; i++) printf("%g ", data11[i]); puts(""); } diff --git a/tests/nsl/CMakeLists.txt b/tests/nsl/CMakeLists.txt index ef2e8a993..fd6bf80b2 100644 --- a/tests/nsl/CMakeLists.txt +++ b/tests/nsl/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(dft) add_subdirectory(sf_basic) diff --git a/tests/nsl/dft/CMakeLists.txt b/tests/nsl/dft/CMakeLists.txt new file mode 100644 index 000000000..48f4dce2a --- /dev/null +++ b/tests/nsl/dft/CMakeLists.txt @@ -0,0 +1,36 @@ +INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) +add_executable (nsldfttest NSLDFTTest.cpp) + +target_link_libraries(nsldfttest Qt5::Test) +target_link_libraries(nsldfttest KF5::Archive KF5::XmlGui ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) + +IF (Qt5SerialPort_FOUND) + target_link_libraries(nsldfttest Qt5::SerialPort ) +ENDIF () +IF (KF5SyntaxHighlighting_FOUND) + target_link_libraries(nsldfttest KF5::SyntaxHighlighting ) +ENDIF () +#TODO: KF5::NewStuff + +IF (CANTOR_LIBS_FOUND) + target_link_libraries(nsldfttest ${CANTOR_LIBS} ) +ENDIF () +IF (HDF5_FOUND) + target_link_libraries(nsldfttest ${HDF5_C_LIBRARIES} ) +ENDIF () +IF (FFTW_FOUND) + target_link_libraries(nsldfttest ${FFTW_LIBRARIES} ) +ENDIF () +IF (NETCDF_FOUND) + target_link_libraries(nsldfttest ${NETCDF_LIBRARY} ) +ENDIF () +IF (CFITSIO_FOUND) + target_link_libraries(nsldfttest ${CFITSIO_LIBRARY} ) +ENDIF () +IF (USE_LIBORIGIN) +target_link_libraries(nsldfttest liborigin-static ) +ENDIF () + +target_link_libraries(nsldfttest labplot2lib) + +add_test(NAME nsldfttest COMMAND nsldfttest) diff --git a/tests/nsl/dft/NSLDFTTest.cpp b/tests/nsl/dft/NSLDFTTest.cpp new file mode 100644 index 000000000..2da864629 --- /dev/null +++ b/tests/nsl/dft/NSLDFTTest.cpp @@ -0,0 +1,147 @@ +/*************************************************************************** + File : NSLDFTTest.cpp + Project : LabPlot + Description : NSL Tests for DFT + -------------------------------------------------------------------- + Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + ***************************************************************************/ + +#include "NSLDFTTest.h" + +extern "C" { +#include "backend/nsl/nsl_dft.h" +} + +void NSLDFTTest::initTestCase() { + const QString currentDir = __FILE__; + m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); +} + +#define ONESIDED 0 +#define TWOSIDED 1 +#define N 10 + +//############################################################################## +//################# one sided tests +//############################################################################## + +void NSLDFTTest::test_onesided_real() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {10, 2, -5.85410196624968, 2, 0.854101966249685}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_real); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_imag() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {0, -4.97979656976556, 0, 0.449027976579585, 0}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_imag); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_magnitude() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {10, 5.36641163872553, 5.85410196624968, 2.04978684837013, 0.854101966249685}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_magnitude); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_amplitude() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {1, 1.07328232774511, 1.17082039324994, 0.409957369674026, 0.170820393249937}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_amplitude); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_power() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {10, 5.75967477524977, 6.85410196624968, 0.840325224750231, 0.145898033750316}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_power); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_phase() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {0, 1.18889174012102, -3.14159265358979, -0.220851801285041, 0}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_phase); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_dB() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {0, 0.614279569861449, 1.36980556663896, -7.7452260401377, -15.3492056533593}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_dB); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_squaremagnitude() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {100, 28.7983738762488, 34.2705098312484, 4.20162612375116, 0.729490168751578}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_squaremagnitude); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_squareamplitude() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {1, 1.15193495504995, 1.37082039324994, 0.168065044950046, 0.0291796067500631}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_squareamplitude); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +void NSLDFTTest::test_onesided_normdB() { + double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; + double result[] = {-1.36980556663896, -0.755525996777514, 0, -9.11503160677666, -16.7190112199983}; + + nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_normdB); + for (unsigned int i = 0; i < N/2; i++) + QCOMPARE(data[i], result[i]); +} + +//############################################################################## +//################# two sided tests +//############################################################################## + +/* QBENCHMARK { + for (unsigned int i = 1; i < 1e7; i++) + Q_UNUSED((int)log2(i)); + } +*/ + +QTEST_MAIN(NSLDFTTest) diff --git a/tests/nsl/dft/NSLDFTTest.h b/tests/nsl/dft/NSLDFTTest.h new file mode 100644 index 000000000..736edf722 --- /dev/null +++ b/tests/nsl/dft/NSLDFTTest.h @@ -0,0 +1,56 @@ +/*************************************************************************** + File : NSLDFTTest.h + Project : LabPlot + Description : NSL Tests for DFT + -------------------------------------------------------------------- + Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + ***************************************************************************/ +#ifndef NSLDFTTEST_H +#define NSLDFTTEST_H + +#include + +class NSLDFTTest : public QObject { + Q_OBJECT + +private slots: + void initTestCase(); + + // one sided + void test_onesided_real(); + void test_onesided_imag(); + void test_onesided_magnitude(); + void test_onesided_amplitude(); + void test_onesided_power(); + void test_onesided_phase(); + void test_onesided_dB(); + void test_onesided_squaremagnitude(); + void test_onesided_squareamplitude(); + void test_onesided_normdB(); + // two sided + // TODO + // performance + // TODO +private: + QString m_dataDir; +}; +#endif diff --git a/tests/nsl/sf_basic/NSLSFBasicTest.cpp b/tests/nsl/sf_basic/NSLSFBasicTest.cpp index 2e6cb9661..9a3b9077f 100644 --- a/tests/nsl/sf_basic/NSLSFBasicTest.cpp +++ b/tests/nsl/sf_basic/NSLSFBasicTest.cpp @@ -1,112 +1,112 @@ /*************************************************************************** File : NSLSFBasicTest.cpp Project : LabPlot Description : NSL Tests for basic special functions -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "NSLSFBasicTest.h" extern "C" { #include "backend/nsl/nsl_sf_basic.h" } void NSLSFBasicTest::initTestCase() { const QString currentDir = __FILE__; m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); } //############################################################################## -//################# handling of empty and sparse files ######################## +//################# log2() tests //############################################################################## void NSLSFBasicTest::testlog2_int_C99() { QBENCHMARK { for (unsigned int i = 1; i < 1e7; i++) Q_UNUSED((int)log2(i)); } } void NSLSFBasicTest::testlog2_int() { for (unsigned int i = 1; i < 1e5; i++) { int result = nsl_sf_log2_int(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (unsigned int i = 1; i < 1e7; i++) nsl_sf_log2_int(i); } } void NSLSFBasicTest::testlog2_longlong() { #ifndef _MSC_VER /* not implemented yet */ for (unsigned long long i = 1; i < 1e5; i++) { int result = nsl_sf_log2_longlong(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (unsigned long long i = 1; i < 1e7; i++) nsl_sf_log2_longlong(i); } #endif } void NSLSFBasicTest::testlog2_int2() { for (int i = 1; i < 1e5; i++) { int result = nsl_sf_log2_int2(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (int i = 1; i < 1e7; i++) nsl_sf_log2_int2(i); } } void NSLSFBasicTest::testlog2_int3() { for (unsigned int i = 1; i < 1e5; i++) { int result = nsl_sf_log2_int3(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (unsigned int i = 1; i < 1e7; i++) nsl_sf_log2_int3(i); } } void NSLSFBasicTest::testlog2p1_int() { for (int i = 1; i < 1e5; i++) { int result = nsl_sf_log2p1_int(i); QCOMPARE(result, (int)log2(i) + 1); } QBENCHMARK { for (int i = 1; i < 1e7; i++) nsl_sf_log2p1_int(i); } } QTEST_MAIN(NSLSFBasicTest)