Search code examples
pythonarraysnumpywavspectrogram

too many indicies for an array


I've used the code from user Anil_M in this question i want to get the same result. but in My case it plots the first image enter image description here

and after that i get an error

Traceback (most recent call last):
  File "c:/Users/Maram/Desktop/Spectrogram/first_attempt.py", line 73, in <module>
    fftArray = fftArray[0:numUniquePoints]
TypeError: slice indices must be integers or None or have an __index__ method 

and i dont get this enter image description here

or when i try to use a slightly larger wav file, it doesnt even display the first image. I just get this error:

Traceback (most recent call last):
      File "c:/Users/Maram/Desktop/Spectrogram/first_attempt.py", line 38, in <module>
        mySoundOneChannel = mySound[:,0]
    IndexError: too many indices for array

I think these errors are somehow related but i just don't understand how or why it's caused.

could someone help me get it to run say a larger wav file/longer track, or help me get my existing code to run the power against frequency graph? any help would be really appreciated.

(I dont know if this is considered a duplicate - in the original question no one has the same problems im facing.)


Solution

  • The statement mySoundOneChannel = mySound[:,0] reads the first channel of the file. If your file contains only 1 channel change it to mySoundOneChannel = mySound[0:]

    As for the second issue - numUniquePoints is of type float, so you must cast it - numUniquePoints = int(numpy.ceil((mySoundLength + 1) / 2.0)).