-
Notifications
You must be signed in to change notification settings - Fork 3
/
rearrange.go
69 lines (55 loc) · 1.4 KB
/
rearrange.go
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 edf aims to implement an interface to the EDF+ standard as described
on http://www.edfplus.info/ for the Go language. The objective is to read a
file and provide some kind of access to the stored data stored. It was written
by Cristiano Silva Jr. while working at the Laboratory of Neuroscience and
Behavior from the University of Brasilia and was released under the GPL license.
*/
package edf
import "math"
func elvis(pred bool, a int16, b int16) int16 {
if pred {
return a
}
return b
}
func identifyOverflow(inlet []int16) []int16 {
var last int16
limit := len(inlet)
outlet := make([]int16, limit)
for i := 0; i < limit; i++ {
it := inlet[i]
diff := float64(it) - float64(last)
if math.Abs(diff) > 16000 { // half int16 is a hell of a diff
outlet[i] = elvis(diff > 0, 500, -500)
} else {
outlet[i] = 0
}
last = it
}
return outlet
}
func rearrange(inlet []int16) []int16 {
limit := len(inlet)
overflow := identifyOverflow(inlet)
midlet := make([]int, limit)
step := int(math.Pow(2, 15)) - 1
factor := 0
for i := 0; i < limit; i++ {
if overflow[i] < 0 {
factor += step
} else if overflow[i] > 0 {
factor -= step
}
midlet[i] = int(inlet[i]) + factor
}
return convert(midlet)
}
func convert(midlet []int) []int16 {
limit := len(midlet)
outlet := make([]int16, limit)
for i := 0; i < limit; i++ {
outlet[i] = int16(midlet[i] / 2.0)
}
return outlet
}