sdr_helper.rtlsdr

Support functions for the RTL-SDR using pyrtlsdr

Copyright (c) July 2017, Mark Wickert All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.

Module Contents

Classes

RTLSDRStream

Class used to set up an RTLSDR stream object

class sdr_helper.rtlsdr.RTLSDRStream(rtl_in=0, rtl_fs=2400000.0, fc=103900000.0, gain=40, rtl_buffer_size=2**15, audio_out=1, audio_buffsize=4096, audio_fs=48000)

Bases: object

Class used to set up an RTLSDR stream object

_interaction(stream)

Enables Jupyter Widgets for mono FM example

interactive_fm_rx(fc=103900000.0, gain=40, audio_out=1, audio_buffsize=4096, audio_fs=48000)

Sets up interactive mono FM example

_interact_frequency(freq_val, min_freq=87.5, max_freq=108, freq_step=0.2)

Sets up tuning frequency slider widget for Mono FM Example

_interact_audio_gain(gain_val=0, min_gain=-60, max_gain=6, gain_step=0.1)

Sets up audio gain slider widget for Mono FM Example

async _get_rx_data()

Gets samples from RTLSDR and decimates by 10 for mono FM example

async _process_rx_data()

Processes decimated samples, and decimates further to audio rate. Implements an FM discriminator for the mono FM example.

async _get_rx_data_user()

Used by run_user_stream() method. Asynchronously reads in samples from RTLSDR and implements the stage 1 decimator. The stage 1 decimator can be defined by the user in run_user_stream() or a default decimator can be used. This is a private method that is only used internally. Decimated samples are stored in the rx_data queue which is consumed by the _process_rx_data_user private method.

async _process_rx_data_user(callback)

Used by run_user_stream() method. Consumed decimated samples from stage 1 decimator stored in the rx_data queue. Passes the data along to a user defined callback. The processed samples are passed along to either the audio sink or the data sink. The audio sink contains a stage 2 decimator and outputs to an audio device via PyAudio. The data sink stores processed samples in a buffer that can be read out asynchronously. This is a private method that is used internally.

_write_circ_buff(samples)

Private method used to write samples to a circular buffer. This circular buffer takes in audio-rate samples from the _process_rx_user_data method and _read_circ_buff reads the samples back out asynchronously in a PyAudio thread. This method increments a write pointer by the length of the samples being written to the buffer and wraps the pointer when the buffer is filled.

async _write_circ_buff_async(samples)

Private method used to asynchronously store processed data from the user callback to a buffer when the data sink is being used. This method wraps the buffer and increments the buffer pointer.

_read_circ_buff(frame_count)

Private method used to read samples from the circular buffer. This is used by the audio sink to consume audio-rate samples. This method handles incrementing a read pointer and wrapping the circular buffer.

async _audio()

private method that starts a PyAudio Thread

_audio_callback(in_data, frame_count, time_info, status)

private audio callback method that is used by the PyAudio thread in the audio sink. Reads samples out of the circular buffer and sends the samples out an audio device.

async _start_streaming()

Async method used to start coroutine for the Mono FM example

async _start_user_stream(callback, m1, m2, b, a, stage1_ic, bb, aa, stage2_ic, audio_sink, user_var)

Async method used by run_user_stream method to start a coroutine running all of the different async stages in the chain.

run_user_stream(callback, m1, m2, b=False, stage1_ic=False, a=False, bb=False, stage2_ic=False, aa=False, audio_sink=True, user_var=None)

Starts a user stream. A user stream follows the flow diagram in the class description. When audio_sink is True, the audio_sink blocks will be used and when audio_sink is False, the data sink block will be used. For any optional parameters set to false, default values will be used for stage 1 or stage 2 filters. The stop() method may be used to stop the stream.

async get_data_out_async()

This method asynchronously returns data from the data_sink buffer when it is full. This is used in the data_sink mode (audio_sink=False).

The following example shows how to continuously stream data and handle the buffer when it is full. The buffer will automatically get rewritten whenever it runs out of space, so the returned buffer must be handled whenever it is filled.

async plot_rf(nfft=2**10, w=6, h=5)

Async method that can be used to plot the PSD of a frame of incoming samples from the SDR. This essentially acts as a power spectrum “probe” right before the Stage 1 decimator. Make sure a stream is running before calling this method. This method must be awaited when called.

parameters: NFFT: Number of points used in the spectrum plot. Should be 2^N value w: width of figure h: height of figure

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_rf(1024,6,5)

This will return a spectrum plot

async plot_stage1(nfft=2**10, w=6, h=5)

Async method that can be used to plot the PSD of a frame of decimated samples from the SDR. This essentially acts as a power spectrum “probe” after the stage 1 decimator and before the user-defined callback. Make sure a stream is running before calling this method. This method must be awaited when called.

parameters: NFFT: Number of points used in the spectrum plot. Should be 2^N value w: width of figure h: height of figure

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_stage1(1024,6,5)

This will return a spectrum plot

async plot_processed_stage1(nfft=2**10, fc=0, w=6, h=5)

