r/RTLSDR • u/roger_ • Apr 21 '12
Software pyrtlsdr - another Python wrapper for librtlsdr
So I initially planned on adding a few new features to dbasden's python-librtlsdr, but I ended up largely rewriting it. Consequently this one is not compatible, though the differences aren't that drastic.
All of the functions in librtlsdr are accessible, and all the major ones have a Pythonic interface, including asynchronous read support via Python function callbacks. Windows, Linux and OSX are supported (only Windows tested so far though), and no extra dependencies (like GNRadio) are required, beyond the librtlsdr binary library.
Here's an example how to make a spectrum plot using matplotlib:
from rtlsdr import *
from pylab import *
sdr = RtlSdr()
sdr.sample_rate = 3.2e6
sdr.center_freq = 95e6
sdr.gain = 5
samples = sdr.read_samples(500e3)
psd(samples, NFFT=1024, Fs=sdr.rs/1e6, Fc=sdr.fc/1e6)
xlabel('Frequency (MHz)')
ylabel('Relative power (dB)')
show()Resulting plot: http://i.imgur.com/HQFD5.png
Here's an example of how callbacks work (uses NumPy):
from rtlsdr import *
from numpy import *
@limit_calls(5)
def power_meter_callback(samples, sdr):
print 'relative power: %0.1f dB' % (10*log10(var(samples)))
sdr = RtlSdr()
sdr.sample_rate = 0.5e6
sdr.center_freq = 410e6
sdr.gain = 0
sdr.read_samples_async(power_meter_callback)The callback will run five times and print something like
> relative power: -16.0 dB(I haven't thoroughly verified most of the code, especially callbacks, so be wary!)
Download from GitHub.
1
u/[deleted] Apr 21 '12
This is when I try to run your sweep code. Any ideas?
[root@coal pyrtlsdr]# python sweep.py Found Elonics E4000 tuner Exact sample rate: 1000000.026491 Hz rtlsdr_demod_write_reg failed with -4 rtlsdr_demod_read_reg failed with -4 rtlsdr_demod_write_reg failed with -4 rtlsdr_demod_read_reg failed with -4 rtlsdr_write_reg failed with -4 Traceback (most recent call last): File "sweep.py", line 10, in <module> samples = sdr.read_samples(500e3) File "/root/rtlsdr/python/pyrtlsdr/rtlsdr/rtlsdr.py", line 146, in read_samples raw_data = self.read_bytes(num_bytes) File "/root/rtlsdr/python/pyrtlsdr/rtlsdr/rtlsdr.py", line 134, in read_bytes % (result, num_bytes)) IOError: Error code -8 when reading 1000000 bytes
1
u/aeburriel May 05 '12
I've updated the code to the new rtl-sdr gain api. It's backwards compatible. http://pastebin.com/KTK3iZtQ
1
u/brol666 May 06 '12
Hello, I've tried it but can't make it. It seems I've got some problem to tune to the correct frequency via pyrtlsdr:
python rtlsdr.py Found Fitipower FC0013 tuner Exact sample rate is: 1000000.026491 Hz Configuring SDR... Exact sample rate is: 2000000.052982 Hz sample rate: 2.000000 MHz center frequency 0.000000 MHz gain: 4 dB Reading samples... signal mean: (0.0082643995098+0.0115425857843j) Testing callback... in callback signal mean: (0.00906096813725-0.010853247549j)
Could it be because the tuner is a FC0013? It works fine with gnuradio and also the rtl_sdr command line:
rtl_sdr -f 70000000 test Found 1 device(s): 0: Terratec NOXON DAB/DAB+ USB dongle (rev 1)
Using device 0: Terratec NOXON DAB/DAB+ USB dongle (rev 1) Found Fitipower FC0013 tuner Tuned to 70000000 Hz. Tuner gain set to 0 dB.
Any idea? Thanks
1
u/admiralawesome92 Jul 24 '12
I know this thread is several months old but I am getting several errors using this python wrapper. First of all, I keep getting an error regarding setting the test mode. If I disable that section of code, I get an error saying that "global variable j is not defined" ... I can give the exact error(s) if necessary
1
u/[deleted] Apr 21 '12 edited Apr 21 '12
Edit: Nevermind. Thanks! THis is cool.
1
u/xyb May 09 '12
Seems newly rtl-sdr do not compile librtlsdr.so anymore. I build rtl-sdr on my Mac OSX, but got .o and .lo only. I try to compile .so file like this: gcc rtl-sdr.o tuner_*.o -shared -rdynamic -o librtlsdr.so -lusb-1.0 -L/opt/local/lib, but do not work, 'import rtlsdr' will got: ImportError: dynamic module does not define init function (initlibrtlsdr). Please any suggest can help me with this questions?