简体   繁体   中英

WAV FFT: Slice indices must be integers

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")

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM