1

I am trying to calculate periodogram using my code

from scipy import signal
import numpy as np
import matplotlib.pyplot as plt

x = [line.rstrip('\n') for line in open('27000.dat')]
x = np.array(x)
fs=64

f, Pxx_den = signal.periodogram(x, fs)
plt.semilogy(f, Pxx_den)
plt.xlabel('frequency [Hz]')
plt.ylabel('PSD [V**2/Hz]')
plt.show()

But I got

  File "m3.py", line 9, in <module>
    f, Pxx_den = signal.periodogram(x, fs)
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 141, in periodogram
    scaling, axis)
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 273, in welch
    return_onesided, scaling, axis)
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 391, in csd
    mode='psd')
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 824, in _spectral_helper
    scale = 1.0 / (fs * (win*win).sum())
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S64') dtype('S64') dtype('S64')

Why? I am on Ubuntu 16.04,Python 2.7.11.

Richard Rublev
  • 7,718
  • 16
  • 77
  • 121
  • Possible duplicate of [TypeError: ufunc 'add' did not contain a loop with signature matching types](http://stackoverflow.com/questions/35013726/typeerror-ufunc-add-did-not-contain-a-loop-with-signature-matching-types) – Kyle Heuton Jan 11 '17 at 00:39

1 Answers1

2

As in this question, the obscure error code comes from running numpy functions on string data rather than floating-point data. The call to rstrip returns a string. I don't know what your data file looks like, but adding x = x.astype(np.float) after x = np.array(x) should fix your problem.

Community
  • 1
  • 1
Kyle Heuton
  • 9,318
  • 4
  • 40
  • 52