Home > Net >  Plotting modified spectrogram in Python
Plotting modified spectrogram in Python

Time:01-10

I have successfully plotted a spectrogram using:

y, sr=librosa.load(file)
x=librosa.stft(y, n_fft=147)
fig = plt.figure(figsize=(12,9))
plt.pcolormesh(np.abs(x))

Now I want to modified elements of the spectrogram, so I tried:

for i in range(8):
        for j in range (len(x-1)):
            arr[i][j]=x[i][j]
#Nothing modified yet

But when I try to plot that with

plt.pcolormesh(np.abs(arr))

I get the message: “bad operand type for abs(): 'list'”

Any ideas?

I have tried applying abs() directly to:

arr[i][j]=abs(x[i][j])

and

arr[i][j]=np.abs(x[i][j])

But nothing works. What I'm expecting is to be able to modify the spectrogram.

CodePudding user response:

Full error trace:

wind.py:48: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  plt.pcolormesh(np.abs(arr))
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\WIN 10 PRO\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "wind.py", line 48, in playF
    plt.pcolormesh(np.abs(arr))
TypeError: bad operand type for abs(): 'list'

CodePudding user response:

I have solved it by using arr = np.zeros(shape=(8, len(x[0])-1)) instead of lists or np.array.

  • Related