pythonplotmatplotlibpsd

trimming matplotlib power spectrum chart


My first stackoverflow question, hope I do it right..

I'm trying to build a Power Spectrum Density chart (matplotlib.pyplot.psd) and I need to discard 256 bins at both ends of the spectrum before plotting. For my task I need to actually delete data points and not simply limit the x axis range.

The psd function returns a tuple of two arrays, and I thought I could simply remove the elements of the array and then call show(). but it doesn't seem to work.

from matplotlib.pyplot import *
import numpy as np

#put some dummy data into s
s=np.vectorize(complex)(range(2048),range(2048))

x=psd(s, 2048, 194171, window=np.bartlett(2048),Fc=14050000)

#trim x
del x[0][-256:]
del x[0][:256]
del x[1][-256:]
del x[1][:256]

show()

this gives:

Traceback (most recent call last):
  File "dummy-fft.py", line 10, in <module>
    del x[0][-256:]
ValueError: cannot delete array elements

Is there a better way to truncate a psd plot?


Solution

  • Instead of delete the head and tail, you can use slice to get the part that you want:

    from matplotlib.pyplot import *
    import numpy as np
    
    #put some dummy data into s
    s=np.vectorize(complex)(range(2048),range(2048))
    
    power, freq=psd(s, 2048, 194171, window=np.bartlett(2048),Fc=14050000)
    figure()
    semilogy(freq[256:-256], power[256:-256])
    show()