-
Notifications
You must be signed in to change notification settings - Fork 1
/
realtime_2.py
61 lines (53 loc) · 1.57 KB
/
realtime_2.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
import numpy as np
import matplotlib.pyplot as plt
import rtlsdr
import threading
import time
# Function to read samples and update the plot
def update_plot(samples, sdr, ax):
Fs = sdr.get_sample_rate()
N = len(samples)
PSD = np.abs(np.fft.fft(samples))**2 / (N*Fs)
PSD_log = 10*np.log10(PSD)
PSD_shifted = np.fft.fftshift(PSD_log)
freq_axis = np.fft.fftfreq(len(samples), 1/Fs)
freq_axis = np.fft.fftshift(freq_axis)
center_frequency = sdr.get_center_freq()
freq_axis = freq_axis + center_frequency
f = freq_axis/1e6
ax.clear()
ax.plot(f, PSD_shifted)
ax.set_xlabel("Frequency MHz")
ax.set_ylabel("Power dB")
ax.set_title(f"SDR Spectrum at {center_frequency/1e6:.3f} MHz")
ax.grid(True)
plt.draw()
plt.pause(0.001)
# Function to continuously read samples from the SDR
def read_samples(sdr, ax):
while True:
try:
samples = sdr.read_samples(256*1024)
update_plot(samples, sdr, ax)
except usb.core.USBError:
print('SDR device error: USB transfer failed')
time.sleep(1)
except Exception as e:
print(f'Error: {e}')
break
# Set up the SDR device
sdr = rtlsdr.RtlSdr()
sdr.sample_rate = 2.4e6
sdr.center_freq = 100e6
sdr.gain = 50
# Create a plot to display the spectrum
fig, ax = plt.subplots()
ax.set_ylim(-100, 0)
# Start a separate thread to read the samples and update the plot
t = threading.Thread(target=read_samples, args=(sdr, ax))
t.daemon = True
t.start()
# Show the plot
plt.show()
# Clean up the SDR device
sdr.close()