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