rtlsdr_helper

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.

class sk_dsp_comm.rtlsdr_helper.RTLSDR_stream(rtl_in=0, rtl_fs=2400000.0, fc=103900000.0, gain=40, rtl_buffer_size=32768, audio_out=1, audio_buffsize=4096, audio_fs=48000)[source]

Class used to set up an RTLSDR stream object

Methods

clear_buffer() Clears the circular buffer used by the audio sink
clear_logs() Used in Mono FM Receiver example to clear widget logs
get_bandwidth() returns the bandwidth of the SDR
get_buffer() Returns the data_sink buffer at the current index
get_center_freq() Returns the center frequency of the SDR
get_data_out_async() This method asynchronously returns data from the data_sink buffer when it is full.
get_gain() Returns the receiver gain of the SDR
get_processed_stage1_frame() Async method that can be used to get a frame of decimated data after the callback.
get_rf_frame() Async method that can be used to get a frame of incoming RF samples.
get_sample_rate() Returns the sample rate of the SDR
get_stage1_frame() Async method that can be used to get a frame of decimated data after the stage 1 decimation filter.
get_stage2_frame() Async method that can be used to get a frame of decimated data after the stage 2 decimation filter.
interactive_FM_Rx([fc, gain, audio_out, …]) Sets up interactive mono FM example
plot_processed_stage1([NFFT, FC, w, h]) Async method that can be used to plot the PSD of a frame of decimated and processed samples from the SDR.
plot_rf([NFFT, w, h]) Async method that can be used to plot the PSD of a frame of incoming samples from the SDR.
plot_stage1([NFFT, w, h]) Async method that can be used to plot the PSD of a frame of decimated samples from the SDR.
plot_stage2([NFFT, FC, w, h]) Async method that can be used to plot the PSD of a frame of data after the stage 2 decimator.
reset_data_out_queue() Clears data_sink queue
run_plot_processed_stage1_stream([NFFT, …]) This method can be used to instantiate a spectrum analyzer after the callback during a stream.
run_plot_rf_stream([NFFT, refresh_rate, …]) This method can be used to instantiate a spectrum analyzer of the RF input during a stream.
run_plot_stage1_stream([NFFT, refresh_rate, …]) This method can be used to instantiate a spectrum analyzer after stage 1 during a stream.
run_plot_stage2_stream([NFFT, refresh_rate, …]) This method can be used to instantiate a spectrum analyzer after the callback during a stream.
run_user_stream(callback, M1, M2[, b, …]) Starts a user stream.
set_NFFT(NFFT) Sets the FFT size for any running spectrum analyzer
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.
set_audio_gain(gain) Sets the audio gain value used to scale the PyAudio volume.
set_audio_gain_db(gain) Sets the audio gain value (in dB) used to scale the audio_sink output volume
set_audio_in(audio_in) Selects the audio input device.
set_audio_out(audio_out) Selects the audio output device.
set_fc(fc) Sets tuning center frequency value (in Hz) on the SDR
set_fc_mhz(fc) Sets tuning center frequency value (in MHz) on the SDR
set_fs(fs) Sets the sample rate (in samples/second) to the SDR This should generally be left at 2.4 Msps.
set_gain(gain) Sets receiver gain value (in dB) on the SDR
set_refresh_rate(refresh_rate) Sets the refresh_rate (in Hz) of any running spectrum analyzer
set_rtl_buffer_size(rtl_buffer_size) Sets the circular buffer size used by the audio_sink and the data_sink.
set_stage1_coeffs(b[, a, zi]) Can be used to set the stage 1 decimation filter coefficients.
set_stage2_coeffs(bb[, aa, zi]) Can be used to set the stage 2 decimation filter coefficients.
show_logs() Used in Mono FM Receiver example to show logs inside of a widget
stop() Stops a running stream.
stop_all() Stops any running spectrum analyzer and stops streaming
stop_all_plots() Stops any running spectrum analyzer
stop_processed_stage1_plot() Stops updating a callback spectrum analyzer instance
stop_rf_plot() Stops updating an RF spectrum analyzer instance
stop_stage1_plot() Stops updating a stage 1 spectrum analyzer instance
stop_stage2_plot() Stops updating a stage 2 spectrum analyzer instance
toggle_invert() Toggles between black and white background of a running spectrum analyzer
clear_buffer()[source]

Clears the circular buffer used by the audio sink

clear_logs()[source]

Used in Mono FM Receiver example to clear widget logs

get_bandwidth()[source]

returns the bandwidth of the SDR

get_buffer()[source]

Returns the data_sink buffer at the current index

get_center_freq()[source]

Returns the center frequency of the SDR

get_data_out_async()[source]

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.

get_gain()[source]

Returns the receiver gain of the SDR

get_processed_stage1_frame()[source]

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

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

get_rf_frame()[source]

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

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

get_sample_rate()[source]

Returns the sample rate of the SDR

get_stage1_frame()[source]

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

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

get_stage2_frame()[source]

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

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

interactive_FM_Rx(fc=103900000.0, gain=40, audio_out=1, audio_buffsize=4096, audio_fs=48000)[source]

Sets up interactive mono FM example

