0

this is the code i tried in python 3.6.1 but it gets me an error !!

import numpy as np
data = np.genfromtxt('filename', dtype=None , delimiter=",")
NFFT = 200     # the length of the windowing segments
Fs = 500  # the sampling rate
Pxx, freqs, bins, im = plt.specgram(data, NFFT=NFFT,   Fs=Fs,noverlap=100, cmap=plt.cm.gist_heat)
plt.show()

there is another line of code instead of using plt.specgram but it gets also an error:

f, t, Sxx = signal.spectrogram(data, Fs)

And this is the text file signal data:

33,4.9106E+13,-0.6946377,12.680544,0.50395286;
33,4.91061E+13,5.012288,11.264028,0.95342433;
33,4.91061E+13,4.903325,10.882658,-0.08172209;
33,4.91062E+13,-0.61291564,18.496431,3.0237172;

And this is the error..

Warning (from warnings module):
  File "C:\Python36\lib\site-packages\matplotlib\mlab.py", line 1281
    "(=%d) >= signal length (=%d)." % (NFFT, len(x)))
UserWarning: Only one segment is calculated since parameter NFFT (=200) >= signal length (=4).
Traceback (most recent call last):
  File "C:/Users/hp/Desktop/hispectrogram.py", line 96, in <module>
    Pxx, freqs, bins, im = plt.specgram(data, NFFT=NFFT,   Fs=Fs,noverlap=100, cmap=plt.cm.gist_heat)
  File "C:\Python36\lib\site-packages\matplotlib\pyplot.py", line 3427, in specgram
    vmin=vmin, vmax=vmax, data=data, **kwargs)
  File "C:\Python36\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "C:\Python36\lib\site-packages\matplotlib\axes\_axes.py", line 7215, in specgram
    mode=mode)
  File "C:\Python36\lib\site-packages\matplotlib\mlab.py", line 1288, in specgram
    mode=mode)
  File "C:\Python36\lib\site-packages\matplotlib\mlab.py", line 716, in _spectral_helper
    return_window=True)
  File "C:\Python36\lib\site-packages\matplotlib\mlab.py", line 265, in apply_window
    windowVals = window(np.ones(xshapetarg, dtype=x.dtype))
  File "C:\Python36\lib\site-packages\matplotlib\mlab.py", line 214, in window_hanning
    return np.hanning(len(x))*x
TypeError: invalid type promotion
Hadeer El-Zayat
  • 281
  • 5
  • 20
  • There are several problems with your code. If your data really looks like it does in the snippet you've posted, then the values are not comma seperated. But you ask `genfromtxt` to interpret the file as comma seperated. Next one is that you try to do a 200 points FFT with 100 points hop size over a 4 point signal. What do you expect to be the result of such a calculation? The actual error occures because your variable `data` is a record array that holds numerical vals and strings. You cannot multiply such an array with pure a numerial one like `hanning(len(x))`. Try to load only numericals. – MaxPowers Nov 04 '17 at 11:50
  • please forgive me about those errors but iam new in this field and i need some help.. i edited the data in the text file, what else should i do ? ,, thnx i advance. – Hadeer El-Zayat Nov 04 '17 at 17:31

0 Answers0