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