-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode_raw.py
executable file
·111 lines (92 loc) · 3.15 KB
/
decode_raw.py
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys, struct, collections
from decode_pkt import process_pkt
FMT = struct.Struct("<h")
THRESH = 3000
STRONG_START = 50
STRONG_STOP = 2
RINGBUF_SIZE = 100
SAMP_RATE = 4e6
DATA_RATE = 1e6
CORR_LEN = long( (SAMP_RATE/DATA_RATE) * 20)
CORR_THRESH = THRESH * CORR_LEN
def corr_fn(i):
return 2* int((i % ( 2 * SAMP_RATE / DATA_RATE)) / (SAMP_RATE / DATA_RATE))-1
def decode(fp):
strong = 0
ringbuf = [None] * RINGBUF_SIZE
ringbuf_i = 0
in_pkt = False
pkt_started = False
v = 0
pos = 0
time = 0
last_time = 0
last_corr_v = 0
decode_v = 0
sample_pos = 0
dc = 0
pkt = []
pk = 0
while True:
try:
v, = FMT.unpack(fp.read(FMT.size))
except:
break
ringbuf_oldval = ringbuf[ringbuf_i]
ringbuf[ringbuf_i] = v
if ringbuf_oldval is not None:
if abs(ringbuf_oldval) >= THRESH:
strong = strong - 1
if abs(v) >= THRESH:
strong = strong + 1
if not in_pkt:
if strong >= STRONG_START:
print >>sys.stderr, "Packet at %fs, delta %fms" % (time, (time - last_time)*1e3),
in_pkt = True
last_time = time
else:
if strong <= STRONG_STOP:
print >>sys.stderr, "ended at %fs: %s" % (time, process_pkt("".join(pkt)))
print "".join(pkt)
in_pkt = False
last_corr_v = 0
pkt_started = False
pkt = []
if (pk > 0 and v <= 0) or (pk <= 0 and v > 0):
pk = v
if abs(v) > abs(pk):
pk = v
v = pk
if in_pkt:
if not pkt_started:
corr_v = 0
for i in range(CORR_LEN):
corr_v = corr_v + corr_fn(i) * (ringbuf[ (ringbuf_i - i + RINGBUF_SIZE - 1)%RINGBUF_SIZE ])
if last_corr_v > CORR_THRESH and corr_v < last_corr_v:
pkt_started = pos
sample_pos = 2
decode_tmp = 0
decode_v = 0
dc = 0
for i in range(CORR_LEN/2):
dc = dc + (ringbuf[ (ringbuf_i - i + RINGBUF_SIZE - 1)%RINGBUF_SIZE ])
dc = dc / float(CORR_LEN/2)
last_corr_v = corr_v
else:
#decode_tmp = decode_tmp + v - dc
decode_tmp = decode_tmp + [-THRESH, THRESH][v > dc]
sample_pos = sample_pos + 1
if sample_pos >= int(SAMP_RATE/DATA_RATE):
decode_v = decode_tmp / sample_pos
decode_tmp = 0
sample_pos = sample_pos - int(SAMP_RATE/DATA_RATE)
pkt.append( ["0", "1"][decode_v>0] )
#print pos, v, dc, decode_v
ringbuf_i = (ringbuf_i + 1) % RINGBUF_SIZE
pos = pos + 1
time = float(pos) / SAMP_RATE
if __name__ == "__main__":
with file(sys.argv[1], "rb") as fp:
decode(fp)