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