Skip to content

Instantly share code, notes, and snippets.

@fchorney
Created January 18, 2012 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fchorney/1635136 to your computer and use it in GitHub Desktop.
Save fchorney/1635136 to your computer and use it in GitHub Desktop.
import struct
import wave
import numpy
from numpy import *
from numpy.fft import fft
def main():
w = wave.open('FGI.wav', 'r')
data_size = w.getnframes() * w.getnchannels()
frame_size = 0.025 # 25ms
hop_size = 0.010 # 10ms
data = w.readframes(data_size)
w.close()
data = struct.unpack('{n}h'.format(n=data_size), data)
X = stft(data, 44100, frame_size, hop_size)
print len(X)
derp = X[22000]
f = open('testdata.dat', 'w')
count = 0
for idx in range(len(derp['fft']) / 2):
f.write("%s %s\n" % (count,numpy.abs(derp['fft'][idx])))
count = count + 1
f.close()
def stft(x, fs, framesz, hop):
framesamp = int(framesz*fs)
hopsamp = int(hop*fs)
w = numpy.hamming(framesamp)
X = []
for i in range(0, len(x) - framesamp, hopsamp):
b = numpy.array(w * x[i:i + framesamp])
a = fft(b)
c = numpy.fft.fftfreq(len(a))
v = {
'fft': numpy.fft.fftshift(a),
'freq': numpy.fft.fftshift(c)}
X.append(v)
return X
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment