2

I'm using the matplotlib function psd to generate the power spectral density of a bunch of radio signals I'm receiving. All I want are the returned values but the function plots the whole spectrum not matter what. Is there a way to prevent it from plotting? Is there another function that could do this without the plot? I'm trying to run this as rapidly as possible so anything to speed it up (aka preventing the plot entirely) would be very useful.

The code is pretty straightforward but I'm not sure how to suppress this plotting and ideally prevent it from doing it entirely because I want this code to run as fast as possible:

from pylab import *
power, psd_frequencies = psd(radio_samples, NFFT=256, Fs=samples_rate, Fc=center_frequency)

Alternatives to running psd() that would be faster are very welcome too.

1
  • Just as a tip, I strongly recommend never using pylab in real code. Pylab is just a namespace clutter for matplotlib's pyplot and numpy smooshed together, see this answer. If what you want is simply a power spectral density, there are plenty of options in scipy or you can make your own with an fft. Commented Jul 5, 2018 at 15:25

1 Answer 1

3

To reproduce exactly what matplotlib plots in the psd plot, you may use its own method:

from matplotlib.mlab import psd
power, psd_frequencies = psd(radio_samples, NFFT=256, Fs=samples_rate)
psd_frequencies += center_frequency

This gives you the data, but without the plot.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay that answers it pretty well. I see some discussion of this elsewhere but is there any reason to use scipy.signal.welch instead of this? Looks like they do pretty much the same thing?
I did not look into the exact details, but the point is that scipy is not a dependency of matplotlib. So matplotlib needs its own implementation of Welch's algorithm instead of relying on the one from scipy. From a user's perspective the use of scipy may be totally fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.