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