Skip to content

Simulated radios

pysdrlib.open("sim") is a radio with no hardware behind it: tones, noise and bursts built to order, on pysdrlib's real streaming path (see pysdrlib.sim). Simulated radios can also share an air (pysdrlib.air): what one transmits, every one in it hears, shifted to its frequency, resampled to its rate, and attenuated by the air's loss_db, then through its own noise, gain and ADC.

Exact, in one process

A sim that isn't realtime counts its own samples, so a transmitter written and a receiver pumped in one process give the same samples every run: a test of what reads a radio, with a signal that came through a transmitter.

air = Air("example", directory=directory, loss_db=6)   # a name; the settings every device in it shares
tx = pysdrlib.device("sim", realtime=False)            # not realtime: on their own sample clocks, exact and repeatable
rx = pysdrlib.device("sim", realtime=False, noise=-60)
tx.open(air=air, transmit=True)
rx.open(air=air)
rx.initialize(cf=433.90e6, Fs=2e6)

tx.set_tx_freq(433.92e6)                 # 20 kHz above the receiver
tx.set_tx_sample_rate(250e3)             # at a rate of its own
tx.set_tx_gain_db(41)                    # the "PA" stage: 47 dB is full scale, so 6 dB down
tx.start_tx()
t = np.arange(25_000) / 250e3            # 100 ms of a 5 kHz tone
tx.write((0.8 * np.exp(2j * np.pi * 5e3 * t)).astype(np.complex64))

rx.pump(4)                               # deliver 4 blocks (262 ms at 2 MS/s): what was sent, then silence
y = rx.get_samples()
on = np.abs(y) > 0.1
peak = np.fft.fftfreq(65536, 1 / 2e6)[np.argmax(np.abs(np.fft.fft(y[:65536])))]
print(f"heard {on.sum() / 2e6 * 1e3:.1f} ms at {peak / 1e3:+.1f} kHz, {20 * np.log10(np.abs(y[on]).mean()):.1f} dBFS")
tx.close()
rx.close()

In time, across processes

Realtime sims are on the wall clock. Writing is paced to the transmit rate, and a pause in writing is silence. The air is shared memory, so a transmitter in one process (or a pysdrlib server --open sim:air=lab,transmit=true) is heard by a receiver in another, such as pyspecan opening sim with air="lab". A realtime receiver hears the air 0.1 s late (Air(latency=)), as a radio's transfers are.

rx = pysdrlib.open("sim", air="example-rt")             # realtime: on the wall clock, as radios are
rx.initialize(cf=915e6, Fs=1e6)
rx.start_rx()
tx = pysdrlib.open("sim", air="example-rt", transmit=True)   # (this could be another process: the air is shared memory)
tx.set_tx_freq(915.1e6)
tx.set_tx_sample_rate(1e6)
tx.start_tx()
tx.write(np.full(200_000, 0.5 + 0j, np.complex64))      # 0.2 s: write waits, as a radio's queue does
print([d.label for d in pysdrlib.find(driver="sim", virtual=True)])
y = rx.read(500_000)                                     # 0.5 s; the air is heard 0.1 s late
tx.close()
rx.close()
print(f"heard {(np.abs(y) > 0.25).sum() / 1e6:.2f} s")

pysdrlib.find(virtual=True) lists a simulated radio in each air with a transmitter in it: a transmitting sim joins the listing at its first write, not when it opens. Each transmit channel is also a shm stream, so pysdrlib.open("shm@air-lab.<pid>-0") receives one transmitter raw.

Channels of your own

Anything can transmit into an air, or receive from it, without a Sim: air.tx_channel(Fs, cf, clock) and air.rx_channel() (close them when done). The clocks, lifetimes and limits are on the pysdrlib.air page.