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