1

I'm trying to extract frequency from .wav files. So I'm using python wave and numpy, I'm almost done! But I face an error.. I followed this url's answer : Extracting frequencies from a wav file python

when I exract frequency from .wav file that created myself by following that answer, it succeed. However, when I exract frequency from .wav file that recorded by mic. it raised an error :

struct.error: unpack requires a buffer of 288768 bytes

following is my code

import wave
import struct
import numpy as np

if __name__ == '__main__':
    wf = wave.open('test6.wav', 'rb')
    frame = wf.getnframes()
    data_size = wf.getnframes()
    frate = wf.getframerate()
    data = wf.readframes(data_size)
    wf.close()
    duration = frame / float(frate)


    data = struct.unpack('{n}h'.format(n=data_size), data)
    data = np.array(data)

    w = np.fft.fft(data)
    freqs = np.fft.fftfreq(len(w))
    print(freqs.min(), freqs.max())
    # (-0.5, 0.499975)

    # Find the peak in the coefficients
    idx = np.argmax(np.abs(w))
    freq = freqs[idx]
    freq_in_hertz = abs(freq * frate)
    print('freqiency: ',freq_in_hertz)
    print('duration: ',duration)

288768 in error message is exactly double of data_size. So when I use data_size=wf.getnframes()*2, it does not raise error. But, it raise an error with file that created by code. How can I solve this?

Muzol
  • 301
  • 1
  • 11
Harry Kim
  • 21
  • 2

1 Answers1

0

Given that the size of the buffer is exactly double data_size, I would guess that the .wav file you recorded with your mic has two channels instead of one. You can verify this by looking at the output of wf.getnchannels(). It should be 2 for your mic recording.

If this is the case, you can load just one channel of your mic recording by following this answer: Read the data of a single channel from a stereo wave file in Python

Michael
  • 969
  • 6
  • 19