How-to
Python
Python receives the four channels through the SoapySDR driver and the SoapySDR Python bindings. The samples arrive as NumPy arrays.
Requirements
| Item | Value |
|---|---|
| Host | Linux, set up as in Host setup |
| Driver | SoapyVortarian, built and installed: SoapySDR driver |
| Packages | python3-soapysdr, python3-numpy; python3-matplotlib for plots, python3-scipy for filters |
sudo apt install python3-soapysdr python3-numpy python3-matplotlib
Check that Python loads the driver (the board is not contacted):
python3 -c "import SoapySDR; print([dict(d) for d in SoapySDR.Device.enumerate('driver=vortarian')])"
Capture example
One contiguous block of all four channels, then the strongest spectral line of each channel in dBFS:
import numpy as np
import SoapySDR
from SoapySDR import SOAPY_SDR_RX, SOAPY_SDR_CF32, SOAPY_SDR_OVERFLOW
N = 2**20 # samples per channel, 8.4 ms
dev = SoapySDR.Device("driver=vortarian,host=192.168.67.2")
dev.setFrequency(SOAPY_SDR_RX, 0, 375e6) # NCO of ADC 1: RX A, RX B
dev.setFrequency(SOAPY_SDR_RX, 2, 375e6) # NCO of ADC 2: RX C, RX D
dev.setGain(SOAPY_SDR_RX, 0, 20) # RF gain 0 or 20 dB, all channels
rx = dev.setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32, [0, 1, 2, 3])
x = np.zeros((4, N), np.complex64) # rows: RX A to RX D, 1.0 = full scale
dev.activateStream(rx) # the board starts streaming
try:
got = 0
while got < N:
r = dev.readStream(rx, [ch[got:] for ch in x], N - got, timeoutUs=1000000)
if r.ret > 0:
if got == 0:
t0 = r.timeNs # board time of the first sample [ns]
got += r.ret
elif r.ret == SOAPY_SDR_OVERFLOW: # datagrams lost: start the block again
got = 0
else:
raise RuntimeError(f"readStream returned {r.ret}")
finally:
dev.deactivateStream(rx) # the board stops streaming
dev.closeStream(rx)
w = np.hanning(N)
P = 20 * np.log10(np.abs(np.fft.fftshift(np.fft.fft(x * w), axes=1)) / w.sum() + 1e-12)
f = 375 + np.fft.fftshift(np.fft.fftfreq(N, 1 / 125e6)) / 1e6 # [MHz]
for name, p in zip("ABCD", P):
k = p.argmax()
print(f"RX {name}: {p[k]:6.1f} dBFS at {f[k]:.3f} MHz, median {np.median(p):6.1f} dBFS")
print(f"board time of the first sample: {t0 / 1e9:.6f} s")
activateStreamsendsdebug on,deactivateStreamsendsdebug off; thefinallyblock stops the stream also after an error.- When the board has not been initialized since power-up, the driver runs
initwhen the device is opened. - Row k of
xis the same sampling instant in all four channels.
Example scripts
Scripts in host-tools/python:
| Script | Function |
|---|---|
vortarian_spectrogram.py | Spectrogram of one contiguous capture of all four channels, saved as PNG. |
vortarian_waterfall.py | Live spectra and waterfalls of four channels. |
vortarian_detail.py | Detail of a few hundred microseconds: zoomed spectrogram, level, instantaneous frequency. |
vortarian_ble_packet.py | One Bluetooth Low Energy advertising packet: level, I and Q, frequency with the decoded bits. |
vortarian_tone_check.py | Frequency, level and noise floor of the strongest tone of each channel, phase between the channels. |
vortarian_pcap_check.py | Header fields, lost datagrams and timestamps of a tcpdump capture; needs no driver. |
Spectrogram
python3 vortarian_spectrogram.py --args driver=vortarian,host=192.168.67.2 --nco 375e6 --rf 2450e6
vortarian_spectrogram.py with the 4R2C-2G5 front-end (2450 MHz at IF 375 MHz), RF gain 20 dB, antennas on RX A and RX B, none on RX C and RX D. 2050 spectra of 1024 samples, each line the mean power of 4 spectra (32.8 µs). One Bluetooth Low Energy advertising event: packets on 2402, 2426 and 2480 MHz at about 8.2, 8.8 and 9.4 ms. Wi-Fi frames between 2400 and 2445 MHz, periodic bursts from 2450 MHz upwards. Same window, scaling and axes as the GNU Octave and MATLAB spectrogram scripts.
| Option | Default | Meaning |
|---|---|---|
--ms | 20 | capture length in ms; the capture is held in memory, 4 bytes per sample and channel |
--nco | not set | NCO frequency of both ADCs in Hz; IF axis |
--rf | not set | RF frequency at the NCO frequency in Hz; adds the RF axis |
--fft | 1024 | FFT length, Hann window |
--channels | 0 1 2 3 | channels to capture |
--lim | −105 −40 | colour scale in dBFS; a full-scale tone reads 0 dBFS |
--rows | 600 | maximum number of lines; a longer capture is reduced by combining spectra |
--hold | mean | mean or max of the spectra combined into one line; max keeps short bursts |
--out | vortarian_spectrogram.png | output file |
Detail of a signal
vortarian_detail.py cuts a few hundred microseconds out of a capture and draws them three ways. --save keeps the capture, --load reads it again, so one capture can be examined at several places and resolutions.
python3 vortarian_detail.py --args driver=vortarian,host=192.168.67.2 --save cap.npy
python3 vortarian_detail.py --load cap.npy --channel 1 --at 0.55 --us 700 --fft 256
700 µs of RX B. A carrier at 2450.5 MHz is switched on about every 510 µs for about 100 µs at −46 dBFS; at switch-on its frequency settles from 5 MHz above within about 3 µs, at switch-off it runs up by 27 MHz in about 30 µs while the level falls by 10 dB. A weaker source at 2466 MHz shows the same pattern. The Wi-Fi frame at 2437 MHz ends at about 277 µs: the level drops from −59 to −63 dBFS. In the 16.8 ms spectrogram above, the same pulses are the row of dashes at 2450 MHz.
| Panel | Computed as | Resolution |
|---|---|---|
| Spectrogram | FFT of --fft samples, Hann window, 7/8 overlap | 256 samples: 2.05 µs and 488 kHz per bin; 1024 samples: 8.19 µs and 122 kHz |
| Level | |x|² averaged over 64 samples, in dBFS | 0.5 µs; whole 125 MHz band |
| Instantaneous frequency | phase step between consecutive samples × fs / 2π, averaged over 16 samples | 0.13 µs; valid while one signal dominates, drawn above --threshold |
| Option | Default | Meaning |
|---|---|---|
--save, --load | not set | write or read the capture: .npy, 4 × N complex64, 1.0 = full scale |
--channel | 0 | 0 to 3 = RX A to RX D |
--threshold | −55 | level in dBFS that starts the detail |
--at | not set | start of the detail in ms, instead of the threshold |
--us | 700 | length of the detail in µs |
--fft | 256 | FFT length of the zoomed spectrogram |
Signal in the time domain
A narrow-band signal is taken out of the capture by mixing it to 0 Hz, filtering and decimating. With x from the capture example (NCO 375 MHz at RF 2450 MHz), the Bluetooth Low Energy advertising channel on 2402 MHz of RX A:
from scipy import signal # python3-scipy
fs, D = 125e6, 5
y = x[0] * np.exp(-2j * np.pi * (2402e6 - 2450e6) / fs * np.arange(N)) # 2402 MHz to 0 Hz
y = signal.lfilter(signal.firwin(129, 1e6, fs=fs), 1, y)[::D] # 2 MHz wide, 25 MS/s
level = 20 * np.log10(np.abs(y) + 1e-9) # envelope [dBFS]
f = np.angle(y[1:] * np.conj(y[:-1])) * (fs / D) / (2 * np.pi) # instantaneous frequency [Hz]
vortarian_ble_packet.py: one advertising packet from a capture, antenna on RX A, RF gain 20 dB. Top: envelope, −29.4 dBFS, about 55 dB above the noise in the 2 MHz channel; the transmitter is on for about 400 µs, about 20 µs of carrier and then the 376 µs packet. Middle: I and Q of the first 44 µs. Bottom: instantaneous frequency, GFSK at 1 Mbit/s with about ±230 kHz deviation; the 8 preamble bits and the 32 bits of the advertising access address 0x8E89BED6 can be read directly. The bit labels were added for the figure.
Calls
| Call | Function |
|---|---|
SoapySDR.Device("driver=vortarian,host=...") | Opens the board. Arguments: SoapySDR driver. |
setFrequency(SOAPY_SDR_RX, ch, f) | NCO frequency, 250e6 to 500e6. Channels 0 and 1 share one NCO, channels 2 and 3 the other. |
setGain(SOAPY_SDR_RX, 0, g) | RF gain, 0 or 20 dB, common to all channels. |
setupStream(SOAPY_SDR_RX, fmt, [0, 1, 2, 3]) | SOAPY_SDR_CF32: complex float, 1.0 = full scale. SOAPY_SDR_CS16: int16 pairs, native. |
readStream(stream, buffers, n, timeoutUs=...) | Fills up to n samples per channel. Returns the count in ret and the board time of the first sample in timeNs. |
getHardwareTime() | Board time at the end of the newest datagram received, in ns. |
Return values of readStream
ret | Meaning | Action |
|---|---|---|
| > 0 | number of samples written to every buffer | continue |
SOAPY_SDR_OVERFLOW | datagrams were lost, or the board reported a full FIFO | the block is not contiguous: start it again |
SOAPY_SDR_TIMEOUT | no datagrams within the timeout | check the links and Host setup |
Memory and speed
| Quantity | Value |
|---|---|
CF32, four channels | 4 GB per second |
CS16, four channels | 2 GB per second |
| Reading at the full rate | sustained with 12.5 and 25 million samples per readStream call |
Allocate the buffers once and read into slices of them, as in the example. Converting or copying per call costs more time than the reception.
Related
- Scaling of the spectrum in dBFS, windows, noise per bin: Spectrum scaling.
- The same capture in GNU Octave and MATLAB; live displays in GNU Radio.