-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtl_quickstart.py
More file actions
76 lines (60 loc) · 1.84 KB
/
rtl_quickstart.py
File metadata and controls
76 lines (60 loc) · 1.84 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
# pip install pyrtlsdr
import numpy as np
import time
import torch
import plotly.graph_objects as go
# Use either PlutoSDR or RTL-SDR based on available hardware
use_rtlsdr = True # Set to False to use PlutoSDR instead
# Common parameters
fc = 98e6 # Center frequency
fs = 2.4e6 # Sample rate (note: RTL-SDR has max of 2.4 MSPS for stable operation)
Nsamp = 2**10
numRows = 1
if use_rtlsdr:
from rtlsdr import RtlSdr
# Configure RTL-SDR
sdr = RtlSdr()
sdr.center_freq = int(fc)
sdr.sample_rate = int(fs)
sdr.gain = 40.0 # RTL-SDR uses different gain values
else:
import adi
# Configure PlutoSDR
sdr = adi.Pluto("ip:192.168.2.1")
sdr.rx_lo = int(fc)
sdr.sample_rate = int(fs)
sdr.gain_control_mode_chan0 = 'manual'
sdr.rx_hardwaregain_chan0 = 70.0
sdr.rx_buffer_size = int(Nsamp)
sdr.rx_destroy_buffer()
# Setup plot
fftSize = Nsamp
window = np.blackman(fftSize)
freq = np.fft.fftshift(np.fft.fftfreq(fftSize, 1/fs)) + fc
specData = np.zeros((numRows, fftSize))
time_axis = (np.arange(numRows) + 1) * fftSize/fs
# Timing calculation
Texpected = Nsamp/fs*numRows
print('time expected (s) ' + str(Texpected))
tic = time.time()
for i in range(numRows):
if use_rtlsdr:
# Get samples from RTL-SDR
samples = sdr.read_samples(Nsamp)
else:
# Get samples from PlutoSDR
samples = sdr.rx()
spectrum = np.fft.fftshift(np.fft.fft(samples * window))
powerDb = 10 * np.log10(np.abs(spectrum)**2)
specData = np.vstack([specData[1:], powerDb])
toc = time.time() - tic
print(Texpected/toc) # check that we aren't losing too much data
# Create and display plot
fig = go.Figure()
fig.update_layout(template='plotly_dark')
fig.add_trace(go.Scatter(x=freq, y=powerDb))
fig.show()
print(f"Total time: {time.time() - tic} seconds")
# Clean up
if use_rtlsdr:
sdr.close()