0

I was running ffts no problem last week, now I'm getting some strange errors:

Traceback (most recent call last):
  File "call.py", line 52, in <module>
    main()
  File "call.py", line 50, in main
    validate(temp_path)
  File "call.py", line 35, in validate
    ivr_wav = IVR_Wav(validate_path)
  File "call.py", line 42, in __init__
    self.fft = get_fft(temp_wav_path)
  File "call.py", line 21, in get_fft
    return numpy.fft.fft(frames)
  File "/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py", line 174, in fft
    return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache)
  File "/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py", line 50, in _raw_fft
    n = a.shape[axis]
IndexError: tuple index out of range

My code is:

def get_fft(fft_path):
    wav = wave.open(fft_path, 'r')
    frames = wav.readframes(wav.getnframes())
    wav.close()
    return numpy.fft.fft(frames)

and the NumPy code is:

def _raw_fft(a, n=None, axis=-1, init_function=fftpack.cffti,work_function=fftpack.cfftf, fft_cache = _fft_cache ):
    a = asarray(a)
    if n is None:
         n = a.shape[axis]
    if n < 1:
         raise ValueError("Invalid number of FFT data points (%d) specified." % n)

It makes sense that -1 is out of range for a tuple, but how can I remedy this? I don't quite know, but I believe calling numpy.fft.fft(path, axis=0) should allow the n=a.shape[axis] to execute, but will this set n to the length in samples? Is this even what is proper? I am not super familiar with numpy and nd-arrays.

Will
  • 3,611
  • 5
  • 26
  • 47

1 Answers1

1

The problem isn't your FFT, but the readFrames command. wav.readFrames returns Bytes as the output, and they're definitely not array-like, which Numpy FFT requires.

Have a look here:

What is returned by wave.readframes?

I suspect you thought this would behave like the MATLAB waveRead command, which returns an array of floats, but this does not appear to be the case here.

Edit: I got this to work by using the map() command.

waveform = map(ord, list(frames))
plt.plot(np.fft.fft(waveform))
plt.show()
Community
  • 1
  • 1
ESPeach
  • 181
  • 4
  • 8