Sunday, August 11, 2024

Running An RTL Based SDR From Python

 

Running a RTL Based Stick from python can be useful to quickly test out settings. This can come in handy once you realize that even though you might type in a sampling rate of: “2.345678e6”, that floating point number has to be converted to an integer to actually program the chips inside the RTL Stick and floating point can’t be represented by an integer exactly, so some rounding will have to occur.

Exact sampling rates are sometimes desired when demodulating a known digital signal and you want the sampling rate to be an exact multiple of the signals bit rate. The question arises: “How far off will it be?”.

There are, of course, many sources of error in any transmitter / receiver arrangement, from: Clock Source Error, Temperature Drift, sometimes Acceleration Drift when dealing with rockets, etc.

Most digital formats have a decent error tolerance on clock accuracy. For instance, the common Serial Port of a PC has an error tolerance of around 6% in the clock speeds, which can be proportioned off to 3% error in the transmitter and receiver, which can combine for a total of 6% error.

Many digital encodings for wireless include an embedded bit clock so that the clock can be recreated at the receiver end [1].

 

Installation

A library called pyrtlsdr [2] is a wrapper around the standard RTL driver (librtlsdr) If you have a working install of the librtlsdr, then pyrtlsdr should work just fine and all you have to do is to install the pyrtlsdr python package with the usual python command,

pip install pyrtlsdr

You can either install this in your Radioconda environment, or just on your normal PC's Python environment as the package does not require Radioconda or GNURadio to function.


Using Straight Python Control

A simple program is shown below that starts the library, sets a sample rate (and reads it back so we can see the actual rate set). Sets a center frequency (and reads it back so we can see the frequency set). And finally sets a RF Gain (and reads it back so we can see the actual gain set).

from rtlsdr import RtlSdr

sdr = RtlSdr()

sdr.sample_rate = 2e6
print(f'Actual Sample Rate = {sdr.sample_rate} Hz')

sdr.center_freq = 120.5589876e6
print(f'Actual frequency = {sdr.center_freq} Hz')

sdr.gain = 30
print(f'Actual gain = {sdr.gain}')

sdr.close()

Figure 1 - A simple program to test the accuracy of various settings.

 

Found Rafael Micro R820T/2 tuner
Actual Sample Rate = 2000000.052981908 Hz
Actual frequency = 120559000 Hz
Actual gain = 29.7

Figure 2 - The output produced from running the program in figure 1.

 

Note: I have noticed that as of the time of this writing, the "Gain" readback of a RTL-SDR Blog V4 is not correct and will always return a value of '0'. The Gain sets correctly, only the readback is incorrect.

The library can also be used to get all the possible RF Gain values and manipulate samples from the RTL Stick, etc. But I find it most useful in just getting a handle on setting various parameters quickly. While the working examples of the library may be somewhat scarce, the library documentation itself is quite complete.

 

Bonus

If you are interested in Python programming complete radios in Python but without using GNURadio, you can also check out the PYSDR Project website for an excellent starting point [3].


References

[1] Manchester Encoding is an example of a digital encoding scheme that embeds the clock with the data stream. This makes the demodulation of the original digital data to be mostly independent of the receivers bit-clock. https://en.wikipedia.org/wiki/Manchester_code

[2] pyrtlsdr Project - https://pypi.org/project/pyrtlsdr/

[3] PYSDR Project - https://pysdr.org/

No comments:

Post a Comment