Engauge Digitizer 2
Loading...
Searching...
No Matches
TestCorrelation.cpp
1#include "Correlation.h"
2#include "Logger.h"
3#include "MainWindow.h"
4#include <qmath.h>
5#include <QStringList>
6#include <QtTest/QtTest>
7#include "Test/TestCorrelation.h"
8
9QTEST_MAIN (TestCorrelation)
10
12 QObject(parent)
13{
14}
15
16void TestCorrelation::cleanupTestCase ()
17{
18}
19
20void TestCorrelation::initTestCase ()
21{
22 const QString NO_ERROR_REPORT_LOG_FILE;
23 const QString NO_REGRESSION_OPEN_FILE;
24 const bool NO_GNUPLOT_LOG_FILES = false;
25 const bool NO_REGRESSION_IMPORT = false;
26 const bool DEBUG_FLAG = false;
27 const QStringList NO_LOAD_STARTUP_FILES;
28
29 initializeLogging ("engauge_test",
30 "engauge_test.log",
31 DEBUG_FLAG);
32
33 MainWindow w (NO_ERROR_REPORT_LOG_FILE,
34 NO_REGRESSION_OPEN_FILE,
35 NO_GNUPLOT_LOG_FILES,
36 NO_REGRESSION_IMPORT,
37 NO_LOAD_STARTUP_FILES);
38 w.show ();
39}
40
41void TestCorrelation::loadSinusoid (double function [],
42 int n,
43 int center) const
44{
45 for (int i = 0; i < n; i++) {
46 int x = i - center;
47 if (x == 0) {
48 function [i] = 1.0;
49 } else {
50 function [i] = qSin (x) / x;
51 }
52 }
53}
54
55void TestCorrelation::loadThreeTriangles (double function [],
56 int n,
57 int center) const
58{
59 const int PEAK_SEPARATION = 50, PEAK_HALF_WIDTH = 5;
60
61 int x;
62 for (int i = 0; i < n; i++) {
63
64 // First try for peak at center
65 x = i - center;
66 if (x > PEAK_HALF_WIDTH) {
67
68 // Failed, so try again for peak at center-separation
69 x = i - (center - PEAK_SEPARATION);
70 if (x > PEAK_HALF_WIDTH) {
71
72 // Failed, so try again for peak at center+separation
73 x = i - (center + PEAK_SEPARATION);
74 }
75 }
76
77 if (x < PEAK_HALF_WIDTH) {
78
79 // Map 0<x<PEAK_HALF_WIDTH to 1<function<0
80 function [i] = (double) (PEAK_HALF_WIDTH - x) / (double) PEAK_HALF_WIDTH;
81
82 } else {
83
84 function [i] = 0;
85 }
86 }
87}
88
89void TestCorrelation::testShiftSinusoidNonPowerOf2 ()
90{
91 const int N = 1000; // Non power of 2
92 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
93
94 int binStartMax;
95 double function1 [N], function2 [N], correlations [N];
96 double corrMax;
97
98 Correlation correlation (N);
99
100 // Function1 peak is at INDEX_MAX
101 // Function2 peak is at INDEX_MAX + INDEX_SHIFT
102 loadSinusoid (function1, N, INDEX_MAX);
103 loadSinusoid (function2, N, INDEX_MAX + INDEX_SHIFT);
104
105 correlation.correlateWithShift (N,
106 function1,
107 function2,
108 binStartMax,
109 corrMax,
110 correlations);
111
112 QVERIFY (binStartMax = INDEX_SHIFT);
113}
114
115void TestCorrelation::testShiftSinusoidPowerOf2 ()
116{
117 const int N = 1024; // Power of 2
118 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
119
120 int binStartMax;
121 double function1 [N], function2 [N], correlations [N];
122 double corrMax;
123
124 Correlation correlation (N);
125
126 // Function1 peak is at INDEX_MAX
127 // Function2 peak is at INDEX_MAX + INDEX_SHIFT
128 loadSinusoid (function1, N, INDEX_MAX);
129 loadSinusoid (function2, N, INDEX_MAX + INDEX_SHIFT);
130
131 correlation.correlateWithShift (N,
132 function1,
133 function2,
134 binStartMax,
135 corrMax,
136 correlations);
137
138 QVERIFY (binStartMax = INDEX_SHIFT);
139}
140
141void TestCorrelation::testShiftThreeTrianglesNonPowerOf2 ()
142{
143 const int N = 1000; // Non power of 2
144 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
145
146 int binStartMax;
147 double function1 [N], function2 [N], correlations [N];
148 double corrMax;
149
150 Correlation correlation (N);
151
152 // Function1 peak is at INDEX_MAX
153 // Function2 peak is at INDEX_MAX + INDEX_SHIFT
154 loadThreeTriangles (function1, N, INDEX_MAX);
155 loadThreeTriangles (function2, N, INDEX_MAX + INDEX_SHIFT);
156
157 correlation.correlateWithShift (N,
158 function1,
159 function2,
160 binStartMax,
161 corrMax,
162 correlations);
163
164 QVERIFY (binStartMax = INDEX_SHIFT);
165}
166
167void TestCorrelation::testShiftThreeTrianglesPowerOf2 ()
168{
169 const int N = 1024; // Power of 2
170 const int INDEX_MAX = 200, INDEX_SHIFT = 50;
171
172 int binStartMax;
173 double function1 [N], function2 [N], correlations [N];
174 double corrMax;
175
176 Correlation correlation (N);
177
178 // Function1 peak is at INDEX_MAX
179 // Function2 peak is at INDEX_MAX + INDEX_SHIFT
180 loadThreeTriangles (function1, N, INDEX_MAX);
181 loadThreeTriangles (function2, N, INDEX_MAX + INDEX_SHIFT);
182
183 correlation.correlateWithShift (N,
184 function1,
185 function2,
186 binStartMax,
187 corrMax,
188 correlations);
189
190 QVERIFY (binStartMax = INDEX_SHIFT);
191}
Fast cross correlation between two functions.
Definition Correlation.h:15
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition MainWindow.h:78
Unit tests of fast correlation algorithm.