-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.txt
More file actions
171 lines (149 loc) · 5.35 KB
/
code.txt
File metadata and controls
171 lines (149 loc) · 5.35 KB
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
int findNextSampleWithinRange(float *data1, float *data2,
int current_index, int last_index,
float thresholdLo1, float thresholdHi1,
float thresholdLo2, float thresholdHi2)
{
if (data2)
{
// loop while the current sample of either data1 or data2 is outside the range of interest
while (current_index <= last_index &&
((data1[current_index] <= thresholdLo1 || data1[current_index] >= thresholdHi1) ||
(data2[current_index] <= thresholdLo2 || data2[current_index] >= thresholdHi2)))
{
current_index++;
}
}
else
{
// loop while the current sample is outside the range of interest
while (current_index <= last_index &&
(data1[current_index] <= thresholdLo1 || data1[current_index] >= thresholdHi1))
{
current_index++;
}
}
return current_index;
}
int findNextSampleOutsideRange(float *data1, float *data2,
int current_index, int last_index,
float thresholdLo1, float thresholdHi1,
float thresholdLo2, float thresholdHi2)
{
if (data2)
{
// loop while the current sample of either data1 and data2 is within the range of interest
while (current_index <= last_index &&
data1[current_index] > thresholdLo1 && data1[current_index] < thresholdHi1 &&
data2[current_index] > thresholdLo2 && data2[current_index] < thresholdHi2)
{
current_index++;
}
}
else
{
// loop while the current sample is within the range of interest
while (current_index <= last_index &&
data1[current_index] > thresholdLo1 && data1[current_index] < thresholdHi1)
{
current_index++;
}
}
return current_index;
}
std::vector<std::pair<int,int>> performContinuitySearch(float *data1, float *data2,
int indexBegin, int indexEnd,
float thresholdLo1, float thresholdHi1,
float thresholdLo2, float thresholdHi2,
int winLength)
{
// we would probably do some input validation here in production code!
std::vector<std::pair<int, int>> vecContinuities;
// loop from indexBegin to indexEnd-winLength
// if we get within winLength samples from indexEnd, we no longer need to search
int i = indexBegin;
while (i <= indexEnd-winLength)
{
i = findNextSampleWithinRange(data1, data2, i, indexEnd-winLength,
thresholdLo1, thresholdHi1, thresholdLo2, thresholdHi2);
// store the index of the first sample within the range of interest
int start_index = i;
i = findNextSampleOutsideRange(data1, data2, i, indexEnd,
thresholdLo1, thresholdHi1, thresholdLo2, thresholdHi2);
// compute the number of consecutive samples within the range of interest
int length_of_continuity = i-start_index+1;
// if the continuity is long enough, then add it to our collection
if (length_of_continuity >= winLength) {
// Success: We can return the start index and the current index since we have
// counted the required number of samples
vecContinuities.push_back(std::make_pair(start_index, i-1));
}
}
// return any continuities that have been found
return vecContinuities;
}
int searchContinuityAboveValue(float *data, int indexBegin, int indexEnd, float threshold, int winLength)
{
std::vector<std::pair<int,int>> vecContinuities =
performContinuitySearch(data, NULL, indexBegin, indexEnd,
threshold, std::numeric_limits<double>::infinity(),
0.0, 0.0, winLength);
if (vecContinuities.empty())
return -1;
std::pair<int,int> pair = vecContinuities[0];
return pair.first;
}
int backSearchContinuityWithinRange(float *data, int indexBegin, int indexEnd, float thresholdLo, float thresholdHi, int winLength)
{
// swapped indexBegin and indexEnd indices because search is backward (indexBegin > indexEnd)
std::vector<std::pair<int,int>> vecContinuities =
performContinuitySearch(data, NULL, indexEnd, indexBegin,
thresholdLo, thresholdHi,
0.0, 0.0, winLength);
if (vecContinuities.empty())
return -1;
// return second index of last continuity pair
std::pair<int,int> pair = vecContinuities.back();
return pair.second;
}
int searchContinuityAboveValueTwoSignals(float *data1, float *data2, int indexBegin, int indexEnd, float threshold1, float threshold2, int winLength)
{
std::vector<std::pair<int,int>> vecContinuities =
performContinuitySearch(data1, data2, indexBegin, indexEnd,
threshold1, std::numeric_limits<double>::infinity(),
threshold2, std::numeric_limits<double>::infinity(), winLength);
if (vecContinuities.empty())
return -1;
std::pair<int,int> pair = vecContinuities[0];
return pair.first;
}
std::vector<std::pair<int, int>> searchMultiContinuityWithinRange(float *data,
int indexBegin, int indexEnd, float thresholdLo, float thresholdHi, int winLength)
{
return performContinuitySearch(data, NULL, indexBegin, indexEnd,
thresholdLo, thresholdHi, 0.0, 0.0, winLength);
}
// being that this code is being written in C, we could use a simple
// data structure such as the following to store our swing data:
struct SwingData {
int *timeStamp;
float *Ax;
float *Ay;
float *Az;
float *Wx;
float *Wy;
float *Wz;
};
// Then the functions above could be called as follows:
int indexBegin = 100;
int indexEnd = 1000;
float threshold = 10.0;
int winLength = 3;
int first_index = searchContinuityAboveValue(swing_data.Ax, indexBegin, indexEnd, threshold, winLength);
if (first_index == -1)
{
// Failed to find 'winLength' conectutive samples above the threshold!
}
else
{
// success!
}