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