Just a quick update here.
I've been messing around with the RTL-SDR Python Library to get some readings from the SDR dongle.
In the end, I managed to make the first version of code that takes two frequencies as inputs, and after some time shows me the spectrum results gathered between those two frequencies.
The code has been developed to remove the typical "central frequency spike" caused by the 0MHz reading and to be as flat as possible between multiple readings, which is necessary because one reading only has 1MHz of bandwidth.
from rtlsdr import RtlSdr
from matplotlib import mlab as mlab
import matplotlib.pyplot as plt
import os
try:
sdr = RtlSdr()
except:
print("Error: cannot initialize the SDR device")
exit(0)
os.system("clear")
print("Insert initial frequency [MHz]: ", end="")
CENTER_FREQ = int(input())*1e6
print("Insert final frequency [MHz]: ", end="")
END_FREQ = int(input())*1e6
GAIN = 4
SAMPLE_RATE = 2.4e6
F_CORRECTION = 60
DATA_RES = 128
pow_arr = []
freq_arr = []
# configure device
sdr.sample_rate = SAMPLE_RATE
sdr.center_freq = CENTER_FREQ
sdr.freq_correction = F_CORRECTION
sdr.gain = GAIN
garbage = sdr.read_samples(DATA_RES * 1024)
step = 2.5e5
half_step = step/2
center_f_arr = []
n = int((END_FREQ - CENTER_FREQ)/step)
#init loading bar
bar = "["
for i in range(1,n+1):
bar = bar + " "
bar = bar + "]"
#main loop
for k in range(0,n):
sdr.center_freq = CENTER_FREQ + (step*k)
center_f_arr.append((CENTER_FREQ + (step*k))/1e6) #save the central frequency for later
samples = sdr.read_samples(DATA_RES * 1024)
power, psd_freq = mlab.psd(samples, NFFT=1024, Fs=sdr.sample_rate/1e6)
psd_freq = psd_freq + sdr.center_freq/1e6
for i in range(0, len(psd_freq)):
if psd_freq[i] < (sdr.center_freq+half_step)/1e6 and psd_freq[i] > (sdr.center_freq-half_step)/1e6:
freq_arr.append(psd_freq[i])
pow_arr.append(power[i])
os.system("clear")
string_list = list(bar)
string_list[k+1] = "="
bar = "".join(string_list)
print(bar)
print("Frequency: ", end="")
print(psd_freq[i])
if(len(freq_arr) > 536870000):
print("Fatal error: trying to store to much data")
exit(0)
i = 0
for e in freq_arr:
if e in center_f_arr:
pow_arr[i] = pow_arr[i-1]
pow_arr[i+1] = pow_arr[i+2]
pow_arr[i-1] = pow_arr[i-2]
i=i+1
print("\n")
print(len(freq_arr))
print("\n")
plt.plot(freq_arr, pow_arr)
plt.plot(END_FREQ/1e6, 0.005) #noise visual filter
plt.show()
It's just a very alpha version of the code and needs a bit more work, but for now and for some early testing, it's enough.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.