我对python和rtlsdr还是个新手,但是我有一个项目,我正在和一个树莓派合作,当听到无线电信号时,它会触发警告灯。
在更高的层次上,我的项目是使用2米无线电频段的外部天线,连接到插入Raspberry Pi的RTL-SDR加密狗。我有一个连接到GPIO引脚的标准继电器,当它“听到”信号时,它会打开灯。
我真的不在乎99%的输入信号。我只想知道什么时候有载波在147.3 the,让GPIO把灯打开。就这样。没有更多也没有更少。当然,这可以简单地完成吗?我已经阅读了scipy.signal.butter带通滤波器,但我不能让它在这个频率下工作。
我的代码如下,我欢迎任何改进的建议,但理想情况下,我正在寻找一种方法,让程序识别中心频率为147.3 My的峰值,然后调用我的'warning_lights.py‘脚本。
import RPi.GPIO as GPIO
from rtlsdr import *
from scipy import signal
import peakdetect
import datetime
import sys
import subprocess
def restart():
import subprocess
import time
time.sleep(120)
command = "/usr/bin/sudo /sbin/shutdown -r now"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print (output)
# configure SDR device and settings
sdr = RtlSdr()
sdr.sample_rate = 2.4e6 # Hz
sdr.center_freq = 147.3e6 # Hz
sdr.gain = 'auto' # Possible values are 0.0 0.9 1.4 2.7 3.7 7.7 8.7 12.5 14.4 15.7 16.6 19.7 20.7 22.9 25.4 28.0 29.7 32.8 33.8 36.4 37.2 38.6 40.2 42.1 43.4 43.9 44.5 48.0 49.6
num_samples = 1024*1024
procs = []
while True:
try:
samples = sdr.read_samples(num_samples)
power, psd_freq = psd(samples, NFFT=1024, Fs=sdr.sample_rate/1e6,
Fc=sdr.center_freq/1e6)
power_db = 10*np.log10(power)
maxima, minima = peakdetect.peakdetect(power_db, psd_freq, delta=1)
for mx in maxima:
if mx[0] == 147.3: #checking that peak was on 147.3 MHz
if mx[1] > (-15): #checking dBm of signal
try:
while proc.poll() is None:
proc.terminate()
except:
print("Warning lights not on. Turning them on.")
finally:
print(mx[1])
proc = subprocess.Popen([sys.executable, '/home/pi/scripts/warning_lights.py'])
procs.append(proc)
except:
restart()发布于 2021-07-25 18:54:20
当你调谐到147.3 the时,你从SDR得到的并不是实际的无线电信号,因为它出现在输入端。SDR将RF信号的频率向下移动到更低的频率,以便更容易处理和数字化。因此,原始信号中以147.3 0Hz为中心的部分被下移到0 0Hz,进入所谓的“基带”。超出一定带宽(在您的情况下为2.4 away )的所有内容都会被过滤掉。例如,如果有一个148.3 1MHz的纯正弦信号,它将在SDR的输出中显示为1 1MHz正弦信号。想出一个频谱图来可视化这一点是非常有帮助的:Wikipedia article on baseband。
因此,如果有一个以147.3 0Hz为中心的无线电信号,你在SDR的输出中看不到147.3 0Hz,而是0 0Hz(由于SDR电路的频率不是非常准确,所以会有轻微的偏移)。正如您所提到的,使用scipy进行过滤应该很容易。
还有另一个可能成为问题的陷阱。大多数SDR在它们调谐到的频率上产生一个小的频率峰值(称为"DC尖峰“)。在我的RTL-SDR中,我发现当增益较低时就会出现这种情况。解决这个问题的方法是调到你感兴趣的信号带宽之外的频率(147.5 The就可以了),然后在处理过程中补偿这种偏移。
因此,应该起作用的是:
# configure SDR device and settings
sdr = RtlSdr()
offset_freq = 200e3 # Hz
sdr.sample_rate = 2.4e6 # Hz
sdr.center_freq = 147.3e6 - offset_freq # Hz
sdr.gain = 'auto' # Possible values are 0.0 0.9 1.4 2.7 3.7 7.7 8.7 12.5 14.4 15.7 16.6 19.7 20.7 22.9 25.4 28.0 29.7 32.8 33.8 36.4 37.2 38.6 40.2 42.1 43.4 43.9 44.5 48.0 49.6
num_samples = 1024*1024
procs = []
while True:
try:
samples = sdr.read_samples(num_samples)
power, psd_freq = psd(samples, NFFT=1024, Fs=sdr.sample_rate/1e6,
Fc=sdr.center_freq/1e6)
power_db = 10*np.log10(power)
maxima, minima = peakdetect.peakdetect(power_db, psd_freq, delta=1)
for mx in maxima:
if mx[0] == offset_freq: #checking that peak was on 147.3 MHz
if mx[1] > (-15): #checking dBm of signal
try:
while proc.poll() is None:
proc.terminate()
except:
print("Warning lights not on. Turning them on.")
finally:
print(mx[1])
proc = subprocess.Popen([sys.executable,
'/home/pi/scripts/warning_lights.py'])
procs.append(proc)
except:
restart()这在图像中没有说明,但您也可以从载波以下的任何频率获得信号中的负频率。
https://stackoverflow.com/questions/66540414
复制相似问题