【发布时间】:2021-01-29 18:43:48
【问题描述】:
我正在尝试对 .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(),比如c[:abs(d-1)] -
@AbhinavMathur 如果我改变位置,我仍然会遇到同样的错误,我不确定为什么它似乎适用于我链接的堆栈问题并且代码完全相同。
标签: python python-3.x scipy signal-processing wav