Device
pysdrlib.base.device ¶
The device-agnostic interface every SDR presents
Lifecycle and tuning (open, initialize, set_rx_freq, set_rx_sample_rate, start_rx...), reading (read, stream,
get_samples), and the controls a device may have, discovered through caps (see pysdrlib.base.caps):
sdr = pysdrlib.device("rtl_sdr"); sdr.open()
sdr.caps.gains["rx"] # the device's own gain stages
sdr.set_rx_gain(0.5) # gain as a fraction of this device's range: the same call on every radio
sdr.set_rx_gain_db(30) # ... or in dB: a total (split by the device) or one stage, set_rx_gain_db(12, stage="IF")
sdr.set_rx_bandwidth(1.5e6) # analog filter, when the device has one
sdr.set_freq_correction(14.8) # ppm error of the device's reference
sdr.set_setting("dither", True) # anything else the device has (caps.settings)
with sdr.stream(65536, meta=True) as s:
for x, m in s: ... # m: RxMeta (sample index, gaps, retunes inside the block)
Every control takes channel=0, and tuning takes direction ("rx", "tx", or None for both). caps.channels has the
channel counts and caps.shared what one setting changes (a frequency may be one channel's, shared by a direction's
channels, or the whole device's). A multi-channel device streams several channels together:
sdr.start_rx(channels=(0, 1)); x = sdr.read(65536, channel=(0, 1)) # (2, 65536), the same instants
sdr.start_tx(channels=(0, 1)); sdr.write(np.stack((a, b))) # sent sample for sample together
A device that lacks a control raises err.NotSupported. Values out of range are clamped with a warning, or raise
err.InvalidValue with strict = True.
Classes:
| Name | Description |
|---|---|
Device |
The interface every driver implements. Use |
Device ¶
The interface every driver implements. Use pysdrlib.open or pysdrlib.device to get one; see the module
docs (pysdrlib.base.device) for an overview.
Class attributes a driver sets: NAME, CAPS (its limits and gain stages, pysdrlib.base.caps), CAN_TRANSMIT, DUPLEX
(receive and transmit at once) and SAMPLE_FORMAT (the layout of the samples the hardware delivers).
Methods:
| Name | Description |
|---|---|
find |
[DeviceInfo] of the units of this driver that are attached, for |
open |
Connect to device. serial=...: the unit with this serial number (where the driver can choose) |
initialize |
Tune the receiver (and the transmitter, where it has its own synthesizer) to |
close |
Disconnect from device, stopping what is streaming first |
start_rx |
Start receiving. channels: which to stream together (default: the last ones, at first channel 0); |
stop_rx |
Stop receiving |
get_samples |
The low-level call: whatever has arrived since the last call (any number, possibly none, without waiting). Most |
start_tx |
Start transmitting (then |
stop_tx |
Stop transmitting |
set_rx_sample_rate |
Set the receive sample rate (the nominal one: a frequency correction is applied to what the hardware is asked |
set_tx_sample_rate |
Set the transmit sample rate (see |
get_rx_sample_rate |
Receive sample rate [Hz] last set for this channel |
get_tx_sample_rate |
Transmit sample rate [Hz] last set for this channel (None: never set) |
set_rx_freq |
Set the receive center frequency (corrected by |
set_tx_freq |
Set the transmit center frequency (see |
get_rx_inverted |
Whether the receive spectrum comes mirrored, from a converter whose LO is above the signal (see |
get_rx_context |
How this channel's samples are received, for a recording to keep ( |
set_level_calibration |
What a level in these samples is at the antenna port: dBm = dBFS + |
get_level_calibration |
The level calibration given to |
get_rx_freq |
Receive center frequency [Hz] last set for this channel (over the air, whatever converter is on the radio) |
get_tx_freq |
Transmit center frequency [Hz] last set for this channel (None: never set) |
set_freq_correction |
Error of the device's frequency reference in parts per million (positive: it runs fast). Tuning and |
get_freq_correction |
Frequency reference error [ppm] last set with |
get_freq_correction_accuracy |
How well the reference's error is known [ppm, a standard error], as given to |
set_rx_gain |
The receive gain, as a fraction of the device's range: 0.0 its least, 1.0 its most (linear in dB), of the total |
set_tx_gain |
The transmit gain, as a fraction of the device's range (see |
get_rx_gain |
The receive gain last set, as a fraction of the device's range (None before a gain was set); |
get_tx_gain |
The transmit gain last set, as a fraction of the device's range (None before a gain was set) |
set_rx_gain_db |
The receive gain in decibels: a total (the device splits it across its stages) or one |
set_tx_gain_db |
The transmit gain in decibels (see |
get_rx_gain_db |
The receive gain last set in decibels: a stage's, or the total of the stages (None before any was set) |
get_tx_gain_db |
The transmit gain last set in decibels: a stage's, or the total of the stages (None before any was set) |
get_rx_gains_db |
{stage name: gain [dB]} of what each receive stage was last set to (stages never set are missing) |
get_tx_gains_db |
{stage name: gain [dB]} of what each transmit stage was last set to |
rx_gain_stages |
{stage name: GainStage} of the receiver, as the device names its stages ( |
tx_gain_stages |
{stage name: GainStage} of the transmitter |
rx_gain_modes |
["manual"] and, when the receiver can set its own gain, "auto" |
tx_gain_modes |
The transmitter's gain modes (["manual"]) |
set_rx_gain_mode |
"auto": the receiver sets its own gain (AGC), "manual": the gain last set. Raises |
set_tx_gain_mode |
The transmitter's gain mode (see |
get_rx_gain_mode |
Current receive gain mode, "manual" unless |
get_tx_gain_mode |
Current transmit gain mode |
set_rx_bandwidth |
Receive analog filter bandwidth [Hz]: the narrowest the device has that is at least |
set_tx_bandwidth |
Transmit analog filter bandwidth [Hz] (see |
get_rx_bandwidth |
Receive analog filter bandwidth [Hz] last set (None: never set) |
get_tx_bandwidth |
Transmit analog filter bandwidth [Hz] last set (None: never set) |
rx_antennas |
The receiver's selectable antenna ports ([]: just the one) |
tx_antennas |
The transmitter's selectable antenna ports ([]: just the one) |
rx_antenna_info |
Each receive antenna port and what is on it, as |
tx_antenna_info |
Each transmit antenna port and what is on it: as |
set_rx_antenna |
Select a receive antenna port by name (see |
set_tx_antenna |
Select a transmit antenna port by name (see |
get_rx_antenna |
The receive antenna port last selected (None: never set) |
get_tx_antenna |
The transmit antenna port last selected (None: never set) |
clock_sources |
Reference clock sources ([]: not selectable) |
set_clock_source |
Select the reference clock by name (see |
get_clock_source |
The reference clock last selected (None: never set) |
set_bias_t |
Set bias-t |
settings |
{name: caps.Setting}: everything else this device can be told (dither, AGC, test modes...) |
set_setting |
Apply one of |
get_setting |
Value of a device specific setting listed in |
set_chains |
The hardware between this radio and the air ( |
get_chains |
The chains in use ( |
get_caps |
|
read |
Exactly |
read_into |
|
stream |
Blocks of |
sweep |
Step the receiver across frequencies, yielding (frequency, samples): |
find_gain |
The highest receive gain at which the signal doesn't clip, found by bisecting the gains the device can set |
read_available |
What is queued for a channel now (up to |
wait_available |
Wait up to |
flush |
Discard the samples waiting to be read (of every streaming channel: they stay aligned); returns how many |
write |
Queue samples (complex, full scale +-1) for transmission, after |
get_tx_stats |
The transmit stream's counts for a channel (default: its first): written, sent, underruns, pending... |
split_rx_gain_db |
How a total receive gain [dB] is shared between the stages: |
split_tx_gain_db |
How a total transmit gain [dB] is shared between the stages: {stage: gain} |
Attributes:
| Name | Type | Description |
|---|---|---|
is_controller |
bool
|
Whether this handle may change settings: always for a local device; a remote one may be a viewer |
rx_channels |
The channels the receive stream carries |
|
tx_channels |
The channels the transmit stream carries |
|
caps |
Capabilities
|
What this device can do (see pysdrlib.base.caps). Refined by the driver once open |
Source code in src/pysdrlib/base/device.py
is_controller
property
¶
Whether this handle may change settings: always for a local device; a remote one may be a viewer
caps
property
¶
What this device can do (see pysdrlib.base.caps). Refined by the driver once open
find
classmethod
¶
[DeviceInfo] of the units of this driver that are attached, for pysdrlib.find. A driver that can list them
overrides it (it must not open a radio, and takes addresses and/or timeout if it can look at a network:
see pysdrlib.find); by default there are none to list, and pysdrlib.open(name, ...) is how it is opened
Source code in src/pysdrlib/base/device.py
open ¶
Connect to device. serial=...: the unit with this serial number (where the driver can choose)
Source code in src/pysdrlib/base/device.py
initialize ¶
Tune the receiver (and the transmitter, where it has its own synthesizer) to cf and Fs, and set the gains.
rx_gain and tx_gain are fractions of the device's range, as set_rx_gain takes; rx_gain_db and tx_gain_db
are decibels, as set_rx_gain_db takes. Each is a number for the total, None (the defaults), or a dict of
stages, {"IF": 16, "BB": 20} (see rx_gain_stages(); a stage not named keeps the device's). Not both for one direction
Source code in src/pysdrlib/base/device.py
close ¶
Disconnect from device, stopping what is streaming first
Source code in src/pysdrlib/base/device.py
start_rx ¶
Start receiving. channels: which to stream together (default: the last ones, at first channel 0);
read(..., channel=...) reads them
Source code in src/pysdrlib/base/device.py
stop_rx ¶
Stop receiving
get_samples ¶
The low-level call: whatever has arrived since the last call (any number, possibly none, without waiting). Most
code wants read (an exact count, waiting for it, with the stream's metadata) or stream (blocks until you stop)
Source code in src/pysdrlib/base/device.py
start_tx ¶
Start transmitting (then write). channels: which to transmit together (default: the last ones, at first 0)
Source code in src/pysdrlib/base/device.py
stop_tx ¶
Stop transmitting
Source code in src/pysdrlib/base/device.py
set_rx_sample_rate ¶
Set the receive sample rate (the nominal one: a frequency correction is applied to what the hardware is asked
for). What else changes with it: caps.shared["rate"] (a device with one clock for both directions sets both).
flush: while receiving, discard the samples from before the change. Returns the rate in effect
Source code in src/pysdrlib/base/device.py
set_tx_sample_rate ¶
Set the transmit sample rate (see set_rx_sample_rate). Raises err.NotSupported on a receiver
get_rx_sample_rate ¶
get_tx_sample_rate ¶
Transmit sample rate [Hz] last set for this channel (None: never set)
set_rx_freq ¶
Set the receive center frequency (corrected by set_freq_correction). What else moves with it:
caps.shared["freq"] (channels sharing a synthesizer, or both directions on a device with one).
flush: while receiving, discard what was received before the change and the device's settling time
after it (caps.retune_settle), so the next read is all at the new frequency. Returns the frequency
Source code in src/pysdrlib/base/device.py
set_tx_freq ¶
Set the transmit center frequency (see set_rx_freq). Raises err.NotSupported on a receiver
get_rx_inverted ¶
Whether the receive spectrum comes mirrored, from a converter whose LO is above the signal (see caps.Chain):
flip it (samples[::-1] of the spectrum, or conjugate the samples) to read it the right way round
Source code in src/pysdrlib/base/device.py
get_rx_context ¶
How this channel's samples are received, for a recording to keep (RxContext): the frequency over the air and
the rate, the chain (its LO, whether it inverts), the reference correction and clock source, gains, antenna port
Source code in src/pysdrlib/base/device.py
set_level_calibration ¶
What a level in these samples is at the antenna port: dBm = dBFS + offset_db, where dBFS has full scale at
|x| = 1 in the complex64 samples read returns. Measured at, and holding for, the receive gains (and gain mode)
and frequency current now: get_rx_context carries it (level_offset, level_accuracy) while the gains are
unchanged and the frequency is within span, and says None otherwise. accuracy: a standard error [dB] (None:
not known). span: the width [Hz] centred on the frequency now that it holds over (None: the sample rate now, the
band it was measured in). offset_db None: forget it. Returns get_level_calibration
Source code in src/pysdrlib/base/device.py
get_level_calibration ¶
The level calibration given to set_level_calibration (None: none): {"offset_db", "accuracy", "freq", "span",
"gains", "gain_mode", "holds"}, where freq, span, gains and gain_mode are what it was made at, and holds says whether the gains and
frequency now are still those
Source code in src/pysdrlib/base/device.py
get_rx_freq ¶
Receive center frequency [Hz] last set for this channel (over the air, whatever converter is on the radio)
get_tx_freq ¶
Transmit center frequency [Hz] last set for this channel (None: never set)
set_freq_correction ¶
Error of the device's frequency reference in parts per million (positive: it runs fast). Tuning and
sample rate are both corrected, so set_rx_freq(f) gives f and set_rx_sample_rate(r) gives r (the rate only
where the device has continuous rates). Re-applies them. accuracy: how well ppm is known, a standard error
[ppm] (a calibration's; None: not known), which get_rx_context passes on so a reader can say what a frequency is worth
Source code in src/pysdrlib/base/device.py
get_freq_correction ¶
get_freq_correction_accuracy ¶
How well the reference's error is known [ppm, a standard error], as given to set_freq_correction (None: not known)
set_rx_gain ¶
The receive gain, as a fraction of the device's range: 0.0 its least, 1.0 its most (linear in dB), of the total
or of one stage. None: the defaults. The same fraction is the same place in the range on every device, so this is
the call for code that should run on more than one kind of radio: 40 dB is most of one radio's gain range, and a
third of another's. It takes a fraction, not decibels (for those, set_rx_gain_db): a value outside 0-1 raises
err.InvalidValue rather than being clamped, because it is nearly always decibels given to the wrong call, and
clamping would set the most gain. flush: while receiving, discard what was received before the change and its
settling time (see set_rx_freq). Returns the fraction in effect
Source code in src/pysdrlib/base/device.py
set_tx_gain ¶
The transmit gain, as a fraction of the device's range (see set_rx_gain). Returns the fraction in effect
get_rx_gain ¶
The receive gain last set, as a fraction of the device's range (None before a gain was set); get_rx_gain_db
has decibels
get_tx_gain ¶
The transmit gain last set, as a fraction of the device's range (None before a gain was set)
set_rx_gain_db ¶
The receive gain in decibels: a total (the device splits it across its stages) or one stage's. None: the
defaults. Decibels mean different things on different radios; for code that should run on any of them use
set_rx_gain. Returns the gain in effect (the total, or the stage's)
Source code in src/pysdrlib/base/device.py
set_tx_gain_db ¶
The transmit gain in decibels (see set_rx_gain_db). Returns the gain in effect
get_rx_gain_db ¶
The receive gain last set in decibels: a stage's, or the total of the stages (None before any was set)
get_tx_gain_db ¶
The transmit gain last set in decibels: a stage's, or the total of the stages (None before any was set)
get_rx_gains_db ¶
{stage name: gain [dB]} of what each receive stage was last set to (stages never set are missing)
get_tx_gains_db ¶
rx_gain_stages ¶
{stage name: GainStage} of the receiver, as the device names its stages (stage= of the gain calls)
tx_gain_stages ¶
rx_gain_modes ¶
tx_gain_modes ¶
set_rx_gain_mode ¶
"auto": the receiver sets its own gain (AGC), "manual": the gain last set. Raises err.NotSupported for a mode
the device lacks
set_tx_gain_mode ¶
get_rx_gain_mode ¶
Current receive gain mode, "manual" unless set_rx_gain_mode changed it
get_tx_gain_mode ¶
set_rx_bandwidth ¶
Receive analog filter bandwidth [Hz]: the narrowest the device has that is at least bandwidth (None: the
sample rate). Returns the bandwidth set
Source code in src/pysdrlib/base/device.py
set_tx_bandwidth ¶
Transmit analog filter bandwidth [Hz] (see set_rx_bandwidth). Returns the bandwidth set
get_rx_bandwidth ¶
get_tx_bandwidth ¶
rx_antennas ¶
tx_antennas ¶
rx_antenna_info ¶
Each receive antenna port and what is on it, as rx_antennas lists them (one entry, port None, when there is
just the one): [{"port", "chain", "antenna", "inverted", "selected"}]. chain: the name of the caps.Chain the
port goes through (None: none declared); antenna: Chain.describe() of the antenna fitted (None: not known;
pysdrlib.base.antennas.format_antenna makes text of it); inverted: whether the spectrum comes mirrored there
(see get_rx_inverted); selected: whether it is the port in use (None for each of several when none was ever
selected: the radio's default)
Source code in src/pysdrlib/base/device.py
tx_antenna_info ¶
set_rx_antenna ¶
Select a receive antenna port by name (see rx_antennas). Raises err.NotSupported for a name the device lacks
set_tx_antenna ¶
get_rx_antenna ¶
get_tx_antenna ¶
clock_sources ¶
set_clock_source ¶
Select the reference clock by name (see clock_sources). Raises err.NotSupported for a name the device lacks
Source code in src/pysdrlib/base/device.py
get_clock_source ¶
set_bias_t ¶
settings ¶
set_setting ¶
Apply one of settings() (to channel, where the setting is per channel). Returns the value set
Source code in src/pysdrlib/base/device.py
get_setting ¶
Value of a device specific setting listed in caps.settings. Raises err.NotSupported for an unknown name
Source code in src/pysdrlib/base/device.py
set_chains ¶
The hardware between this radio and the air (caps.Chain: an LNB, a converter, and the antenna on it), for a
radio on this machine (a server's are in its frontends file, pysdrlib.remote.frontends). Frequencies are over
the air from then on: the radio tunes the chain's IF. [] or None: back to the driver's own.
The radio stays where it is tuned: get_rx_freq (and a stream's metadata) then says what that is over the air
Source code in src/pysdrlib/base/device.py
get_chains ¶
get_caps ¶
read ¶
Exactly count samples (complex64), waiting for them. Raises err.Timeout after timeout seconds
(None: wait as long as the device delivers; a stalled device raises err.StreamStalled).
meta=True returns (samples, RxMeta): the first sample's index, where samples were lost, retunes...
channel: one of rx_channels, or several (a sequence): then a (channels, count) array whose rows start at
the same sample index (the same instant), and a list of RxMeta. (Samples one channel alone lost inside the block
move the rest of its row: see its gaps.)
on_drop="raise": raise err.Dropped if samples were lost inside this block (RxMeta.gaps), for a capture that
must be contiguous; the exception carries the block (.samples, .meta, .lost). None: return it anyway.
latest=True: the newest count samples at the current settings, for a display: what is queued before them
is skipped, and so is everything from before the last change of a setting (a retune, a new rate or gain...) and
its settling (caps.retune_settle); if not enough has arrived since, wait for it. The skipped samples are
counted in RxMeta.discarded, not as gaps. A remote viewer learns of its controller's changes from the stream,
so a read made as one is still on its way returns samples from before it, and RxMeta.freq says so
Source code in src/pysdrlib/base/device.py
read_into ¶
read into a complex64 array you own (filled completely). Returns its length, or (length, RxMeta)
Source code in src/pysdrlib/base/device.py
stream ¶
Blocks of count samples, in a with block that starts receiving (and stops it after) when it wasn't:
with sdr.stream(65536) as s:
for x in s: ...
flush: when already receiving, discard what was queued before the stream starts. on_drop: as read's, for
each block (an err.Dropped ends a for loop over the stream; call s.read() in a loop to carry on past one).
latest: each block is the newest (see read), for a display that would rather skip than fall behind
Source code in src/pysdrlib/base/device.py
sweep ¶
sweep(start, stop=None, step=None, count=65536, timeout=None, meta=False, channel=0, repeat=False, offset=0.0, skip=0, on_drop=None, retries=3)
Step the receiver across frequencies, yielding (frequency, samples): count samples at each, all received
after its retune and settling (set_rx_freq(..., flush=True)):
for freq, x in sdr.sweep(88e6, 108e6, count=65536): ... # start to stop (inclusive)
for freq, x in sdr.sweep([162.4e6, 162.55e6], count=4096): ... # or a list of frequencies
step: Hz between steps (default: 80% of the sample rate, as a band's edges roll off). offset: tune this far
above each frequency and shift the samples back, so the device's DC spike (its LO leaking through) lands
offset Hz off the middle of each step's spectrum, not on it (the samples of an inverting chain stay mirrored,
see get_rx_inverted). skip: samples to discard after each retune on top
of the device's own settling (caps.retune_settle). repeat: sweep again and again, until the loop breaks.
meta: yield (frequency, samples, RxMeta). on_drop: "raise" (as read's), or "retry": capture a step again
when samples were lost inside it, up to retries times, then raise err.Dropped. Starts receiving if it wasn't,
and stops it again when the sweep ends (or the loop breaks)
Source code in src/pysdrlib/base/device.py
find_gain ¶
find_gain(channel=0, count=65536, max_clip=0.0001, clip_level=0.98, headroom_db=0.0, confirm=None, timeout=None, skip=0)
The highest receive gain at which the signal doesn't clip, found by bisecting the gains the device can set (clipping only grows with gain), and set. Returns it as a fraction of the gain range:
sdr.find_gain() # -> 0.62: channel 0's gain, now in effect
sdr.find_gain(channel=[0, 1]) # -> {0: 0.62, 1: 0.71}: each channel's, from aligned reads
At each step the gain is set (skipping its settling, flush=True), skip more samples are discarded, and
count are read; a block clips when more than max_clip of its samples have I or Q at clip_level of full
scale or beyond. For a bursty signal (TDMA, WiFi, BLE...) count must span a burst: a window that misses
them reads as clean. confirm: then read this many samples (default 4 * count) at the gain found, and step down
while that clips. headroom_db: back off this much below the gain found.
Channels whose gain is one setting (caps.shared["gain"]) get the gain at which none clips.
The gain mode is set to "manual" (an AGC would undo it). Starts receiving if it wasn't, and stops it again; when
already receiving, the channels must be among rx_channels. The result holds while the signal stays as it was
Source code in src/pysdrlib/base/device.py
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
read_available ¶
What is queued for a channel now (up to count), without waiting: (samples, index of the first). Stops at a
gap, so the samples are contiguous; the next call's index shows the jump. For relaying a stream (the remote
server); read is the way to read one
Source code in src/pysdrlib/base/device.py
wait_available ¶
Wait up to timeout seconds until a channel has count samples queued; returns how many are queued (fewer
than count: the time ran out). For relaying a stream (the remote server) without polling; read is the
way to read one
Source code in src/pysdrlib/base/device.py
flush ¶
Discard the samples waiting to be read (of every streaming channel: they stay aligned); returns how many
Source code in src/pysdrlib/base/device.py
write ¶
Queue samples (complex, full scale +-1) for transmission, after start_tx. Blocks while the queue is full,
up to timeout seconds for the whole call (None: until all are queued). Returns how many (per channel) were
queued: fewer when the time ran out, or stop_tx or close (from another thread) ended the stream. A queue
that takes nothing for stall_timeout seconds raises err.StreamStalled (err.Lost: the device is gone).
channel: one of tx_channels (default: the stream's only one), or several with a (channels, n) array: those
rows go out sample for sample together. After a short count, carry on from it (samples[:, n:]): a channel
that had queued more than that isn't sent those again
Source code in src/pysdrlib/base/device.py
get_tx_stats ¶
The transmit stream's counts for a channel (default: its first): written, sent, underruns, pending...
Source code in src/pysdrlib/base/device.py
split_rx_gain_db ¶
How a total receive gain [dB] is shared between the stages: _split_gain's answer, {stage: gain}