-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.go
83 lines (70 loc) · 1.28 KB
/
misc.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
package main
import (
"math"
)
func maxmin(b []byte) (max, min int) {
max, min = 0, 255
for i := 0; i < len(b); i++ {
if int(b[i]) > max {
max = int(b[i])
}
if int(b[i]) < min {
min = int(b[i])
}
}
return
}
func maxfreq(nrml []float64) (max, f float64) {
max, mi := 0, 0
off := 10
for i, v := range nrml[off : len(nrml)/2] {
if v > max {
max = v
mi = i
}
}
f = float64(mi+off) * SAMP / N
return
}
//dummy 1000hz generator
type dummy struct {
c float64
}
func (d dummy) Read(b []byte) (n int, err error) {
freq := 1000.0
for i := range b {
b[i] = byte(127*math.Sin(2*math.Pi*freq*float64(d.c)/SAMP) + 128)
d.c += 1
}
return len(b), nil
}
// actually, modulus squared
func modulus(x complex64) float64 {
return math.Sqrt(float64(real(x * complex(real(x), -imag(x)))))
}
// toss out the first few, and the top half
func maxminc(s []complex64) (max, min float64, maxi, mini int) {
max, min = 0, math.MaxFloat64
for i, x := range s[:len(s)/2] {
v := modulus(x)
if v > max {
max, maxi = v, i
}
if v < min {
min, mini = v, i
}
}
return
}
func binf(i int) float64 {
return float64(i) * SAMP / N
}
func fbin(f float64) int {
return int(f * N / SAMP)
}
func add(xs ...float64) (total float64) {
for _, v := range xs {
total += v
}
return
}