-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgsm.py
More file actions
220 lines (170 loc) · 6.79 KB
/
Copy pathgsm.py
File metadata and controls
220 lines (170 loc) · 6.79 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
from calibratesdr.dabplus.dab import channels
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
from scipy import signal
from .arfcn_freq import channels, band_key
from .fcch_offset import calculate_offset
def wav_to_iq(file_path, from_index=2000, to_index=748000):
"""
Input: file path towards IQ file as .wav
Output: sample rate and array with memmap true
"""
sample_rate, data = wavfile.read(file_path, True)
samples = data[from_index:to_index, 0] + 1j * data[from_index:to_index, 1]
return sample_rate, np.array(samples).astype("complex64") - (127.5 + 1j * 127.5)
def avg_power_spectrum(data, N=256, fs=1):
"""
Input: array of IQ sample, N, sampling rate
Output: average spectrum (using np.abs)
"""
M = int(np.floor(len(data)/N))
data_ = np.reshape(data[:M*N], (M, N)) * np.hamming(N)[None, :]
X = np.fft.fftshift(np.fft.fft(data_, axis=1), axes=1)
return np.r_[-N/2.0:N/2.0]/N*fs, np.mean(abs(X**2), axis=0)
def spectrogram_plot(t_range, f_range, y, dbf=60, fig=None):
"""
Input --
t_range: time axis, nt samples
f_range: frequency axis, nf samples
dbf: Dynamic range of the spectrum
Output --
Spectrogram, with xlabel Time in sec and ylabel Frequency in Hz
"""
eps = 10.0**(-dbf/20.0) # minimum signal
y_max = abs(y).max()
y_log = 20.0 * np.log10((abs(y) / y_max)*(1-eps) + eps)
fig = plt.figure(figsize=(16, 6))
plt.imshow(np.flipud(64.0*(y_log + dbf)/dbf), extent=t_range +
f_range, cmap='RdYlBu', aspect='auto')
plt.xlabel('Time, s')
plt.ylabel('Frequency, Hz')
plt.tight_layout()
return fig
def spectrogram_hann(data, m, fs, fc, dbf=60, fig=None):
"""
Split it into blocks of length m.
Function plots the spectrogram of x, calling function spectrogram_plot
"""
isreal_bool = np.isreal(data).all()
# print(isreal_bool)
length = len(data)
nt = (length + m - 1) // m
data = np.append(data, np.zeros(-length+nt*m))
data = data.reshape((m//2, nt*2), order='F')
data = np.concatenate((data, data), axis=0)
data = data.reshape((m*nt*2, 1), order='F')
data = data[np.r_[m//2:len(data), np.ones(m//2)*(len(data)-1)
].astype(int)].reshape((m, nt*2), order='F')
xmw = data * np.hanning(m)[:, None]
# frequency index
t_range = [0.0, length / fs]
if isreal_bool:
f_range = [fc, fs / 2.0 + fc]
xmf = np.fft.fft(xmw, len(xmw), axis=0)
fig = spectrogram_plot(t_range, f_range, xmf[0:m//2, :], dbf, fig)
else:
f_range = [-fs / 2.0 + fc, fs / 2.0 + fc]
xmf = abs(np.fft.fftshift(np.fft.fft(xmw, len(xmw), axis=0), axes=0))
fig = spectrogram_plot(t_range, f_range, xmf, dbf, fig)
plt.show()
return None
def offset_plot(data, m, fs, fc=0):
burst_t = 576.9e-6
burst_len = 1 + fs*burst_t//1
fcc = fc + 5000
demod_data = np.exp(-1j * fcc * np.linspace(0, len(data), len(data)))*data
spectrogram_hann(demod_data, m, fs, fcc)
plt.show()
h = signal.firwin(141, 7500, nyq=2400000.0/2, window='hanning')
filtered_data = signal.fftconvolve(demod_data, h)[::10]
spectrogram_hann(filtered_data, m, fs/10, fcc)
plt.show()
plt.figure(figsize=(16, 4))
plt.plot(np.r_[0:len(filtered_data)]/fs*100, abs(filtered_data))
plt.title('Magnitude of filtered and demodded signal')
plt.xlabel('t [ms]')
plt.show()
def gsm_plots(data, fs, fc=0):
print(f"Sample rate used: {fs}")
f, sp = avg_power_spectrum(data, N=256, fs=fs/1000)
plt.figure(figsize=(8, 4))
plt.plot(f, 10*np.log10(sp))
plt.title('average power spectrum of GSM')
plt.xlabel('frequency offset [KHz]')
plt.show()
plt.figure(figsize=(16, 4))
plt.plot(np.r_[0:12000.0]/fs*1000, abs(data[:12000]))
plt.title('Magnitude GSM signal, showing TDMA frames')
plt.xlabel('t [ms]')
plt.show()
m = 128 # window length
spectrogram_hann(data, m, fs, fc)
plt.show()
offset_plot(data, m, fs, fc=0)
def main(filepath=None, fc=0, sdr=False, input=input):
fcc = (1625000.0 / 6.0) / 4.0
h_bw = 15000
if filepath != None:
fs, data = wav_to_iq(filepath)
# if input["gr"]== True
gsm_plots(data, fs, fc)
offset = calculate_offset(data, fs, fc)
return (offset, offset / (fc + (1625000.0 / 6.0) / 4.0)*1e6)
if sdr == True and input["fc"] != None:
from rtlsdr import RtlSdr
ppm = 1
N_mf = 1 # debug
device = input["rd"]
sdr = RtlSdr(device_index=device)
fs = 270833.002142 # debug
fs = input["rs"]
fc = input["fc"] # debug
gain = input["rg"]
span = N_mf*235.4*.001
ns = (span*fs+2048)//256 * 256
print('Number of samples = ' + str(ns))
sdr.sample_rate = fs
sdr.gain = gain
sdr.center_freq = fc
sdr.set_freq_correction(ppm)
data = sdr.read_samples(ns)[2048:]
sdr.close()
#gsm_plots(data, fs, fc)
demod_data = np.exp(-1j * fcc * np.linspace(0,
len(data), len(data)))*data
h = signal.firwin(141, h_bw/2, nyq=fs/2, window='hanning')
filtered_data = signal.fftconvolve(demod_data, h)[::10]
offset = calculate_offset(filtered_data, fcc, fs, graph=True)
print(
f"Offset Frequency: {offset} \nOffset in PPM: {offset / (fc + (1625000.0 / 6.0) / 4.0)*1e6}")
# (offset, offset / (fc + (1625000.0 / 6.0) / 4.0)*1e6)
if sdr == True and input["c"] != None:
from rtlsdr import RtlSdr
ppm = 1
N_mf = 1 # debug
fs = 270833.002142
device = input["rd"]
sdr = RtlSdr(device_index=device)
sdr.sample_rate = fs
sdr.gain = 20
span = N_mf*235.4*.001
ns = (span*fs+2048)//256 * 256
#gsm_band = input("Enter band you would like to scan: ")
if input["c"] == "all":
print("Enter one of the following as -c parameter\nGSM Bands: \n\t=>GSM_850\n\t=>GSM_R_900\n\t=>GSM_900\n\t=>GSM_E_900\n\t=>DCS_1800\n\t=>PCS_1900")
gsm_freq = channels(band_key(input["c"]))
for fc in gsm_freq:
sdr.center_freq = fc
sdr.set_freq_correction(ppm)
data = sdr.read_samples(ns)[2048:]
sdr.close()
demod_data = np.exp(-1j * fcc * np.linspace(0,
len(data), len(data)))*data
h = signal.firwin(141, h_bw/2, nyq=fs/2, window='hanning')
filtered_data = signal.fftconvolve(demod_data, h)[::10]
offset = calculate_offset(filtered_data, fcc, fs, graph=True)
print(
f"Offset Frequency: {offset} \nOffset in PPM: {offset / (fc + (1625000.0 / 6.0) / 4.0)*1e6}")
if __name__ == "__main__":
main()