【问题标题】:WAV FFT: Slice indices must be integersWAV FFT:切片索引必须是整数
【发布时间】: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


【解决方案1】:

abs() 不会将您的数字转换为整数。它只是将负数变成正数。当len(c) 是奇数时,您的变量d 是一个以x.5 结尾的浮点数。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2017-06-08
    • 2018-03-12
    • 2016-05-25
    • 2017-09-21
    • 2019-01-08
    • 2019-07-04
    相关资源
    最近更新 更多