-
Notifications
You must be signed in to change notification settings - Fork 0
/
demodulator.py
128 lines (124 loc) · 2.68 KB
/
demodulator.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class Demod:
def __init__(self, sample):
self.s = sample
self.state = 0
self.index = 0
def detect(self):
count = 0
state = 0
current = 0
head = []
while True:
if self.index == len(self.s):
return False
if state == 0:
if self.s[self.index] != 0:
count += 1
current = self.s[self.index]
state = 1
elif state == 1:
if self.s[self.index] == current:
count += 1
else:
# print(self.index)
if count >= 66:
if count <= 110:
head.append(current)
if len(head) == 3:
break
else:
if len(head) == 2:
head.append(current)
break
print("Too long prefix!:", self.index, count)
count = 0
state = 0
current = 0
head = []
continue
else:
print("Too short prefix!:", self.index, count)
count = 0
state = 0
current = 0
head = []
continue
count = 0
if self.s[self.index] == 0:
state = 0
current = 0
else:
current = self.s[self.index]
self.index += 1
if head == [1,-1,1]:
return True
else:
print("Wrong prefix!:", head)
return False
def read(self):
count = 0
state = 0
current = 0
payload = []
while True:
if self.index == len(self.s):
return []
if state == 0:
if self.s[self.index] != 0:
count += 1
current = self.s[self.index]
state = 1
elif state == 1:
if self.s[self.index] == current:
count += 1
else:
# print(self.index)
if count >= 165:
if count <= 275:
payload.append(current)
if len(payload) >= 24:
break
elif count >= 330 and count <= 550:
payload.append(current)
payload.append(current)
if len(payload) >= 24:
break
elif count > 550 and len(payload) == 23:
payload.append(current)
break
else:
print("Ood length of symbol:", len(payload), self.index, count)
else:
print("Too short symbol!:", self.index, count)
count = 0
if self.s[self.index] == 0:
state = 0
current = 0
else:
current = self.s[self.index]
self.index += 1
payload = payload[:24]
out = "0b"
for i in range(8):
symbol = payload[3*i:3*i+3]
if symbol == [-1,1,-1]:
out += "0"
elif symbol == [1,-1,1]:
out += "1"
else:
print("Wrong symbol:", symbol)
# print(out)
return int(out,2)
def demodulate(self):
index = []
payloads = []
while True:
if self.detect():
payload = self.read()
print(payload)
payloads.append(payload)
index.append(self.index)
print("---------------------------------")
if self.index == len(self.s):
break
return index, payloads