-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.py
executable file
·287 lines (225 loc) · 8.25 KB
/
radio.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python2
from __future__ import division
# Import functions and libraries
import numpy as np
import matplotlib.pyplot as plt
import pyaudio
import Queue
import threading,time
import sys
from numpy import pi
from numpy import sin
from numpy import zeros
from numpy import r_
from numpy import ones
from scipy import signal
from scipy import integrate
import threading,time
import multiprocessing
from rtlsdr import RtlSdr, limit_time
from numpy import mean
from numpy import power
from numpy.fft import fft
from numpy.fft import fftshift
from numpy.fft import ifft
from numpy.fft import ifftshift
import bitarray
from scipy.io.wavfile import read as wavread
import serial
# Plot an image of the spectrogram y, with the axis labeled with time tl,
# and frequency fl
#
# t_range -- time axis label, nt samples
# f_range -- frequency axis label, nf samples
# y -- spectrogram, nf by nt array
# dbf -- Dynamic range of the spect
def sg_plot( t_range, f_range, y, dbf = 60) :
eps = 1e-3
# find maximum
y_max = abs(y).max()
# compute 20*log magnitude, scaled to the max
y_log = 20.0 * np.log10( abs( y ) / y_max + eps )
fig=plt.figure(figsize=(15,6))
plt.imshow( np.flipud( 64.0*(y_log + dbf)/dbf ), extent= t_range + f_range ,cmap=plt.cm.gray, aspect='auto')
plt.xlabel('Time, s')
plt.ylabel('Frequency, Hz')
plt.tight_layout()
def myspectrogram_hann_ovlp(x, m, fs, fc,dbf = 60):
# Plot the spectrogram of x.
# First take the original signal x and split it into blocks of length m
# This corresponds to using a rectangular window %
isreal_bool = np.isreal(x).all()
# pad x up to a multiple of m
lx = len(x);
nt = (lx + m - 1) // m
x = np.append(x,zeros(-lx+nt*m))
x = x.reshape((m/2,nt*2), order='F')
x = np.concatenate((x,x),axis=0)
x = x.reshape((m*nt*2,1),order='F')
x = x[r_[m//2:len(x),ones(m//2)*(len(x)-1)].astype(int)].reshape((m,nt*2),order='F')
xmw = x * np.hanning(m)[:,None];
# frequency index
t_range = [0.0, lx / fs]
if isreal_bool:
f_range = [ fc, fs / 2.0 + fc]
xmf = np.fft.fft(xmw,len(xmw),axis=0)
sg_plot(t_range, f_range, xmf[0:m/2,:],dbf=dbf)
print 1
else:
f_range = [-fs / 2.0 + fc, fs / 2.0 + fc]
xmf = np.fft.fftshift( np.fft.fft( xmw ,len(xmw),axis=0), axes=0 )
sg_plot(t_range, f_range, xmf,dbf = dbf)
return t_range, f_range, xmf
def play_audio( Q, p, fs , dev, ser="", keydelay=0.200):
# play_audio plays audio with sampling rate = fs
# Q - A queue object from which to play
# p - pyAudio object
# fs - sampling rate
# dev - device number
# ser - pyserial device to key the radio
# keydelay - delay after keying the radio
# Example:
# fs = 44100
# p = pyaudio.PyAudio() #instantiate PyAudio
# Q = Queue.queue()
# Q.put(data)
# Q.put("EOT") # when function gets EOT it will quit
# play_audio( Q, p, fs,1 ) # play audio
# p.terminate() # terminate pyAudio
# open output stream
ostream = p.open(format=pyaudio.paFloat32, channels=1, rate=int(fs),output=True,
output_device_index=dev, frames_per_buffer=128)
# play audio
while (1):
data = Q.get()
if data=="EOT" :
break
elif (data=="KEYOFF" and ser!=""):
time.sleep(keydelay)
ser.setDTR(0)
#print("keyoff\n")
elif (data=="KEYON" and ser!=""):
ser.setDTR(1) # key PTT
#print("keyon\n")
time.sleep(keydelay) # wait 200ms (default) to let the power amp to ramp up
else:
try:
ostream.write( data.astype(np.float32).tostring() )
except:
print("Exception")
break
def record_audio( queue, p, fs ,dev,chunk=1024):
# record_audio records audio with sampling rate = fs
# queue - output data queue
# p - pyAudio object
# fs - sampling rate
# dev - device number
# chunk - chunks of samples at a time default 1024
#
# Example:
# fs = 44100
# Q = Queue.queue()
# p = pyaudio.PyAudio() #instantiate PyAudio
# record_audio( Q, p, fs, 1) #
# p.terminate() # terminate pyAudio
istream = p.open(format=pyaudio.paFloat32, channels=1, rate=int(fs),input=True,input_device_index=dev,
frames_per_buffer=chunk)
# record audio in chunks and append to frames
frames = [];
while (1):
try: # when the pyaudio object is distroyed stops
data_str = istream.read(chunk) # read a chunk of data
except:
break
data_flt = np.fromstring( data_str, 'float32' ) # convert string to float
queue.put( data_flt ) # append to list
def text2Morse(text,fc,fs,dt):
CODE = {'A': '.-', 'B': '-...', 'C': '-.-.',
'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..',
'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-',
'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.',
' ': ' ', "'": '.----.', '(': '-.--.-', ')': '-.--.-',
',': '--..--', '-': '-....-', '.': '.-.-.-',
'/': '-..-.', ':': '---...', ';': '-.-.-.',
'?': '..--..', '_': '..--.-'
}
Ndot= 1.0*fs*dt
Ndah = 3*Ndot
sdot = sin(2*pi*fc*r_[0.0:Ndot]/fs)
sdah = sin(2*pi*fc*r_[0.0:Ndah]/fs)
# convert to dit dah
mrs = ""
for char in text:
mrs = mrs + CODE[char.upper()] + "*"
sig = zeros(1)
for char in mrs:
if char == " ":
sig = concatenate((sig,zeros(Ndot*7)))
if char == "*":
sig = concatenate((sig,zeros(Ndot*3)))
if char == ".":
sig = concatenate((sig,sdot,zeros(Ndot)))
if char == "-":
sig = concatenate((sig,sdah,zeros(Ndot)))
return sig
def printDevNumbers(p):
N = p.get_device_count()
for n in range(0,N):
name = p.get_device_info_by_index(n).get('name')
print n, name
def afsk(bits, f_low, f_high, baud, fs=44100.0):
bits = 1.0 * bits
Ns = int(float(fs) / baud)
M = np.repeat(bits*2-1,Ns)
f0 = (f_low + f_high) / 2.0
delta_f = (f_high - f_low) / 2.0
# compute phase by integrating frequency
ph = 2*pi*np.cumsum(f0 + M.ravel()*delta_f)/fs
FSK = sin(ph)
return FSK
def nc_afsk_demod(sig, f_low, f_high, TBW=2.0, N=74, fs = 44100.0):
# non-coherent demodulation of afsk1200
# function returns the NRZI (without rectifying it)
BW = float(TBW) / N
h_mark = signal.firwin(numtaps=N, cutoff=BW) * np.exp(1j * 2 * pi * f_low/fs * np.arange(N))
h_space = signal.firwin(numtaps=N, cutoff=BW) * np.exp(1j * 2 * pi * f_high/fs * np.arange(N))
v_space = signal.fftconvolve(sig, h_space)
v_mark = signal.fftconvolve(sig, h_mark)
NRZa = abs(v_space) - abs(v_mark)
return NRZa[N/2:-N/2]
def decode_bits2(NRZI, fs=44100.0, baud=1200):
samples_bit = float(fs) / baud
offset = samples_bit / 2.0
i = offset
n = 0
bits = []
while i < len(NRZI):
bits.append(np.mean(NRZI[i-8:i+8]) > 0)
n += 1
i = int(n*samples_bit + offset)
bits = np.array(bits)
return bits
def genChirpPulse(Npulse, f0, f1, fs):
# Function generates an analytic function of a chirp pulse
# Inputs:
# Npulse - pulse length in samples
# f0 - starting frequency of chirp
# f1 - end frequency of chirp
# fs - sampling frequency
t = r_[0.0:Npulse]/fs
k = (f1 - f0) / max(t)
f_of_t = f0 + k * t
phi_of_t = np.cumsum(f_of_t) * 2 * pi * 1.0/fs
s_chirp_a = np.exp(1j * phi_of_t)
return s_chirp_a
def crossCorr( rcv, pulse_a ):
return signal.fftconvolve(rcv, pulse_a[::-1], mode="full")