-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnapLimitReconstructor_Old.py
executable file
·200 lines (171 loc) · 8.3 KB
/
SnapLimitReconstructor_Old.py
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
# .---------------------------------------------------------------------------.
# | |
# | S N A P L I M I T R E C O N S T R U C T O R |
# | |
# '---------------------------------------------------------------------------'
import pdb
import inspect
from copy import *
from enum import Enum
from Globals import *
from Vector import Vector
from Sample import Sample
from PredictionSample import PredictionSample
class SnapLimitReconstructor(object):
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# P U B L I C F U N C T I O N S
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def __init__(self):
# Data
self.rawSignal = []
self.reconstructedSignal = []
# Algorithm parameters
self.samplingInterval = 10
self.interpolationType = InterpolationType.Time
self.threshold = 60
self.heartbeatRate = 500
self.snapLimit = 0.5
def getReconstructedSignal(self, rawSignal=[], samplingInterval=10,
interpolationType=InterpolationType.Time,
threshold=60, heartbeatRate=500, snapLimit=0.5):
if isinstance( rawSignal, list ):
self.rawSignal = rawSignal
if isinstance( samplingInterval, int ) and samplingInterval > 0:
self.samplingInterval = samplingInterval
if isinstance( interpolationType, Enum ):
self.interpolationType = interpolationType
if (isinstance( threshold, float ) and threshold > 0) or \
(isinstance(threshold, int ) and threshold > 0):
self.threshold = threshold
if isinstance( heartbeatRate, int ) and heartbeatRate > 0:
self.heartbeatRate = heartbeatRate
if isinstance( snapLimit, float ) and snapLimit > 0:
self.snapLimit = snapLimit
self.pullDataFromPackets()
self.executeAlgorithm()
return self.reconstructedSignal
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# P R I V A T E F U N C T I O N S
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def pullDataFromPackets(self):
temp = []
for packet in self.rawSignal:
temp.append(packet.predictionSample)
self.rawSignal = temp
def executeAlgorithm(self):
self.reconstructedSignal = []
self.reconstructedSignal.append( self.findFirstSample() )
reconstructionTime = self.reconstructedSignal[0].time + self.samplingInterval
interpolationSample = PredictionSample(self.reconstructedSignal[0],
self.rawSignal[0].velocity)
targetSample = None
for index, predictionSample in enumerate(self.rawSignal[1:]):
currentTime = predictionSample.sample.time
if currentTime < reconstructionTime:
targetSample = None
interpolationSample = predictionSample
elif currentTime == reconstructionTime:
estimatedSample = self.estimateSample(interpolationSample,
reconstructionTime)
targetSample = self.findTarget(predictionSample)
interpolationSample = self.findSnapSample(predictionSample, estimatedSample, targetSample)
self.reconstructedSignal.append(deepcopy(interpolationSample.sample))
reconstructionTime += self.samplingInterval
elif currentTime > reconstructionTime:
while currentTime > reconstructionTime:
if targetSample != None and reconstructionTime >= targetSample.sample.time:
interpolationSample = targetSample
targetSample = None
estimatedSample = self.estimateSample(interpolationSample,
reconstructionTime)
self.reconstructedSignal.append(deepcopy(estimatedSample.sample))
reconstructionTime += self.samplingInterval
if currentTime < reconstructionTime:
targetSample = None
interpolationSample = predictionSample
elif currentTime == reconstructionTime:
estimatedSample = self.estimateSample(interpolationSample,
reconstructionTime)
targetSample = self.findTarget(predictionSample)
interpolationSample = self.findSnapSample(predictionSample, estimatedSample, targetSample)
self.reconstructedSignal.append(deepcopy(interpolationSample.sample))
reconstructionTime += self.samplingInterval
def findFirstSample(self):
timeDiff = self.rawSignal[0].sample.time % self.samplingInterval
if timeDiff == 0:
return deepcopy(self.rawSignal[0].sample)
else:
change = self.samplingInterval - timeDiff
newSample = Sample()
newSample.time = self.rawSignal[0].sample.time + change
newSample.position = deepcopy(self.rawSignal[0].sample.position)
return newSample
def findTarget(self, predictionSample):
if self.interpolationType == InterpolationType.Time:
return self.findTargetForTimeThreshold(predictionSample)
elif self.interpolationType == Interpolation.Distance:
return self.findTargetForDistanceThreshold(predictionSample)
def findTargetForTimeThreshold(self, predictionSample):
time = min(self.threshold, self.heartbeatRate)
targetSample = self.estimateSample(predictionSample, predictionSample.sample.time + time)
return targetSample
def findTargetForDistanceThreshold(self, predictionSample):
distance = 0
targetSample = None
time = predictionSample.sample.time
timeDiff = 0
while distance < self.threshold and timeDiff < self.heartbeatRate:
time += self.samplingInterval
timeDiff = time - predictionSample.sample.time
targetSample = self.estimateSample(predictionSample, time)
distance = predictionSample.sample.position.distance(target.sample.position)
return targetSample
def findInterpolationSample(self, currentSample, targetSample):
deltaPosition = targetSample.sample.position - \
currentSample.sample.position
deltaTime = targetSample.sample.time - \
currentSample.sample.time
invDeltaTimeVector = Vector( 1 / float(deltaTime), \
1 / float(deltaTime), \
1 / float(deltaTime))
velocity = deltaPosition * invDeltaTimeVector
interpolationSample = PredictionSample()
interpolationSample.sample = deepcopy(currentSample.sample)
interpolationSample.velocity = velocity
return interpolationSample
def findSnapSample(self, currentSample, estimatedSample, targetSample):
deltaPosition = targetSample.sample.position - \
currentSample.sample.position
deltaPosition.x *= self.snapLimit
deltaPosition.y *= self.snapLimit
deltaPosition.z *= self.snapLimit
snapPosition = currentSample.sample.position + deltaPosition
deltaPosition = targetSample.sample.position - snapPosition
deltaTime = targetSample.sample.time - \
currentSample.sample.time
invDeltaTimeVector = Vector( 1 / float(deltaTime), \
1 / float(deltaTime), \
1 / float(deltaTime))
velocity = deltaPosition * invDeltaTimeVector
snapSample = PredictionSample()
snapSample.sample = Sample(currentSample.sample.time, snapPosition)
snapSample.velocity = velocity
return snapSample
def estimateSample(self, interpolationSample, time):
estimatedSample = PredictionSample()
estimatedSample.sample.time = time
estimatedSample.sample.position = self.calculatePosition(interpolationSample, time)
estimatedSample.velocity = deepcopy(interpolationSample.velocity)
return estimatedSample
def calculatePosition(self, interpolationSample, time):
deltaTime = time - interpolationSample.sample.time
if deltaTime < 0:
print "Error at: " + str(interpolationSample.sample.time) + " " + str(time)
return deepcopy(interpolationSample.sample.position)
elif deltaTime == 0:
return deepcopy(interpolationSample.sample.position)
else:
deltaTimeVector = Vector(deltaTime, deltaTime, deltaTime)
deltaPosition = interpolationSample.velocity * deltaTimeVector
estimatedPosition = interpolationSample.sample.position + deltaPosition
return estimatedPosition