Async method that can be used to plot the PSD of a frame of decimated and processed samples from the SDR. This essentially acts as a power spectrum “probe” after the user-defined callback and before the audio_sink or data_sink blocks.1 decimator and before the user-defined callback. Make sure a stream is running before calling this method. This method must be awaited when called.

parameters: NFFT: Number of points used in the spectrum plot. Should be 2^N value FC: Frequency offset for plotting w: width of figure h: height of figure

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_processed_stage1(1024,0,6,5)

This will return a spectrum plot

async plot_stage2(nfft=2**10, fc=0, w=6, h=5)

Async method that can be used to plot the PSD of a frame of data after the stage 2 decimator. This essentially acts as a power spectrum “probe” after the stage2 decimator Make sure a stream is running before calling this method. This method must be awaited when called.

parameters: NFFT: Number of points used in the spectrum plot. Should be 2^N value FC: Frequency offset for plotting w: width of figure h: height of figure

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_processed_stage1(1024,0,6,5)

This will return a spectrum plot

async _plot_rf_stream(w, h)

Private method used to create and update a spectrum analyzer plot of the RF input using matplotlib.

async _plot_rf_stream_bokeh(w, h)

Private method used the create a spectrum analyzer of the RF input using a bokeh plot.

async _update_rf_plot()

Private method used to control the refresh rate of the rf spectrum analyzer plot.

async _start_plot_rf_stream(nfft, refresh_rate, invert, w, h)

Private method used to initialize and start the RF spectrum analyzer.

run_plot_rf_stream(nfft=2**10, refresh_rate=2, invert=True, w=8, h=5)

This method can be used to instantiate a spectrum analyzer of the RF input during a stream. Call the stop_plot_rf_plot method in order to stop the plot from updating. Only one spectrum analyzer instance my be running at once. This only works when using %pylab widget or %pylab notebook

parameters:

NFFT: fftsize used in plotting refresh_rate: defines how often the spectrum analyzer updates (in Hz) invert: Inverts the background to black when true or leaves it white when false w: width of figure h: height of figure

Example: >>> %pylab widget >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> sdr_stream.run_plot_rf_stream(1024,2,True,8,5)

>>> sdr_stream.stop_rf_plot()
>>> sdr_stream.stop()
async _plot_stage1_stream(w, h)

Private method used to create and update a spectrum analyzer plot after the stage 1 decimator using matplotlib.

async _plot_stage1_stream_bokeh(w, h)

Private method used the create a spectrum analyzer after the stage 1 decimator using a bokeh plot.

async _update_stage1_plot()

Private method used to control the refresh rate of the stage 1 spectrum analyzer plot.

async _start_plot_stage1_stream(nfft, refresh_rate, invert, w, h)

Private method used to initialize and start the stage 1 spectrum analyzer.

run_plot_stage1_stream(nfft=2**10, refresh_rate=2, invert=True, w=8, h=5)

This method can be used to instantiate a spectrum analyzer after stage 1 during a stream. Call the stop_plot_rf_plot method in order to stop the plot from updating. Only one spectrum analyzer instance my be running at once. This only works when using %pylab widget or %pylab notebook

parameters:

NFFT: fftsize used in plotting refresh_rate: defines how often the spectrum analyzer updates (in Hz) invert: Inverts the background to black when true or leaves it white when false w: width of figure h: height of figure

Example: >>> %pylab widget >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> sdr_stream.run_plot_stage1_stream(1024,2,True,8,5)

>>> sdr_stream.stop_stage1_plot()
>>> sdr_stream.stop()
async _plot_processed_stage1_stream(w, h)

Private method used to create and update a spectrum analyzer plot after the callback using matplotlib.

async _plot_processed_stage1_stream_bokeh(w, h)

Private method used the create a spectrum analyzer after the callback using a bokeh plot.

async _update_processed_stage1_plot()

Private method used to control the refresh rate of the callback spectrum analyzer plot.

async _start_plot_processed_stage1_stream(nfft, refresh_rate, invert, w, h)

Private method used to initialize and start the callback spectrum analyzer.

run_plot_processed_stage1_stream(nfft=2**10, refresh_rate=2, invert=True, w=8, h=5)

This method can be used to instantiate a spectrum analyzer after the callback during a stream. Call the stop_plot_rf_plot method in order to stop the plot from updating. Only one spectrum analyzer instance my be running at once. This only works when using %pylab widget or %pylab notebook

parameters:

NFFT: fftsize used in plotting refresh_rate: defines how often the spectrum analyzer updates (in Hz) invert: Inverts the background to black when true or leaves it white when false w: width of figure h: height of figure

Example: >>> %pylab widget >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> sdr_stream.run_plot_processed_stage1_stream(1024,2,True,8,5)

>>> sdr_stream.stop_processed_stage1_plot()
>>> sdr_stream.stop()
async _plot_stage2_stream(w, h)

Private method used to create and update a spectrum analyzer plot after the stage 2 decimator using matplotlib.

async _plot_stage2_stream_bokeh(w, h)

Private method used the create a spectrum analyzer after the stage 2 decimator using a bokeh plot.

