【问题标题】:I need to FFT wav files?我需要 FFT wav 文件吗?
【发布时间】:2018-05-08 11:17:59
【问题描述】:

我看到 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

如何创建一个脚本,为参数列表中的每个文件生成频率分布?

【问题讨论】:

  • 看来a 只是一个整数。您确定您的曲目是两个频道吗?
  • @alexblae 他们都是单频道

标签: python numpy audio scipy


【解决方案1】:

您的错误消息表明您正在尝试迭代整数 (a)。当您通过

定义a
a = data.T[0]

您获取data.T 的第一个值。由于您的数据文件是单通道,因此您正在获取第一个通道的第一个值(整数)。将其更改为

a = data.T

会解决你的问题。

【讨论】:

    猜你喜欢
    • 2014-06-16
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    相关资源
    最近更新 更多