我看到this question and answer关于在wav文件上使用fft并试图像这样实现它:
import matplotlib.pyplot as plt
from scipy.io import wavfile # get the api
from scipy.fftpack import fft
from pylab import *
import sys
def f(filename):
fs, data = wavfile.read(filename) # 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) # create a list of complex number
d = len(c)/2 # you only need half of the fft list
plt.plot(abs(c[:(d-1)]),'r')
savefig(filename+'.png',bbox_inches='tight')
files = sys.argv[1:]
for ele in files:
f(ele)
quit()
但每当我打电话时:
$ python fft.py 0.0/4515-11057-0058.flac.wav-16000.wav
我收到错误:
Traceback (most recent call last): File "fft.py", line 18, in <module> f(ele) File "fft.py", line 10, in f b=[(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1) TypeError: 'numpy.int16' object is not iterable
如何创建一个脚本,为参数列表中的每个文件生成频率分布?
答案 0 :(得分:1)
您的错误消息指出您正在尝试迭代整数(a
)。通过
a
时
a = data.T[0]
你抓住data.T
的第一个值。由于您的数据文件是单通道,因此您将获取第一个通道的第一个值(整数)。将此更改为
a = data.T
将解决您的问题。