WAV FFT:切片索引必须是整数

WAV FFT: Slice indices must be integers

我正在尝试对 .wav 文件进行一些分析,我从以下问题 (Python Scipy FFT wav files) 中提取了代码,它似乎提供了我所需要的,但是当 运行将我的代码 运行 设置为以下错误:

TypeError:切片索引必须是整数或 None 或具有 index 方法

这发生在我的代码的第 9 行。我不明白为什么会这样,因为我认为 abs 函数会使它成为一个整数。

    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() 不会将您的数字变成整数。它只是将负数变成正数。当 len(c) 是奇数时,您的变量 d 是一个以 x.5.

结尾的浮点数

你想要的可能是round(d)而不是abs(d)