-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswift_code.txt
More file actions
213 lines (174 loc) · 7.59 KB
/
swift_code.txt
File metadata and controls
213 lines (174 loc) · 7.59 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
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
// struct to hold a continuity pair
struct ContinuityPair {
var first: Int = -1
var second: Int = -1
}
//////////////////////
// Helper functions //
//////////////////////
func findNextSampleWithinRange(data1: [Float], data2: [Float], currentIndex: Int, lastIndex: Int,
thresholdLo1: Float, thresholdHi1: Float, thresholdLo2: Float, thresholdHi2: Float) -> Int
{
var startIndex = currentIndex;
if (!data2.isEmpty)
{
// loop while the current sample of either data1 or data2 is outside the range of interest
while (startIndex <= lastIndex &&
((data1[startIndex] <= thresholdLo1 || data1[startIndex] >= thresholdHi1) ||
(data2[startIndex] <= thresholdLo2 || data2[startIndex] >= thresholdHi2)))
{
startIndex += 1
}
}
else
{
// loop while the current sample is outside the range of interest
while (startIndex <= lastIndex &&
(data1[startIndex] <= thresholdLo1 || data1[startIndex] >= thresholdHi1))
{
startIndex += 1
}
}
return startIndex
}
func findNextSampleOutsideRange(data1: [Float], data2: [Float], currentIndex: Int, lastIndex: Int,
thresholdLo1: Float, thresholdHi1: Float, thresholdLo2: Float, thresholdHi2: Float) -> Int
{
var startIndex = currentIndex;
if (!data2.isEmpty)
{
// loop while the current sample of either data1 and data2 is within the range of interest
while (startIndex <= lastIndex &&
data1[startIndex] > thresholdLo1 && data1[startIndex] < thresholdHi1 &&
data2[startIndex] > thresholdLo2 && data2[startIndex] < thresholdHi2)
{
startIndex += 1
}
}
else
{
// loop while the current sample is within the range of interest
while (startIndex <= lastIndex &&
data1[startIndex] > thresholdLo1 && data1[startIndex] < thresholdHi1)
{
startIndex += 1
}
}
return startIndex
}
func performContinuitySearch(data1: [Float], data2: [Float], indexBegin: Int, indexEnd: Int,
thresholdLo1: Float, thresholdHi1: Float, thresholdLo2: Float, thresholdHi2: Float, winLength: Int) -> [ContinuityPair]
{
// we would probably do some input validation here in production code!
if (winLength < 1) {
return []
}
var vContinuities: [ContinuityPair] = []
// loop from indexBegin to indexEnd-winLength
// if we get within winLength samples from indexEnd, we no longer need to search
var i = indexBegin
while (i <= indexEnd-winLength)
{
i = findNextSampleWithinRange(data1: data1, data2: data2, currentIndex: i, lastIndex: indexEnd-winLength,
thresholdLo1: thresholdLo1, thresholdHi1: thresholdHi1, thresholdLo2: thresholdLo2, thresholdHi2: thresholdHi2)
// store the index of the first sample within the range of interest
let startIndex = i
i = findNextSampleOutsideRange(data1: data1, data2: data2, currentIndex: i, lastIndex: indexEnd,
thresholdLo1: thresholdLo1, thresholdHi1: thresholdHi1, thresholdLo2: thresholdLo2, thresholdHi2: thresholdHi2)
// compute the number of consecutive samples within the range of interest
let length_of_continuity = i-startIndex
// 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
let pair: ContinuityPair = ContinuityPair(first: startIndex, second: i-1)
vContinuities.append(pair)
}
}
// return any continuities that have been found
return vContinuities
}
//////////////////////
// Public functions //
//////////////////////
func searchContinuityAboveValue(data: [Float], indexBegin: Int, indexEnd: Int, threshold: Float, winLength: Int) -> Int
{
if (winLength < 1) {
return -1
}
let data2: [Float] = []
let vContinuities = performContinuitySearch(data1: data, data2: data2, indexBegin: indexBegin, indexEnd: indexEnd,
thresholdLo1: threshold, thresholdHi1: Float.infinity, thresholdLo2: 0.0, thresholdHi2: 0.0, winLength: winLength)
if (vContinuities.isEmpty) {
return -1
}
let pair: ContinuityPair = vContinuities[0]
return pair.first
}
func backSearchContinuityWithinRange(data: [Float], indexBegin: Int, indexEnd: Int, thresholdLo: Float, thresholdHi: Float, winLength: Int) -> Int
{
if (winLength < 1) {
return -1
}
let data2: [Float] = []
let vContinuities = performContinuitySearch(data1: data, data2: data2, indexBegin: indexBegin, indexEnd: indexEnd,
thresholdLo1: thresholdLo, thresholdHi1: thresholdHi, thresholdLo2: 0.0, thresholdHi2: 0.0, winLength: winLength)
if (vContinuities.isEmpty) {
return -1
}
// return second index of last continuity pair
let pair: ContinuityPair = vContinuities[vContinuities.count-1]
return pair.second
}
func searchContinuityAboveValueTwoSignals(data1: [Float], data2: [Float], indexBegin: Int, indexEnd: Int,
threshold1: Float, threshold2: Float, winLength: Int) -> Int
{
if (winLength < 1) {
return -1
}
let vContinuities = performContinuitySearch(data1: data1, data2: data2, indexBegin: indexBegin, indexEnd: indexEnd,
thresholdLo1: threshold1, thresholdHi1: Float.infinity, thresholdLo2: threshold2, thresholdHi2: Float.infinity, winLength: winLength)
if (vContinuities.isEmpty) {
return -1
}
let pair: ContinuityPair = vContinuities[0]
return pair.first
}
func searchMultiContinuityWithinRange(data: [Float], indexBegin: Int, indexEnd: Int,
thresholdLo: Float, thresholdHi: Float, winLength: Int) -> [ContinuityPair]
{
if (winLength < 1) {
return []
}
let data2: [Float] = []
return performContinuitySearch(data1: data, data2: data2, indexBegin: indexBegin, indexEnd: indexEnd,
thresholdLo1: thresholdLo, thresholdHi1: thresholdHi, thresholdLo2: 0.0, thresholdHi2: 0.0, winLength: winLength);
}
///////////////////
// Test function //
///////////////////
func test()
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
let data1: [Float] = [0.0, 1.0, 2.0, 2.0, 2.0, 1.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 2.0, 3.0, 2.0, 4.0, 5.0, 4.0, -1.0, 3.0]
let data2: [Float] = [0.0, 1.0, 2.0, 2.0, 2.0, 1.0, 0.0, 0.0, 0.0, 2.0, 1.0, 1.0, 1.0, 2.0, 1.0, 2.0, 4.0, 5.0, 4.0, -1.0, 3.0]
let indexBegin: Int = 0
let indexEnd: Int = data1.count-1
let thresholdLo: Float = 0.0
let thresholdHi: Float = 15.0
let winLength: Int = 9
let firstIndex = searchContinuityAboveValue(data: data1, indexBegin: indexBegin, indexEnd: indexEnd, threshold: thresholdLo, winLength: winLength)
print("firstIndex = ", firstIndex)
let lastIndex = backSearchContinuityWithinRange(data: data1, indexBegin: indexBegin, indexEnd: indexEnd, thresholdLo: thresholdLo, thresholdHi: thresholdHi, winLength: winLength)
print("lastIndex = ", lastIndex)
let firstIndex_dual = searchContinuityAboveValueTwoSignals(data1: data1, data2: data2, indexBegin: indexBegin, indexEnd: indexEnd, threshold1: thresholdLo, threshold2: thresholdLo, winLength: winLength)
print("firstIndex_dual = ", firstIndex_dual)
let pairs: [ContinuityPair] = searchMultiContinuityWithinRange(data: data1, indexBegin: indexBegin, indexEnd: indexEnd, thresholdLo: thresholdLo, thresholdHi: thresholdHi, winLength: winLength)
print("continuity pairs = ", pairs.count)
for pair in pairs {
print("pair.first = ", pair.first)
print("pair.second = ", pair.second)
}
}
// Call the test function
test()