-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal.cpp
125 lines (98 loc) · 2.34 KB
/
Signal.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
#include "Signal.h"
//Given a filename, constructs a signal that is normalized and concatenated
Signal::Signal(string filename)
{
vector< vector<double> > input;
vector< vector<double> > data;
ifstream inFile(filename.c_str());
//Make sure file is good
if(!inFile){
throw invalid_argument("Failed to open file.");
} else {
double val = 0;
int i = 0;
vector<double> tempInput;
//Reading the values from the file
while(!inFile.eof())
{
inFile >> val;
tempInput.push_back(val);
if(i==NUM_DATA_VALS-1) {
input.push_back(tempInput);
tempInput.clear();
i = 0;
} else {
i++;
}
}
inFile.close();
//Taking all the input data and grouping it within its own type
for(unsigned int i=0; i<input.at(0).size(); i++)
{
vector<double> temp;
for(unsigned int j=0; j<input.size(); j++){
temp.push_back(input.at(j).at(i));
}
data.push_back(temp);
}
normalize(data);
concatenate(data);
}
}
//Copy Constructor
Signal::Signal(const Signal& copy)
{
signalData = copy.signalData;
}
//Gives you the data for a given Signal
vector<double> Signal::getSignalData()
{
return signalData;
}
//Normalizes the signal data
void Signal::normalize(vector< vector<double> >& data)
{
for(unsigned int k=0; k<data.size(); k++){
double sum = 0;
//Find the min and max
for(unsigned int i=0; i<data.at(0).size(); i++){
sum+=pow(data[k][i], 2);
}
double length = sqrt(sum);
//Normalize all the elements
for(unsigned int i=0; i<data.at(0).size(); i++){
if(length!=0)
data[k][i] = data[k][i]/length;
}
}
}
//Concatenates all the signal data, making the final single vector
void Signal::concatenate(vector< vector<double> >& data)
{
for(unsigned int i=0; i<data.size(); i++){
for(unsigned int k=0; k<data.at(0).size(); k++){
signalData.push_back(data[i][k]);
}
}
}
//Given two signals, determines if they are equal to each other.
bool Signal::operator==(const Signal& other) const
{
if(other.signalData.size()!=this->signalData.size()){
return false;
} else {
for(unsigned int i=0; i<other.signalData.size(); i++){
if(other.signalData[i]!=this->signalData[i])
return false;
}
return true;
}
}
//Given two signals, determines if they are not equal to each other.
bool Signal::operator!=(const Signal& other) const
{
if(*this==other){
return false;
}
return true;
}