-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (117 loc) · 2.75 KB
/
main.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
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
package main
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"log"
"math"
"os"
"github.com/funcan/soapyradiotool/mainwindow"
"github.com/funcan/soapyradiotool/mathtools"
)
type U8Data struct {
Len int64
SampleRate int64
I []uint8
J []uint8
Real []float64
}
func ReadU8(filename string, samplerate int64) (*U8Data, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
stats, err := f.Stat()
if err != nil {
return nil, err
}
size := stats.Size()
bytes := make([]byte, size)
reader := bufio.NewReader(f)
_, err = reader.Read(bytes)
ret := U8Data{
Len: size / 2,
SampleRate: samplerate,
I: make([]uint8, size/2),
J: make([]uint8, size/2),
Real: make([]float64, size/2),
}
for i := int64(0); i < size/2; i++ {
ret.I[i] = uint8FromBuf(bytes, i*2)
ret.J[i] = uint8FromBuf(bytes, i*2+1)
ret.Real[i] = math.Sqrt(float64(ret.I[i]) * float64(ret.J[i]))
}
return &ret, nil
}
func uint8FromBuf(buf []byte, offset int64) uint8 {
var ret uint8
tmp := bytes.NewBuffer(buf[offset : offset+1])
err := binary.Read(tmp, binary.LittleEndian, &ret)
// FIXME: Pass up err rather than panic
if err != nil {
panic(err)
}
return ret
}
func readFile(arg interface{}) {
filename, ok := arg.(string)
if !ok {
log.Printf("readFile invalid parameter %T", filename)
return
}
data, err := ReadU8(filename, 2000000)
if err != nil {
log.Printf("readFile error: %s", err)
return
}
f := registerSource("show data")
f(data.Real)
}
func oldmain() {
filename := "/home/duncan/src/rtl_433_tests/tests/generic_remote/01/gfile001.cu8"
data, err := ReadU8(filename, 2000000)
if err != nil {
panic(err)
}
mean := mathtools.Mean(data.Real)
stdDev := mathtools.StdDev(data.Real)
squelched := mathtools.Squelch(data.Real, mean+stdDev)
//avg := mathtools.RollingAverage(squelched, 5)
// for _, d := range(squelched) {
// fmt.Printf("%f\n", d)
// }
hist := mathtools.Histogram(squelched, 100)
for _, d := range hist {
fmt.Printf("%d\n", d)
}
}
var listeners map[string][]func(e interface{})
func registerListener(event string, f func(e interface{})) {
if listeners == nil {
listeners = make(map[string][]func(e interface{}))
}
val, ok := listeners[event]
if !ok {
listeners[event] = make([]func(e interface{}), 0)
val = listeners[event]
}
listeners[event] = append(val, f)
}
func registerSource(event string) func(e interface{}) {
return func(e interface{}) {
log.Printf("event '%s' -> %T", event, e)
for s, funcs := range listeners {
if s == event {
for _, f := range funcs {
f(e)
}
}
}
}
}
func main() {
registerListener("load file", readFile)
mainwindow.Setup(registerListener, registerSource)
}