-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormalized_crosscorrelation.cpp
345 lines (293 loc) · 8.69 KB
/
normalized_crosscorrelation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <numeric>
#include <math.h>
#include <algorithm>
using namespace std;
// This function returns a boolean whether the input
// is a valid input without extra characters
bool isDouble(string input, double& number)
{
stringstream ss(input);
ss >> number;
// If the stringstream is indeed a double with no extra characters
// And is at the end of the line
if (!ss.fail() && ss.eof())
{
return true;
}
else
{
return false;
}
}
//readFile is responsible for obtaining the starting
//index the input file which is stored in startingIndex
//readFile also creates a vector containing all the inputs
double readFile(string filename, vector<double>& inputData,
bool& error)
{
ifstream f;
string line;
string extraTest;
double valid;
double startingIndex = 0;
error = false;
f.open(filename);
// If file not found, prompt the user to re-type
//the filename
if (!f)
{
cerr << "File not found. Please input a valid filename.\n";
error = true;
return 0;
}
// Read first line
getline(f, line);
stringstream ss;
// Assign the first line to ss
ss << line;
// Get first entity (separated by spaces of first line)
// and assign it to startIndex
// Code below does the checking too if first entity is invalid
ss >> extraTest;
if (isDouble(extraTest, valid))
{
startingIndex = valid;
}
else // Prompts the user to check the file and re-input.
{
cout << "\nInvalid input. Please check your input file." << endl;
cout << "Here is the invalid line: " << line << endl;
// Here is the invalid line outputs empty space if file is empty.
cout << "Check your file and input the filename again." << endl;
error = true;
return 0;
}
// If there is still another element
// ie input 1 input 2 (e.g. -3 5)
if ((ss >> valid))
{
inputData.push_back(valid);
}
// If none, then the first value is already
// the first datapoint and startingIndex = 0
else
{
inputData.push_back(startingIndex);
startingIndex = 0;
}
// Read succeeding lines until EOF is reached
while (getline(f, line))
{
stringstream ss;
ss << line;
if (ss >> valid)
{
inputData.push_back(valid);
}
}
f.close();
return startingIndex;
}
// showData prints out all the elements in a vector
// range-based for loop
// in case the user wants to see the data read into each vector
void showData(vector<double> data)
{
for (double dataPoint : data)
{
cout << dataPoint << endl;
}
}
// average is responsible for taking the averages of the elements
// in a given vector. This is used to refine the input signal vector
double average(vector<double> data)
{
double ave;
ave = accumulate(data.begin(), data.end(), 0.0)/data.size();
return ave;
}
// subtractAverages subtracts the average
// from the ith vector element.
vector<double> subtractAverages(vector<double> inputData)
{
vector<double> newData;
double ave;
ave = average(inputData);
for (int i = 0; i < inputData.size(); i++)
{
newData.push_back(inputData[i] - ave);
}
return newData;
}
// autocorrelation takes the crosscorrelation
// of the input signal vector to itself while letting l = 0
double autocorrelation(vector<double> inputData)
{
double sum = 0;
for (int i = 0; i < inputData.size(); i++)
{
sum += inputData[i] * inputData[i];
}
return sum;
}
// contains returns true if a value is contained in the vector
// for instance, if we have vector y and say y[-100] as the element,
// contains will return false because there is the value at y[-100]
// which will be a random number and is not in y.
// We use this to determine whether a certain matching of
// x[n] * y[n-shift] is valid.
bool contains(vector<double> vec, double &elem)
{
bool result = false;
if (find(vec.begin(), vec.end(), elem) != vec.end())
{
result = true;
}
return result;
}
// crossCorrelation responsible for creating the array
// of possible crosscorrelation values given two signals
vector <double> crossCorrelation(vector<double> x, vector<double> y,
int startX, int startY, int shift, int &outputStartIndex)
{
vector <double> Z;
int endX = startX + x.size() - 1; //end index of x
int endY = startY + y.size() - 1; //end index of y
// start index of cross correlated signal
int ccStartIndex = startX - endY;
// end index of cross correlated signal
int ccEndIndex = endX - startY;
int duration = ccEndIndex - ccStartIndex + 1;
double z;
for (shift = ccStartIndex; shift <= ccEndIndex; shift++)
{
z = 0;
for (int i = 0; i < x.size(); i++)
{
if (startX < startY || startX == startY)
{
if ((contains(y, y[i - (startY - startX) - shift]) == 1))
{
z += x[i] * y[i - (startY - startX) - shift];
}
else
{
continue;
}
}
else if (startX > startY)
{
if ((contains(y, y[i + (startX - startY) - shift]) == 1))
{
z += x[i] * y[i + (startX - startY) - shift];
}
else
{
continue;
}
}
}
Z.push_back(z);
}
outputStartIndex = ccStartIndex;
return Z;
}
// normalizationCoefficients returns the normalization
// coefficient needed for normalizing the resulting crosscorrelation;
// reducing them into the values that range from -1 to 1
double normalizationCoefficient(vector<double> x, vector<double> y)
{
return sqrt(autocorrelation(x)*autocorrelation(y));
}
// normalizedCrosscorrelation outputs the resulting vector
// containing all the possible normalized crosscorrelation
// values for a given pair of signal vectors
vector <double> normalizedCrosscorrelation(double normCoef,
vector <double> crosscorreVec)
{
vector <double> normVec;
for (double dataPoint : crosscorreVec)
{
normVec.push_back(dataPoint / normCoef);
}
return normVec;
}
// outputFile writes the values from the normalizedCrosscorrelation
// vector to a given filename in .txt format
void outputFile(string filename, int startIndex,
vector<double> outputData)
{
ofstream f;
cout << endl;
cout << "Program is saving your output to: " << filename;
f.open(filename);
if (startIndex != 0) // To copy input format of (index) (data_1)
{
f << startIndex << ' ';
}
for (double output : outputData)
{
f << output << endl;
}
f.close();
}
// main function
int main()
{
vector <double> xRaw;
vector <double> yRaw;
bool error = false;
int startX, startY;
int shift;
int outputStartIndex;
double z;
do
{
string xFilename;
cout << "\nPlease input filename for x: ";
cin >> xFilename;
startX = readFile(xFilename, xRaw, error);
}
while (error == true);
do
{
string yFilename;
cout << "\nPlease input filename for y: ";
cin >> yFilename;
startY = readFile(yFilename, yRaw, error);
}
while (error == true);
string outputFilename;
cout << "\nPlease input your desired output filename for the ";
cout << "\nnormalized cross-correlated signals: ";
cin >> outputFilename;
vector <double> xSubtractedAverages = subtractAverages(xRaw);
vector <double> ySubtractedAverages = subtractAverages(yRaw);
cout << endl << "Input raw X data: " << endl;
showData(xRaw);
cout << endl << "Input X data with subtracted averages: " << endl;
showData(xSubtractedAverages);
cout << endl << "Input raw Y data: " << endl;
showData(yRaw);
cout << endl << "Input Y data with subtracted averages: " << endl;
showData(ySubtractedAverages);
double autocorrelationX = autocorrelation(xSubtractedAverages);
double autocorrelationY = autocorrelation(ySubtractedAverages);
double normCoef = normalizationCoefficient(xSubtractedAverages,
ySubtractedAverages);
cout << "\nAutocorrelation of X is: " << autocorrelationX << endl;
cout << "Autocorrelation of Y is: " << autocorrelationY << endl;
cout << "Normalization coefficient is: " << normCoef << endl;
vector <double> result = crossCorrelation(xSubtractedAverages,
ySubtractedAverages, startX, startY, shift, outputStartIndex);
vector <double> normVec = normalizedCrosscorrelation(normCoef,
result);
cout << "\nNormalized cross correlated signals starting at index ";
cout << outputStartIndex << endl;
showData(normVec);
outputFile(outputFilename, outputStartIndex, normVec);
}