How to plot real time graph using python for the signals received from RTL SDR without closing the plot?

247 Views Asked by At

I want to plot graph of signal from RTL-SDR dongle but couldn't plot continuously at the same window without closing

Here's the code that i tried,

from pylab import *
from rtlsdr import *
from time import sleep    
sdr = RtlSdr()    
sdr.sample_rate = 2.4e6
p = sdr.start_freq = 95e6
sdr.gain = 50    

try:
    while True: # run until interrupted
        samples = sdr.read_samples(256*1024)
        psd(samples.real, NFFT=1024, Fs=sdr.sample_rate/1e6, Fc=p/1e6)
        xlabel('Frequency (MHz)')
        ylabel('Relative power (dB)')
        show()
        sleep(0.01) # sleep for 1s
except:
    pass

sdr.close()

can anyone help

1

There are 1 best solutions below

0
On

You can use animation, here is an example.

fig = plt.figure()
graph_out = fig.add_subplot(1, 1, 1)


def animate(i):
    graph_out.clear()
    #samples = sdr.read_samples(256*1024)
    samples = sdr.read_samples(128*1024)
    # use matplotlib to estimate and plot the PSD
    graph_out.psd(samples, NFFT=1024, Fs=sdr.sample_rate /
                  1e6, Fc=sdr.center_freq/1e6)
    #graph_out.xlabel('Frequency (MHz)')
    #graph_out.ylabel('Relative power (dB)')


try:
    ani = animation.FuncAnimation(fig, animate, interval=10)
    plt.show()
except KeyboardInterrupt:
    pass
finally:
    sdr.close()