0

I am trying to perform some analysis on a .wav file, I have taken the code from the following question (Python Scipy FFT wav files) and it seems to give exactly what I need however when running the code I run into the following error:

TypeError: slice indices must be integers or None or have an index method

This occurs on line 9 of my code. I don't undertand why this occurs, because I thought that the abs function would make it an integer.

    import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.io import wavfile # get the api
fs, data = wavfile.read('New Recording 2.wav') # load the data
a = data.T[0] # this is a two channel soundtrack, I get the first track
b=[(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
c = fft(b) # calculate fourier transform (complex numbers list)
d = len(c)/2  # you only need half of the fft list (real signal symmetry)
plt.plot(abs(c[:(d-1)]),'r') 
plt.show()
plt.savefig("Test.png", bbox_inches = "tight")
  • You should've added ```abs()``` inside the square brackets then, like ```c[:abs(d-1)]``` – Abhinav Mathur Oct 15 '20 at 09:56
  • @AbhinavMathur I still run into the same error if I change the position, I'm not sure why as it appears to work on stack question I linked and the code is exactly the same. – BeigeSponge Oct 15 '20 at 10:18

1 Answers1

0

abs() doesn't make your number into an integer. It just turns negative numbers into positive numbers. When len(c) is an odd number your variable d is a float that ends in x.5.

What you want is probably round(d) instead of abs(d)

blues
  • 4,547
  • 3
  • 23
  • 39