/
decode.py
79 lines (61 loc) · 2.24 KB
/
decode.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
#!/usr/bin/env python3
import numpy as np
from scipy import signal
import config
def get_envelope(wave):
analytical_signal = signal.hilbert(wave)
envelope = np.abs(analytical_signal)
return envelope
def binary_slicer(envelope):
avg = np.average(envelope) + .02
sliced = [1 if x > avg else 0 for x in envelope]
return sliced
def manchester(square_wave, samp_per_bit, threshold=None):
if threshold is None:
threshold = samp_per_bit / 1.5
transitions = []
for i in range(len(square_wave)-1):
if square_wave[i] == 1 and square_wave[i+1] == 0:
transitions.append((i, 0))
if square_wave[i] == 0 and square_wave[i+1] == 1:
transitions.append((i, 1))
if not transitions:
return []
valid_transitions = [transitions[0]]
for t, b in transitions[1:]:
if np.abs(valid_transitions[-1][0] + (2*samp_per_bit) - t) < threshold:
valid_transitions.append((t, b))
bits = [b for _, b in valid_transitions]
return bits
def checksum(packet):
if len(packet) < config.packet_header_len:
return False
pos = config.packet_checksum_pos
print(sum(packet[:pos] + packet[pos+1:]) % 2)
return (sum(packet[:pos] + packet[pos+1:]) % 2) == packet[pos]
def bits2ascii(b):
return ''.join(chr(int(''.join(x), 2)) for x in zip(*[iter(b)]*8))
def demux(packet):
if not(len(packet) >= config.packet_header_len):
if config.debug:
print('Packet too short')
return False, None, None
if not(checksum(packet)):
if config.debug:
print('Packet failed checksum')
return False, None, None
if not(packet[config.packet_id_pos] == config.cur_id):
if config.debug:
print('Packet had incorrect id')
print('packet id', packet[config.packet_id_pos])
print('cur id', config.cur_id)
return False, None, None
valid = True
config.cur_id = (config.cur_id + 1) % 2
fragmented = (packet[config.packet_fragment_pos] == 1)
bitstring = ''.join(str(b) for b in packet[config.packet_header_len:])
ascii_str = bits2ascii(bitstring)
message = ''
for c in ascii_str:
message += str(chr(ord(c)))
return valid, message, fragmented