File indexing completed on 2024-04-28 03:48:20

0001 /*
0002     File                 : NSLPeakTest.cpp
0003     Project              : LabPlot
0004     Description          : NSL Tests for baseline functions
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2023 Stefan Gerlach <stefan.gerlach@uni.kn>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "NSLPeakTest.h"
0012 
0013 #include "backend/nsl/nsl_peak.h"
0014 
0015 #include <fstream>
0016 
0017 // ##############################################################################
0018 // #################  simple peak find
0019 // ##############################################################################
0020 
0021 void NSLPeakTest::testPeakSimple() {
0022     double data[] = {4., 2., 5., 2., 3., 1., 0, 1};
0023     const size_t N = 8;
0024     const size_t result[] = {0, 2, 4, 7};
0025 
0026     size_t np;
0027     size_t* indices = nsl_peak_detect(data, N, np);
0028     if (indices == nullptr) {
0029         WARN("Error getting peaks")
0030         return;
0031     }
0032 
0033     QCOMPARE(np, 4);
0034 
0035     for (size_t i = 0; i < np; i++)
0036         QCOMPARE(indices[i], result[i]);
0037     free(indices);
0038 }
0039 
0040 void NSLPeakTest::testPeakHeight() {
0041     double data[] = {4., 2., 5., 2., 3., 1., 0, 1};
0042     const size_t N = 8;
0043     const size_t result[] = {0, 2};
0044 
0045     size_t np;
0046     size_t* indices = nsl_peak_detect(data, N, np, 4.);
0047     if (!indices) {
0048         WARN("Error getting peaks")
0049         return;
0050     }
0051 
0052     QCOMPARE(np, 2);
0053 
0054     for (size_t i = 0; i < np; i++)
0055         QCOMPARE(indices[i], result[i]);
0056     free(indices);
0057 }
0058 
0059 void NSLPeakTest::testPeakDistance() {
0060     double data[] = {4., 2., 5., 2., 3., 1., 0, 1};
0061     const size_t N = 8;
0062     const size_t result[] = {0, 4, 7};
0063 
0064     size_t np;
0065     size_t* indices = nsl_peak_detect(data, N, np, 0., 3);
0066     if (!indices) {
0067         WARN("Error getting peaks")
0068         return;
0069     }
0070 
0071     QCOMPARE(np, 3);
0072 
0073     for (size_t i = 0; i < np; i++)
0074         QCOMPARE(indices[i], result[i]);
0075     free(indices);
0076 }
0077 
0078 void NSLPeakTest::testPeakHeightDistance() {
0079     double data[] = {4., 2., 5., 2., 3., 1., 0, 1};
0080     const size_t N = 8;
0081     const size_t result[] = {0, 4};
0082 
0083     size_t np;
0084     size_t* indices = nsl_peak_detect(data, N, np, 3., 3);
0085     if (!indices) {
0086         WARN("Error getting peaks")
0087         return;
0088     }
0089 
0090     QCOMPARE(np, 2);
0091 
0092     for (size_t i = 0; i < np; i++)
0093         QCOMPARE(indices[i], result[i]);
0094     free(indices);
0095 }
0096 
0097 /*void NSLPeakTest::testPeakX() {
0098     std::ifstream d(QFINDTESTDATA(QLatin1String("data/spectrum.dat")).toStdString());
0099     std::ifstream r(QFINDTESTDATA(QLatin1String("data/spectrum_arpls.dat")).toStdString());
0100     const size_t N = 1000;
0101 
0102     double data[N], result[N];
0103     for (size_t i = 0; i < N; i++) {
0104         d >> data[i];
0105         r >> result[i];
0106     }
0107 
0108     nsl_baseline_remove_arpls(data, N, 1.e-2, 1.e4, 10);
0109 
0110     for (size_t i = 0; i < N; ++i)
0111         FuzzyCompare(data[i], result[i], 2.e-5);
0112 }*/
0113 
0114 QTEST_MAIN(NSLPeakTest)