user3310334
user3310334

Reputation:

I need to FFT wav files?

I saw this question and answer about using fft on wav files and tried to implement it like this:

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()

But whenever I call it:

$ python fft.py 0.0/4515-11057-0058.flac.wav-16000.wav

I get the error:

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

How can I create a script that generates frequency distributions for each file in the list of arguments?

Upvotes: 0

Views: 967

Answers (1)

alexblae
alexblae

Reputation: 746

Your error message states that you are trying to iterate over an integer (a). When you define a via

a = data.T[0]

you grab the first value of data.T. Since your data files are single channel, you are taking the first value of the first channel (an integer). Changing this to

a = data.T

will fix your problem.

Upvotes: 1

Related Questions