plot_processed_stage1(NFFT=1024, FC=0, w=6, h=5)[source]

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 = RTLSDR_stream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_processed_stage1(1024,0,6,5)

This will return a spectrum plot

plot_rf(NFFT=1024, w=6, h=5)[source]

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 = RTLSDR_stream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_rf(1024,6,5)

This will return a spectrum plot

plot_stage1(NFFT=1024, w=6, h=5)[source]

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 = RTLSDR_stream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_stage1(1024,6,5)

This will return a spectrum plot

plot_stage2(NFFT=1024, FC=0, w=6, h=5)[source]

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 = RTLSDR_stream() >>> sdr_stream.run_user_stream(callback,10,5) >>> await sdr_stream.plot_processed_stage1(1024,0,6,5)

This will return a spectrum plot

reset_data_out_queue()[source]

Clears data_sink queue

run_plot_processed_stage1_stream(NFFT=1024, refresh_rate=2, invert=True, w=8, h=5)[source]

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

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 = RTLSDR_stream() >>> 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()
run_plot_rf_stream(NFFT=1024, refresh_rate=2, invert=True, w=8, h=5)[source]

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

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 = RTLSDR_stream() >>> 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()
run_plot_stage1_stream(NFFT=1024, refresh_rate=2, invert=True, w=8, h=5)[source]

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

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 = RTLSDR_stream() >>> 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()
run_plot_stage2_stream(NFFT=1024, refresh_rate=2, invert=True, w=8, h=5)[source]

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

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 = RTLSDR_stream() >>> 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()
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)[source]

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.

set_NFFT(NFFT)[source]

Sets the FFT size for any running spectrum analyzer

set_audio_buffsize(audio_buffsize)[source]

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

set_audio_fs(audio_fs)[source]

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_gain(gain)[source]

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

set_audio_gain_db(gain)[source]

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

set_audio_in(audio_in)[source]

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)[source]

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

set_fc(fc)[source]

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

set_fc_mhz(fc)[source]

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

set_fs(fs)[source]

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.

set_gain(gain)[source]

Sets receiver gain value (in dB) on the SDR

set_refresh_rate(refresh_rate)[source]

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

set_rtl_buffer_size(rtl_buffer_size)[source]

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_stage1_coeffs(b, a=[1], zi=False)[source]

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)[source]

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

show_logs()[source]

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

stop()[source]

Stops a running stream.

stop_all()[source]

Stops any running spectrum analyzer and stops streaming

stop_all_plots()[source]

Stops any running spectrum analyzer

stop_processed_stage1_plot()[source]

Stops updating a callback spectrum analyzer instance

stop_rf_plot()[source]

Stops updating an RF spectrum analyzer instance

stop_stage1_plot()[source]

Stops updating a stage 1 spectrum analyzer instance

stop_stage2_plot()[source]

Stops updating a stage 2 spectrum analyzer instance

toggle_invert()[source]

Toggles between black and white background of a running spectrum analyzer

sk_dsp_comm.rtlsdr_helper.complex2wav(filename, rate, x)[source]

Save a complex signal vector to a wav file for compact binary storage of 16-bit signal samples. The wav left and right channels are used to save real (I) and imaginary (Q) values. The rate is just a convent way of documenting the original signal sample rate.

complex2wav(filename,rate,x)

Mark Wickert April 2014

sk_dsp_comm.rtlsdr_helper.discrim(x)[source]

function disdata = discrim(x) where x is an angle modulated signal in complex baseband form.

Mark Wickert

sk_dsp_comm.rtlsdr_helper.fsk_BEP(rx_data, m, flip)[source]

Estimate the BEP of the data bits recovered by the RTL-SDR Based FSK Receiver.

The reference m-sequence generated in Python was found to produce sequences running in the opposite direction relative to the m-sequences generated by the mbed. To allow error detection the reference m-sequence is flipped.

Mark Wickert April 2014

sk_dsp_comm.rtlsdr_helper.mono_FM(x, fs=2400000.0, file_name='test.wav')[source]

Decimate complex baseband input by 10 Design 1st decimation lowpass filter (f_c = 200 KHz)

sk_dsp_comm.rtlsdr_helper.pilot_PLL(xr, fq, fs, loop_type, Bn, zeta)[source]

Mark Wickert, April 2014

sk_dsp_comm.rtlsdr_helper.sccs_bit_sync(y, Ns)[source]
Symbol synchronization algorithm using SCCS
y = baseband NRZ data waveform

Ns = nominal number of samples per symbol

Reworked from ECE 5675 Project Translated from m-code version Mark Wickert April 2014

sk_dsp_comm.rtlsdr_helper.stereo_FM(x, fs=2400000.0, file_name='test.wav')[source]

Stereo demod from complex baseband at sampling rate fs. Assume fs is 2400 ksps

Mark Wickert July 2017

sk_dsp_comm.rtlsdr_helper.wav2complex(filename)[source]

Return a complex signal vector from a wav file that was used to store the real (I) and imaginary (Q) values of a complex signal ndarray. The rate is included as means of recalling the original signal sample rate.

fs,x = wav2complex(filename)

Mark Wickert April 2014