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



我正在尝试对.wav文件进行一些分析,我从下面的问题中提取了代码(Python Scipy FFT wav文件(,它似乎正好给出了我需要的东西,但当运行代码时,我遇到了以下错误:

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

这发生在我的代码的第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)

最新更新