-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoints.go
More file actions
69 lines (59 loc) · 1.73 KB
/
points.go
File metadata and controls
69 lines (59 loc) · 1.73 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
package betterpath
import (
"fmt"
"sort"
)
// Point stores coordinates and timestamp of a point
type Point struct {
x float64
y float64
t int64
}
// Points stores the coordinates sorted by timestamp
type Points []Point
// Print outputs the updated dataset
func (points Points) Print() {
for _, point := range points {
fmt.Print(point.x, ",", point.y, ",", point.t, "\n")
}
}
// RemoveOutliers removes erroneous points by considering their distance
// to the standard deviation of distances
func (points *Points) RemoveOutliers() {
distances, zeroDistances := Distances(*points)
mean := Mean(distances, zeroDistances)
stdDeviation := StdDeviation(distances, mean, zeroDistances)
if stdDeviation == 0 {
// nothing to be done
return
}
// check the first point
for distances[0] > stdDeviation && distances[1] < stdDeviation {
// pop the first point
_, *points = (*points)[0], (*points)[1:]
_, distances = distances[0], distances[1:]
distances[0] = Distance((*points)[0], (*points)[1])
}
// check all the points
for i := 1; i < len(distances)-1; i++ {
if distances[i] > stdDeviation {
// remove the second point of the pair considered
*points = append((*points)[:i+1], (*points)[i+2:]...)
// remove the index of the distance considered
distances = append(distances[:i], distances[i+1:]...)
// update the array of distances with the distance of the new pair
distances[i] = Distance((*points)[i], (*points)[i+1])
i--
}
}
// check the last point
if distances[len(distances)-1] > stdDeviation {
*points = (*points)[:len(*points)-1]
}
}
// SortByTimestamp sorts points by timestsamp
func (points Points) SortByTimestamp() {
sort.SliceStable(points, func(i, j int) bool {
return points[i].t < points[j].t
})
}