1

Currently, I'm writing a python script, which should do the following:

  • read an audio file respectively a wav file via scipy.io.wavfile.read().
  • calculate the spectrogram of given wav file.
  • write the data from spectrogram back into a wav file.

When I try to run the script, I get the following error:

Traceback (most recent call last):
  File "demo2.py", line 61, in <module>
    F,T,S = scipy.signal.spectrogram(s_mono,rate,window,None,nOverlap,nFFT)
AttributeError: 'module' object has no attribute 'spectrogram'

Which is rather strange, because in my opinion my code should work fine. I've already checked for Syntax Errors and I also looked up in the official scipy documentation but I couldn't find any clue what could possible be wrong with it.

Here's a little snippet of my code:

import scipy
from scipy import signal
import scipy.io.wavfile as wav
#---------------------------------

# here's the rest of my code    

F,T,S = scipy.signal.spectrogram(s_mono,rate,window,None,nOverlap,nFFT)

Additional Information: I'm working on a MacBook with MacOS 10.9.5

Nico
  • 53
  • 1
  • 7

3 Answers3

1

Since there's no signal.py, uninstall scipy and reinstall it without using pip. Get it from their website. Getting it with pip seems to almost always have problems.

apoorlydrawnape
  • 288
  • 3
  • 12
1

Please see if this works for you:

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

t = np.arange(10000)
sig = np.sin(2. * math.pi * 1 / 1000. * t)
f, t, Sxx = signal.spectrogram(sig, 1.)
plt.pcolormesh(t, f, Sxx)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

It works for me with python 2.7 and scipy 0.19.

If this works for you, then you are probably causing some weird namespace errors in your script (calling a variable signal, etc.).

sobek
  • 1,386
  • 10
  • 28
  • The moment I try to call signal.spectrogram, I get an attribute error. – Nico Apr 09 '17 at 21:10
  • Have you tried the following: `from scipy.signal import spectral as spcl; sss = spcl.spectrogram(x, fs) ` It works for periodogram, welch and lombscargle but not for csd, coherence & spectrogram. Spectrogram function resides in spectral.py. Any expert advise? – Bussller May 12 '17 at 04:55
  • Thanks - that test worked correctly. Python 3.9 Mac BigSur. – Tim Seed Feb 06 '21 at 03:39
0

I also had an issue with this ... caused by my bad variable assignments.

This is how I read and process a wav file. Please note the wave file needs to have metadata stripped (I used ffmpeg to do this)

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

import soundfile as sf
from matplotlib import pyplot as plt
datasignal, fs_rate= sf.read('40m_stripped.wav')
print(f"Data shape is {datasignal.shape}")
sig=datasignal[::,0]
print(f"Sig shape is {sig.shape}")
f, t, Sxx = signal.spectrogram(sig, fs_rate)
plt.pcolormesh(t, f, Sxx)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()
Tim Seed
  • 5,119
  • 2
  • 30
  • 26