async _update_stage2_plot()

Private method used to control the refresh rate of the stage 2 spectrum analyzer plot.

async _start_plot_stage2_stream(NFFT, refresh_rate, invert, w, h)

Private method used to initialize and start the stage 2 spectrum analyzer.

run_plot_stage2_stream(NFFT=2**10, refresh_rate=2, invert=True, w=8, h=5)

This method can be used to instantiate a spectrum analyzer after the callback during a stream. Call the stop_plot_rf_plot method in order to stop the plot from updating. Only one spectrum analyzer instance my be running at once. This only works when using %pylab widget or %pylab notebook

parameters:

NFFT: fftsize used in plotting refresh_rate: defines how often the spectrum analyzer updates (in Hz) invert: Inverts the background to black when true or leaves it white when false w: width of figure h: height of figure

Example: >>> %pylab widget >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> sdr_stream.run_plot_stage2_stream(1024,2,True,8,5)

>>> sdr_stream.stop_stage2_plot()
>>> sdr_stream.stop()
stop_rf_plot()

Stops updating an RF spectrum analyzer instance

stop_stage1_plot()

Stops updating a stage 1 spectrum analyzer instance

stop_processed_stage1_plot()

Stops updating a callback spectrum analyzer instance

stop_stage2_plot()

Stops updating a stage 2 spectrum analyzer instance

stop_all()

Stops any running spectrum analyzer and stops streaming

stop_all_plots()

Stops any running spectrum analyzer

show_logs()

Used in Mono FM Receiver example to show logs inside of a widget

clear_logs()

Used in Mono FM Receiver example to clear widget logs

_decimate(x, m, fs=2400000.0, stage=1)

Private method used to decimate a signal for the Mono FM Receiver Example

_discrim(x)

Private method used by Mono FM Receiver example discriminate FM signal

Mark Wickert

reset_data_out_queue()

Clears data_sink queue

set_fc_mhz(fc)

Sets tuning center frequency value (in MHz) on the SDR

set_fc(fc)

Sets tuning center frequency value (in Hz) on the SDR

set_gain(gain)

Sets receiver gain value (in dB) on the SDR

set_audio_gain_db(gain)

Sets the audio gain value (in dB) used to scale the audio_sink output volume

set_fs(fs)

Sets the sample rate (in samples/second) to the SDR This should generally be left at 2.4 Msps. The radio can only operate at specific rates.

clear_buffer()

Clears the circular buffer used by the audio sink

set_rtl_buffer_size(rtl_buffer_size)

Sets the circular buffer size used by the audio_sink and the data_sink. When the audio_sink is used, this should be set to a fairly high number (around 2^15). When the data_sink is used, the buffer size can be changed to accommodate the scenario.

set_audio_buffsize(audio_buffsize)

Sets the buffer size used by PyAudio to consume frames processed audio frames from the circular buffer.

set_audio_fs(audio_fs)

Sets the audio sample rate. When the audio sink is used this should be equal to the radio sample rate (fs) / stage 1 decimation factor / stage 2 decimation factor

set_audio_in(audio_in)

Selects the audio input device. This is not used in the class, but should be set to a valid audio input.

set_audio_out(audio_out)

Selects the audio output device. Use sk_dsp_comm.rtlsdr_helper.pah.available_devices() to get device indices.

set_audio_gain(gain)

Sets the audio gain value used to scale the PyAudio volume.

stop()

Stops a running stream.

set_refresh_rate(refresh_rate)

Sets the refresh_rate (in Hz) of any running spectrum analyzer

set_stage1_coeffs(b, a=[1], zi=False)

Can be used to set the stage 1 decimation filter coefficients. This can be used during an active stream.

set_stage2_coeffs(bb, aa=[1], zi=False)

Can be used to set the stage 2 decimation filter coefficients. This can be used during an active stream.

set_nfft(nfft)

Sets the FFT size for any running spectrum analyzer

toggle_invert()

Toggles between black and white background of a running spectrum analyzer

async get_stage1_frame()

Async method that can be used to get a frame of decimated data after the stage 1 decimation filter.

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> stage1_data_frame = await sdr_stream.get_stage1_frame()

async get_rf_frame()

Async method that can be used to get a frame of incoming RF samples.

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> rf_data_frame = await sdr_stream.get_stage1_frame()

async get_processed_stage1_frame()

Async method that can be used to get a frame of decimated data after the callback.

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> callback_data_frame = await sdr_stream.get_stage1_frame()

async get_stage2_frame()

Async method that can be used to get a frame of decimated data after the stage 2 decimation filter.

Example: >>> sdr_stream = RTLSDRStream() >>> sdr_stream.run_user_stream(callback,10,5) >>> stage2_data_frame = await sdr_stream.get_stage2_frame()

get_center_freq()

Returns the center frequency of the SDR

get_gain()

Returns the receiver gain of the SDR

get_sample_rate()

Returns the sample rate of the SDR

get_bandwidth()

returns the bandwidth of the SDR

get_buffer()

Returns the data_sink buffer at the current index