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], correlations [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  correlations);
104 
105  QVERIFY (binStartMax = INDEX_SHIFT);
106 }
107 
108 void TestCorrelation::testShiftSinusoidPowerOf2 ()
109 {
110  const int N = 1024; // Power of 2
111  const int INDEX_MAX = 200, INDEX_SHIFT = 50;
112 
113  int binStartMax;
114  double function1 [N], function2 [N], correlations [N];
115  double corrMax;
116 
117  Correlation correlation (N);
118 
119  // Function1 peak is at INDEX_MAX
120  // Function2 peak is at INDEX_MAX + INDEX_SHIFT
121  loadSinusoid (function1, N, INDEX_MAX);
122  loadSinusoid (function2, N, INDEX_MAX + INDEX_SHIFT);
123 
124  correlation.correlateWithShift (N,
125  function1,
126  function2,
127  binStartMax,
128  corrMax,
129  correlations);
130 
131  QVERIFY (binStartMax = INDEX_SHIFT);
132 }
133 
134 void TestCorrelation::testShiftThreeTrianglesNonPowerOf2 ()
135 {
136  const int N = 1000; // Non power of 2
137  const int INDEX_MAX = 200, INDEX_SHIFT = 50;
138 
139  int binStartMax;
140  double function1 [N], function2 [N], correlations [N];
141  double corrMax;
142 
143  Correlation correlation (N);
144 
145  // Function1 peak is at INDEX_MAX
146  // Function2 peak is at INDEX_MAX + INDEX_SHIFT
147  loadThreeTriangles (function1, N, INDEX_MAX);
148  loadThreeTriangles (function2, N, INDEX_MAX + INDEX_SHIFT);
149 
150  correlation.correlateWithShift (N,
151  function1,
152  function2,
153  binStartMax,
154  corrMax,
155  correlations);
156 
157  QVERIFY (binStartMax = INDEX_SHIFT);
158 }
159 
160 void TestCorrelation::testShiftThreeTrianglesPowerOf2 ()
161 {
162  const int N = 1024; // Power of 2
163  const int INDEX_MAX = 200, INDEX_SHIFT = 50;
164 
165  int binStartMax;
166  double function1 [N], function2 [N], correlations [N];
167  double corrMax;
168 
169  Correlation correlation (N);
170 
171  // Function1 peak is at INDEX_MAX
172  // Function2 peak is at INDEX_MAX + INDEX_SHIFT
173  loadThreeTriangles (function1, N, INDEX_MAX);
174  loadThreeTriangles (function2, N, INDEX_MAX + INDEX_SHIFT);
175 
176  correlation.correlateWithShift (N,
177  function1,
178  function2,
179  binStartMax,
180  corrMax,
181  correlations);
182 
183  QVERIFY (binStartMax = INDEX_SHIFT);
184 }
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:60