forked from bemasher/rtlcap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtlcap.go
More file actions
233 lines (197 loc) · 4.78 KB
/
Copy pathrtlcap.go
File metadata and controls
233 lines (197 loc) · 4.78 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// rtlcap - A app which provides rtl_sdr-flavored interaction with an rtl_tcp instance.
// Copyright (C) 2016 Douglas Hall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
"os/signal"
"strconv"
"strings"
"time"
"github.com/bemasher/rtltcp"
)
type Size int64
func (s Size) String() string {
return strconv.FormatInt(int64(s), 10)
}
func (s *Size) Set(value string) (err error) {
var (
mantissa float64
exponent string
)
mantissa, err = strconv.ParseFloat(value, 64)
if err == nil {
*s = Size(mantissa)
return
}
_, err = fmt.Sscanf(value, "%f%s", &mantissa, &exponent)
if err != nil {
return
}
switch strings.ToLower(exponent) {
case "k":
*s = Size(mantissa * (1 << 10))
case "m":
*s = Size(mantissa * (1 << 20))
case "g":
*s = Size(mantissa * (1 << 30))
case "t":
*s = Size(mantissa * (1 << 40))
case "p":
*s = Size(mantissa * (1 << 50))
case "e":
*s = Size(mantissa * (1 << 60))
case "z":
*s = Size(mantissa * (1 << 70))
case "y":
*s = Size(mantissa * (1 << 80))
default:
err = fmt.Errorf("invalid expontent")
}
return
}
var (
size Size
timeLimit time.Duration
squelch float64
quietSquelch bool
filename string
blocksize int
)
// Default Magnitude Lookup Table
type MagLUT []float64
// Pre-computes normalized squares with most common DC offset for rtl-sdr dongles.
func NewSqrtMagLUT() (lut MagLUT) {
lut = make([]float64, 0x100)
for idx := range lut {
lut[idx] = (127.4 - float64(idx))
lut[idx] *= lut[idx]
}
return
}
// Calculates complex magnitude on given IQ stream writing result to output.
func (lut MagLUT) Execute(input []byte, output []float64) {
for idx := 0; idx < len(input); idx += 2 {
output[idx>>1] = math.Sqrt(lut[input[idx]] + lut[input[idx+1]])
}
}
func Mean(sig []float64) (mean float64) {
for _, val := range sig {
mean += val
}
mean /= float64(len(sig))
return
}
func init() {
log.SetFlags(log.Lmicroseconds)
rand.Seed(time.Now().UnixNano())
flag.Var(&size, "bytes", "number of bytes to capture")
flag.IntVar(&blocksize, "blocksize", 4096, "number of samples per block")
flag.DurationVar(&timeLimit, "duration", 0, "length of time to capture")
flag.Float64Var(&squelch, "squelch", 0.0, "minimum mean level a sample block must be to commit to disk")
flag.BoolVar(&quietSquelch, "quietsquelch", false, "suppress log output messages for squelched blocks")
flag.StringVar(&filename, "o", "/dev/null", "filename to write samples to")
}
func main() {
var sdr rtltcp.SDR
sdr.RegisterFlags()
flag.Parse()
err := sdr.Connect(nil)
if err != nil {
log.Fatal(err)
}
defer sdr.Close()
sdr.HandleFlags()
var (
output io.Writer
)
if filename == "/dev/null" {
output = ioutil.Discard
} else {
sampleFile, err := os.Create(filename)
if err != nil {
log.Fatal("error creating output file:", err)
}
defer sampleFile.Close()
output = sampleFile
}
var (
tLimit <-chan time.Time
sigint chan os.Signal
bytesRead int64
)
if timeLimit != 0 {
sigint = make(chan os.Signal, 1)
signal.Notify(sigint, os.Kill, os.Interrupt)
tLimit = time.After(timeLimit)
}
in, out := io.Pipe()
go func() {
for {
io.CopyN(out, sdr, 16384)
}
}()
block := make([]byte, blocksize<<1)
mag := make([]float64, blocksize)
lut := NewSqrtMagLUT()
var min, max float64
meanTick := time.Tick(time.Second)
for {
// Exit on interrupt or time limit, otherwise receive.
select {
case <-sigint:
return
case <-tLimit:
return
case <-meanTick:
if !(quietSquelch && max < squelch) {
log.Printf("Min: %0.3f Max: %0.3f\n", min, max)
}
min = math.MaxFloat64
max = -math.MaxFloat64
default:
if size != 0 && bytesRead > int64(size) {
return
}
_, err := in.Read(block)
if err != nil {
log.Fatal("Error reading sample block:", err)
}
lut.Execute(block, mag)
mean := Mean(mag)
if mean > max {
max = mean
}
if mean < min {
min = mean
}
if squelch != 0 && mean < squelch {
continue
}
n, err := output.Write(block)
if err != nil {
log.Fatal("Error writing sample block:", err)
}
bytesRead += int64(n)
}
}
}