Controls and channels¶
Every device describes itself in sdr.caps, and the same calls work on all of them. A control a device doesn't
have raises err.NotSupported.
What a radio can do¶
caps = sdr.caps # refined for the attached unit once open
print(caps.freq, caps.rates) # [Range]: tunable and sample-rate ranges (lo == hi: one discrete value)
print(caps.channels, caps.shared) # channels per direction; what one setting changes
print(sdr.rx_gain_stages()) # {stage: GainStage}: the device's own gain stages, by its names
Capabilities (reference) is what the driver declares, refined once the device is open for the
unit attached (a tuner's real gain table, the rates its firmware offers).
Gain¶
sdr.set_rx_gain(0.6) # a fraction of the device's range (0 least, 1 most): the portable call
sdr.set_rx_gain_db(40) # or in dB: a total the device splits across its stages...
sdr.set_rx_gain_db(16, stage="IF") # ... or one stage, by its name
print(sdr.get_rx_gain(), sdr.get_rx_gain_db(), sdr.get_rx_gains_db()) # fraction, total dB, dB by stage
There are three ways to set gain, and they're meant for different code:
set_rx_gain(fraction)is the portable call. 0.5 is the middle of this radio's range, whatever the radio. 40 dB is most of one radio's range and a third of another's, so code that should run on more than one kind of radio uses the fraction. A value outside 0-1 raises rather than being clamped: it is nearly always decibels given to the wrong call.set_rx_gain_db(total)sets a total in dB, which the device splits across its stages.set_rx_gain_db(db, stage="...")sets one stage, by the device's own names (rx_gain_stages()). Each driver also keeps its own per-stage setters (set_rx_lna_gain,set_rx_vga_gain...) alongside the generic ones.
Everything else¶
print(sdr.rx_gain_modes(), sdr.settings()) # automatic gain modes, everything else the device has
for call in (lambda: sdr.set_rx_bandwidth(1.5e6), # analog filter (caps.bandwidths)
lambda: sdr.set_rx_gain_mode("auto")): # automatic gain where the device has it
try:
call()
except pysdrlib.err.NotSupported as e: # the simulator has neither
print("not supported:", e)
sdr.set_freq_correction(14.8) # ppm error of the reference: tuning (and continuous rates) corrected
sdr.dc_correction = True # remove the DC offset from what read() returns
sdr.strict = True # out-of-range values raise instead of being clamped
try:
sdr.set_rx_freq(20e9)
except pysdrlib.err.InvalidValue as e:
print(e)
Each control that differs between receiving and transmitting has a call for each direction: set_rx_freq /
set_tx_freq, set_rx_gain / set_tx_gain, set_rx_bandwidth / set_tx_bandwidth, set_rx_antenna /
set_tx_antenna... Each takes channel=0. sdr.rx_antennas() and sdr.clock_sources() list what a device has.
Channels¶
A device with several channels (sdr.caps.channels, e.g. a B210's 2 RX and 2 TX) streams any of them together:
sdr = pysdrlib.device("sim", channels=2, transmit=True) # device(): constructor arguments, then open()
sdr.open()
sdr.initialize(cf=100e6, Fs=2e6)
sdr.set_rx_gain_db(30, channel=1) # every control takes the channel
print(sdr.caps.channels, sdr.caps.shared) # how many per direction; what one setting changes
sdr.start_rx(channels=(0, 1))
x = sdr.read(65536, channel=(0, 1)) # (2, 65536), rows starting on the same sample
x1 = sdr.read(8192, channel=1) # or one channel at a time
sdr.stop_rx()
sdr.start_tx(channels=(0, 1))
tone = np.exp(2j * np.pi * 0.01 * np.arange(8192)).astype(np.complex64)
sdr.write(np.stack((tone, -tone))) # rows go out sample for sample together
sdr.stop_tx()
sdr.close()
caps.shared says what one setting changes: "freq": "direction" means the channels of a direction share one
synthesizer, so retuning one retunes the others. Samples the hardware lost before they reached pysdrlib (an overflow)
show as gaps on every channel alike, so the channels stay aligned.
Transmitting¶
Radios that transmit (caps.channels["tx"] > 0) take complex64 at full scale ±1 through start_tx() and
write(samples). write blocks while the transmit queue is full, and get_tx_stats() counts underruns. Start at
tx_gain=0, into a load or an antenna you are licensed to use. The HackRF is half-duplex; see each driver's page.
The method reference for all of this is pysdrlib.base.device.