00001 #include "Correlation.h"
00002 #include "Logger.h"
00003 #include "MainWindow.h"
00004 #include <qmath.h>
00005 #include <QStringList>
00006 #include <QtTest/QtTest>
00007 #include "Test/TestCorrelation.h"
00008
00009 QTEST_MAIN (TestCorrelation)
00010
00011 TestCorrelation::TestCorrelation(QObject *parent) :
00012 QObject(parent)
00013 {
00014 }
00015
00016 void TestCorrelation::cleanupTestCase ()
00017 {
00018 }
00019
00020 void TestCorrelation::initTestCase ()
00021 {
00022 const QString NO_ERROR_REPORT_LOG_FILE;
00023 const QString NO_REGRESSION_OPEN_FILE;
00024 const bool NO_GNUPLOT_LOG_FILES = false;
00025 const bool NO_REGRESSION_IMPORT = false;
00026 const bool NO_RESET = false;
00027 const bool DEBUG_FLAG = false;
00028 const QStringList NO_LOAD_STARTUP_FILES;
00029
00030 initializeLogging ("engauge_test",
00031 "engauge_test.log",
00032 DEBUG_FLAG);
00033
00034 MainWindow w (NO_ERROR_REPORT_LOG_FILE,
00035 NO_REGRESSION_OPEN_FILE,
00036 NO_GNUPLOT_LOG_FILES,
00037 NO_RESET,
00038 NO_REGRESSION_IMPORT,
00039 NO_LOAD_STARTUP_FILES);
00040 w.show ();
00041 }
00042
00043 void TestCorrelation::loadSinusoid (double function [],
00044 int n,
00045 int center) const
00046 {
00047 for (int i = 0; i < n; i++) {
00048 int x = i - center;
00049 if (x == 0) {
00050 function [i] = 1.0;
00051 } else {
00052 function [i] = qSin (x) / x;
00053 }
00054 }
00055 }
00056
00057 void TestCorrelation::loadThreeTriangles (double function [],
00058 int n,
00059 int center) const
00060 {
00061 const int PEAK_SEPARATION = 50, PEAK_HALF_WIDTH = 5;
00062
00063 int x;
00064 for (int i = 0; i < n; i++) {
00065
00066
00067 x = i - center;
00068 if (x > PEAK_HALF_WIDTH) {
00069
00070
00071 x = i - (center - PEAK_SEPARATION);
00072 if (x > PEAK_HALF_WIDTH) {
00073
00074
00075 x = i - (center + PEAK_SEPARATION);
00076 }
00077 }
00078
00079 if (x < PEAK_HALF_WIDTH) {
00080
00081
00082 function [i] = (double) (PEAK_HALF_WIDTH - x) / (double) PEAK_HALF_WIDTH;
00083
00084 } else {
00085
00086 function [i] = 0;
00087 }
00088 }
00089 }
00090
00091 void TestCorrelation::testShiftSinusoidNonPowerOf2 ()
00092 {
00093 const int N = 1000;
00094 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
00095
00096 int binStartMax;
00097 double function1 [N], function2 [N], correlations [N];
00098 double corrMax;
00099
00100 Correlation correlation (N);
00101
00102
00103
00104 loadSinusoid (function1, N, INDEX_MAX);
00105 loadSinusoid (function2, N, INDEX_MAX + INDEX_SHIFT);
00106
00107 correlation.correlateWithShift (N,
00108 function1,
00109 function2,
00110 binStartMax,
00111 corrMax,
00112 correlations);
00113
00114 QVERIFY ((binStartMax = INDEX_SHIFT));
00115 }
00116
00117 void TestCorrelation::testShiftSinusoidPowerOf2 ()
00118 {
00119 const int N = 1024;
00120 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
00121
00122 int binStartMax;
00123 double function1 [N], function2 [N], correlations [N];
00124 double corrMax;
00125
00126 Correlation correlation (N);
00127
00128
00129
00130 loadSinusoid (function1, N, INDEX_MAX);
00131 loadSinusoid (function2, N, INDEX_MAX + INDEX_SHIFT);
00132
00133 correlation.correlateWithShift (N,
00134 function1,
00135 function2,
00136 binStartMax,
00137 corrMax,
00138 correlations);
00139
00140 QVERIFY ((binStartMax = INDEX_SHIFT));
00141 }
00142
00143 void TestCorrelation::testShiftThreeTrianglesNonPowerOf2 ()
00144 {
00145 const int N = 1000;
00146 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
00147
00148 int binStartMax;
00149 double function1 [N], function2 [N], correlations [N];
00150 double corrMax;
00151
00152 Correlation correlation (N);
00153
00154
00155
00156 loadThreeTriangles (function1, N, INDEX_MAX);
00157 loadThreeTriangles (function2, N, INDEX_MAX + INDEX_SHIFT);
00158
00159 correlation.correlateWithShift (N,
00160 function1,
00161 function2,
00162 binStartMax,
00163 corrMax,
00164 correlations);
00165
00166 QVERIFY ((binStartMax = INDEX_SHIFT));
00167 }
00168
00169 void TestCorrelation::testShiftThreeTrianglesPowerOf2 ()
00170 {
00171 const int N = 1024;
00172 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
00173
00174 int binStartMax;
00175 double function1 [N], function2 [N], correlations [N];
00176 double corrMax;
00177
00178 Correlation correlation (N);
00179
00180
00181
00182 loadThreeTriangles (function1, N, INDEX_MAX);
00183 loadThreeTriangles (function2, N, INDEX_MAX + INDEX_SHIFT);
00184
00185 correlation.correlateWithShift (N,
00186 function1,
00187 function2,
00188 binStartMax,
00189 corrMax,
00190 correlations);
00191
00192 QVERIFY ((binStartMax = INDEX_SHIFT));
00193 }