软件定义无线电 SDR 实践 258 字

Python

本例使用 Python 包 pyrtlsdr,可在其官方网站获取。我在 Windows 10 上使用的是该包的 0.2.92 版本和 Python 3.8。

采集:2026-11-18T02:49:28+08:00 · 查看英文原文 ↗

原始来源:https://www.sharetechnote.com/html/SDR_RTL2832_Python.html

正文

Python

本例使用 Python 包 pyrtlsdr,可在其官方网站获取。我在 Windows 10 上使用的是该包的 0.2.92 版本和 Python 3.8。

注意 1:要运行这个脚本,你需要先安装 RTL-SDR 驱动并确认其可正常工作。关于如何安装和测试驱动,请参见本节。

from pylab import * from rtlsdr import * sdr = RtlSdr() # configure device sdr.sample_rate = 2.4e6 sdr.center_freq = 105.5e6 sdr.gain = 4 samples = sdr.read_samples(256*1024) sdr.close() # use matplotlib to estimate and plot the PSD psd(samples, NFFT=1024, Fs=sdr.sample_rate/1e6, Fc=sdr.center_freq/1e6) xlabel(‘Frequency (MHz)’) ylabel(‘Relative power (dB)’) show()

from pylab import *

from rtlsdr import *

sdr = RtlSdr()

configure device

sdr.sample_rate = 2.4e6

sdr.center_freq = 105.5e6

sdr.gain = 4

samples = sdr.read_samples(256*1024)

sdr.close()

use matplotlib to estimate and plot the PSD

psd(samples, NFFT=1024, Fs=sdr.sample_rate/1e6, Fc=sdr.center_freq/1e6)

xlabel(‘Frequency (MHz)’)

ylabel(‘Relative power (dB)’)

show()

(原页面未提供图示说明)

图注:(原页面未提供图示说明)

注意 2:这个 Python 包需要一些称为 librtlsdr 的依赖项。我使用的是 librtlsdr v0.8,可从此处获取。我按下面高亮的方式使用了该包。

(原页面未提供图示说明)

图注:(原页面未提供图示说明)

(原页面未提供图示说明)

图注:(原页面未提供图示说明)

C:\Users\jaeku\AppData\Local\Programs\Python\Python38\Lib\site-packages\rtlsdr

(原页面未提供图示说明)

图注:(原页面未提供图示说明)

按如下方式修改 librtlsdr.py:

import sys import os import ctypes # add this line from ctypes import * from ctypes.util import find_library def load_librtlsdr(): if sys.platform == “linux” and ‘LD_LIBRARY_PATH’ in os.environ.keys(): ld_library_paths = [local_path for local_path in os.environ[‘LD_LIBRARY_PATH’].split(’:’) if local_path.strip()] driver_files = [local_path + ‘/librtlsdr.so’ for local_path in ld_library_paths] else: driver_files = [] driver_files += [‘librtlsdr.so’, ‘rtlsdr/librtlsdr.so’] driver_files += [‘rtlsdr.dll’, ‘librtlsdr.so’] driver_files += [’..//rtlsdr.dll’, ’..//librtlsdr.so’] driver_files += [‘rtlsdr//rtlsdr.dll’, ‘rtlsdr//librtlsdr.so’] driver_files += [lambda : find_library(‘rtlsdr’), lambda : find_library(‘librtlsdr’)] dll = None # Comment out following part # for driver in driver_files: # if callable(driver): # driver = driver() # try: # dll = CDLL(driver) # break # except: # pass # else: # raise ImportError(‘Error loading librtlsdr. Make sure librtlsdr ’\ # ‘(and all of its dependencies) are in your path’) dll = ctypes.WinDLL (“c:\rtlsdrlib\librtlsdr.dll”) # add this line return dll

import sys

import os

import ctypes # add this line

from ctypes import *

from ctypes.util import find_library

def load_librtlsdr():

if sys.platform == “linux” and ‘LD_LIBRARY_PATH’ in os.environ.keys():

ld_library_paths = [local_path for local_path in os.environ[‘LD_LIBRARY_PATH’].split(’:’) if local_path.strip()]

driver_files = [local_path + ‘/librtlsdr.so’ for local_path in ld_library_paths]

else:

driver_files = []

driver_files += [‘librtlsdr.so’, ‘rtlsdr/librtlsdr.so’]

driver_files += [‘rtlsdr.dll’, ‘librtlsdr.so’]

driver_files += [’..//rtlsdr.dll’, ’..//librtlsdr.so’]

driver_files += [‘rtlsdr//rtlsdr.dll’, ‘rtlsdr//librtlsdr.so’]

driver_files += [lambda : find_library(‘rtlsdr’), lambda : find_library(‘librtlsdr’)]

dll = None

Comment out following part

for driver in driver_files:

if callable(driver):

driver = driver()

try:

dll = CDLL(driver)

break

except:

pass

else:

raise ImportError(‘Error loading librtlsdr. Make sure librtlsdr ’\

‘(and all of its dependencies) are in your path’)

dll = ctypes.WinDLL (“c:\rtlsdrlib\librtlsdr.dll”) # add this line

return dll

参考资料:

使用 GNU Radio Companion 第 1 部分

GNU Radio Companion,第 2 部分

eapbg #59 GNU Radio Companion 入门:用 SDR 读取钥匙扣

编写 GNU Radio 模块

图示资源


同族文档