Repository navigation
Expand file tree
/
Copy patholivia-rx.py
More file actions
231 lines (189 loc) · 5.63 KB
/
Copy patholivia-rx.py
File metadata and controls
231 lines (189 loc) · 5.63 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
OLIVIA MFSK Modulator
@author: Federico Santandrea
Source code is lightly commented for clarity.
Please read the document to better understand the various processes.
"""
# Libraries
import os, sys
import numpy as np
import sounddevice as sd
from numpy.fft import fft
# Hardcoded parameters
DEFAULT_PARAMS = "32/1000@1500"
SAMPLE_RATE = 8000
BLOCK_THRESHOLD = 24
# Global parameters
'''
Input:
- CENTER_FREQUENCY, SYMBOLS (# of tones), BANDWIDTH
Computed:
- spb: number of bits for a symbol
- fsep: frequency separation between tones, in Hz (also = baud rate)
- wlen: time separation between tones, in samples
'''
# Global objects
'''
- buf: audio sample input buffer
- sin: sounddevice InputStream for sample acquisition
'''
def __main__():
'''
Main program flow.
'''
welcomeAndSetup()
initSound()
syms = []
while True:
# Fetch new samples
updateBuffer()
sym = detectSymbol()
syms.append(sym)
if len(syms) == 64:
# Enough symbols to decode a block
if decodeAndPrintBlock(syms):
# Block decoded successfully, waiting for a new one
syms = []
else:
# Probably not a complete block, try rolling
syms = syms[1:]
#
def welcomeAndSetup():
'''
Decodes command line parameters and prints a welcome message.
'''
global CENTER_FREQUENCY, SYMBOLS, BANDWIDTH
global spb, fsep, wlen, slen
if len(sys.argv) == 1:
params = DEFAULT_PARAMS
elif len(sys.argv) == 2:
params = sys.argv[1]
else:
printUsageAndQuit()
try:
CENTER_FREQUENCY = int(params.split("@")[1])
SYMBOLS = int(params.split("/")[0])
BANDWIDTH = int(params.split("@")[0].split("/")[1])
spb = int(np.log2(SYMBOLS))
fsep = BANDWIDTH/SYMBOLS # = baud
wlen = int(np.ceil(SAMPLE_RATE/fsep))
except:
printUsageAndQuit()
if os.isatty(0):
print("*** Olivia demodulator ***")
print("(C) 2020 Federico Santandrea")
print("federico.santandrea@studio.unibo.it")
print()
print("Starting Olivia demodulator at center " + str(CENTER_FREQUENCY) +
"Hz, using " + str(SYMBOLS) + " tones over " + str(BANDWIDTH) + "Hz")
print()
def printUsageAndQuit():
'''
Prints usage help if needed.
'''
print("usage: " + sys.argv[0] + " [syms/bandwidth@centerfrequency]")
print("Example (default): " + sys.argv[0] + " " + DEFAULT_PARAMS)
quit()
def initSound():
'''
Prepares global InputStream for sample acquisition.
'''
global sin, buf
sin = sd.InputStream(samplerate=SAMPLE_RATE, blocksize=wlen,
#channels=2, device=2, # stereo mix
dtype=np.float32)
sin.start()
buf= np.zeros(wlen)
def updateBuffer():
'''
Acquires a new wlen-ful of samples from audio device.
'''
global buf
(samples, of) = sin.read(wlen)
buf = samples[:,0] # consider only one channel
def detectSymbol():
'''
Applies Fourier transform to audio buffer to detect
symbol corresponding to sampled tone.
Returns
-------
int
Most likely symbol number.
'''
spectrum = np.abs(fft(buf))
ix = CENTER_FREQUENCY - BANDWIDTH/2 + fsep/2
measures = np.zeros(SYMBOLS)
for i in range(0, SYMBOLS):
ix += fsep
measures[i] = spectrum[int(ix * wlen / SAMPLE_RATE)]
mix = np.argmax(measures)
return degray(mix)
def decodeAndPrintBlock(syms):
'''
Decodes a full block of 64 symbols, then prints it
to standard output.
'''
w = np.zeros((spb, 64))
# key = 0xE257E6D0291574EC
key = np.flip(np.array(
[1, 1, 1, 0, 0, 0, 1, 0,
0, 1, 0, 1, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 0,
1, 1, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0, 1,
0, 0, 0, 1, 0, 1, 0, 1,
0, 1, 1, 1, 0, 1, 0, 0,
1, 1, 1, 0, 1, 1, 0, 0]))
output = ""
doubt = 0
for i in range(0, spb):
for j in range(0, 64):
bit = (syms[j] >> ((i+j) % spb)) & 1
if bit == 1:
w[i,j] = -1
else:
w[i,j] = 1
w[i,:] = w[i,:] * (-2*np.roll(key, -13*i)+1)
w[i,:] = fwht(w[i,:])
c = np.argmax(np.abs(w[i,:]))
if abs(w[i,c]) < BLOCK_THRESHOLD:
doubt += 1
if w[i,c] < 0:
c = c + 64
if c != 0:
output += chr(c)
if doubt == 0:
print(output, end="", flush=True)
return True
else:
return False
#
def fwht(data):
'''
Fast Walsh-Hadamard transform.
'''
step = 1
while step < len(data):
for ptr in range(0, len(data), 2*step):
for ptr2 in range(ptr, step+ptr):
bit1 = data[ptr2]
bit2 = data[ptr2+step]
newbit1 = bit2
newbit1 = newbit1 + bit1
newbit2 = bit2
newbit2 = newbit2 - bit1
data[ptr2] = newbit1
data[ptr2+step] = newbit2
step *= 2
return data
def degray(n):
mask = n
while mask != 0:
mask >>= 1
n ^= mask
return n
#
#
__main__()