-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
107 lines (91 loc) · 1.58 KB
/
util.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
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
// Copyright 2018 Scott Cotton. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package hilbert
import (
"math"
snd "zikichombo.org/sound"
)
type pad struct {
snd.Source
n int
}
func (p *pad) Samples(dst []float64) (int, error) {
i := 0
for i < p.n && i < len(dst) {
dst[i] = 0.0
i++
}
p.n -= i
n, err := p.Source.Receive(dst[i:])
if err != nil {
return 0, err
}
return n + i, nil
}
type discard struct {
snd.Sink
n int
}
func (d *discard) PutSamples(vs []float64) error {
if d.n >= len(vs) {
d.n -= len(vs)
return nil
}
n := d.n
d.n = 0
return d.Sink.Send(vs[n:])
}
func princVal(v float64) float64 {
return math.Mod(v+math.Pi, 2*math.Pi) - math.Pi
}
func rotate(d []complex128) {
m := len(d) / 2
for i := 1; i < m; i++ {
d[i] *= -1i
}
for i := m; i < len(d); i++ {
d[i] *= 1i
}
}
func unWrap(phs []float64) {
unWrapFrom(0.0, phs)
}
func unWrapFrom(last float64, phs []float64) {
var dp, acc float64
acc = 0.0
for i, ph := range phs {
dp = ph - last
if dp < -math.Pi {
acc += 2 * math.Pi
} else if dp > math.Pi {
acc -= 2 * math.Pi
}
last = ph
phs[i] = ph + acc
}
}
func wrap(phs []float64) {
for i, ph := range phs {
phs[i] = princVal(ph)
}
}
func diff(vs []float64) {
diffFrom(0.0, vs)
}
func diffFrom(last float64, vs []float64) {
for i, v := range vs {
vs[i] = v - last
last = v
}
}
func unDiff(vs []float64) {
unDiffFrom(0.0, vs)
}
func unDiffFrom(last float64, vs []float64) {
var t float64
for i, v := range vs {
t = v + last
vs[i] = t
last = t
}
}