[英]WAV FFT: Slice indices must be integers
我正在尝试对 .wav 文件进行一些分析,我从以下问题( Python Scipy FFT wav 文件)中获取了代码,它似乎提供了我所需要的内容,但是在运行代码时我遇到了以下错误:
类型错误:切片索引必须是整数或无或具有索引方法
这发生在我的代码的第 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
结尾的x.5
。
你想要的可能是round(d)
而不是abs(d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.