psd

Hi I have some data for a 24hr period with a sample rate of 100 samples/second. I want to create a power spectrum using matplotlibs function psd. I want it to have 10 minute windows with a 50% overlap, but cant seem to get the syntax right. My code is as follows:

NFFT = len(data)
Fs = 100
window=np.hanning(Fs6010)
noverlap = window*0.5
plt.psd(data, NFFT, Fs, window, noverlap )

anyone kow how to do this properly???

Thanks,
D
``

Be careful to use a suitable value for NFFT. It must be linked to your
windows size, not the total data length, and you would rather use a
power of 2 for efficience. Do not use it to increase the frequency
resolution (use pad_to instead).

Fs = 100
NFFT = Fs*60*10
Pxx, f = plt.psd(data, NFFT, Fs, window=np.hanning(NFFT), NFFT/2)

···

Le lundi 30 janvier 2012 à 11:45 +0000, David Craig a écrit :

Hi I have some data for a 24hr period with a sample rate of 100
samples/second. I want to create a power spectrum using matplotlibs
function psd. I want it to have 10 minute windows with a 50% overlap,
but cant seem to get the syntax right. My code is as follows:

NFFT = len(data)
Fs = 100
window=np.hanning(Fs*60*10)
noverlap = window*0.5
plt.psd(data, NFFT, Fs, window, noverlap )

anyone kow how to do this properly???

--
Fabrice Silva

Hi, thanks for that. I’ve made the following changes:

NFFT = 1006010 # Linked to window size
Fs = stream[0].stats.sampling_rate
win = np.hanning(NFFT)
overlap = NFFT/2
power, freq = plt.psd(data, NFFT, Fs, win, overlap)

but it returns the following error:

Traceback (most recent call last):
File “/home/davcra/Desktop/python_scripts/welchPSD.py”, line 20, in
power, freq = plt.psd(data, NFFT, Fs, win, overlap)
File “/usr/lib/python2.7/site-packages/matplotlib/pyplot.py”, line 2322, in psd
ret = ax.psd(x, NFFT, Fs, Fc, detrend, window, noverlap, pad_to, sides, scale_by_freq, **kwargs)
File “/usr/lib/python2.7/site-packages/matplotlib/axes.py”, line 7876, in psd
sides, scale_by_freq)
File “/usr/lib/python2.7/site-packages/matplotlib/mlab.py”, line 389, in psd
scale_by_freq)
File “/usr/lib/python2.7/site-packages/matplotlib/mlab.py”, line 419, in csd
noverlap, pad_to, sides, scale_by_freq)
File “/usr/lib/python2.7/site-packages/matplotlib/mlab.py”, line 268, in _spectral_helper
thisX = windowVals * detrend(thisX)
TypeError: ‘int’ object is not callable

···

On Mon, Jan 30, 2012 at 12:13 PM, Fabrice Silva <silva@…83…1918…> wrote:

Le lundi 30 janvier 2012 à 11:45 +0000, David Craig a écrit :

Hi I have some data for a 24hr period with a sample rate of 100

samples/second. I want to create a power spectrum using matplotlibs

function psd. I want it to have 10 minute windows with a 50% overlap,

but cant seem to get the syntax right. My code is as follows:

NFFT = len(data)

Fs = 100

window=np.hanning(Fs6010)

noverlap = window*0.5

plt.psd(data, NFFT, Fs, window, noverlap )

anyone kow how to do this properly???

Be careful to use a suitable value for NFFT. It must be linked to your

windows size, not the total data length, and you would rather use a

power of 2 for efficience. Do not use it to increase the frequency

resolution (use pad_to instead).

Fs = 100

NFFT = Fs6010

Pxx, f = plt.psd(data, NFFT, Fs, window=np.hanning(NFFT), NFFT/2)

Fabrice Silva


Try before you buy = See our experts in action!

The most comprehensive online learning library for Microsoft developers

is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,

Metro Style Apps, more. Free future releases when you subscribe now!

http://p.sf.net/sfu/learndevnow-dev2


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Please be careful with argument order. Expected arguments for psd are
- the data array
- the NFFT value
- the sampling frequency
- the detrend callable (*missing in your example*)
- and then the overlap number

You should either specify on of the detrend pylab functions
(detrend_none, detrend_mean, and detrend_linear) or use keyword
arguments as I did in the example.

···

Le lundi 30 janvier 2012 à 13:34 +0000, David Craig a écrit :

Hi, thanks for that. I've made the following changes:

NFFT = 100*60*10 # Linked to window size
Fs = stream[0].stats.sampling_rate
win = np.hanning(NFFT)
overlap = NFFT/2
power, freq = plt.psd(data, NFFT, Fs, win, overlap)

but it returns the following error:

Traceback (most recent call last):
  File "/home/davcra/Desktop/python_scripts/welchPSD.py", line 20, in <module>
    power, freq = plt.psd(data, NFFT, Fs, win, overlap)
  File "/usr/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2322, in psd
    ret = ax.psd(x, NFFT, Fs, Fc, detrend, window, noverlap, pad_to, sides, scale_by_freq, **kwargs)
  File "/usr/lib/python2.7/site-packages/matplotlib/axes.py", line 7876, in psd
    sides, scale_by_freq)
  File "/usr/lib/python2.7/site-packages/matplotlib/mlab.py", line 389, in psd
    scale_by_freq)
  File "/usr/lib/python2.7/site-packages/matplotlib/mlab.py", line 419, in csd
    noverlap, pad_to, sides, scale_by_freq)
  File "/usr/lib/python2.7/site-packages/matplotlib/mlab.py", line 268, in _spectral_helper
    thisX = windowVals * detrend(thisX)
TypeError: 'int' object is not callable

--
Fabrice Silva

David Craig :

Hi, thanks for that. I've made the following changes:

NFFT = 100*60*10 # Linked to window size
Fs = stream[0].stats.sampling_rate
win = np.hanning(NFFT)
overlap = NFFT/2
power, freq = plt.psd(data, NFFT, Fs, win, overlap)

but it returns the following error:

Traceback (most recent call last):
  File "/home/davcra/Desktop/python_scripts/welchPSD.py", line 20, in <module>
    power, freq = plt.psd(data, NFFT, Fs, win, overlap)
  File "/usr/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2322, in psd
    ret = ax.psd(x, NFFT, Fs, Fc, detrend, window, noverlap, pad_to, sides, scale_by_freq, **kwargs)
  File "/usr/lib/python2.7/site-packages/matplotlib/axes.py", line 7876, in psd
    sides, scale_by_freq)
  File "/usr/lib/python2.7/site-packages/matplotlib/mlab.py", line 389, in psd
    scale_by_freq)
  File "/usr/lib/python2.7/site-packages/matplotlib/mlab.py", line 419, in csd
    noverlap, pad_to, sides, scale_by_freq)
  File "/usr/lib/python2.7/site-packages/matplotlib/mlab.py", line 268, in _spectral_helper
    thisX = windowVals * detrend(thisX)
TypeError: 'int' object is not callable

Please, look the syntax of .psd in
http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=psd#matplotlib.pyplot.psd

You will find, for example, that the 5th argument, detrend, should be callable. In your code it is just a number.
Use explicit keywords, it may save you from misunderstanding yourself...

Jerzy Karczmarczuk

OH.
Fabrice Silva found it as well. OK, two is better than one...