Skip to content

Instantly share code, notes, and snippets.

@dos1
Created January 30, 2015 17:39
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 dos1/27580fc0ffb94ecad333 to your computer and use it in GitHub Desktop.
Save dos1/27580fc0ffb94ecad333 to your computer and use it in GitHub Desktop.
Gender recognition
#!/usr/bin/env python
import sys, wave, numpy, struct
wav = wave.open(sys.argv[1], "r")
(nchannels, sampwidth, rate, nframes, comptype, compname) = wav.getparams()
frames = wav.readframes(nframes * nchannels)
out = struct.unpack_from("%dh" % nframes * nchannels, frames)
signal = numpy.array(out if nchannels == 1 else list([(out[i]+out[i+1])/2 for i in range(0, len(out), 2)]))[::4]
amplitude = abs(numpy.fft.fft(signal))
freq = numpy.linspace(0, rate / 4, len(amplitude))
zipped = filter(lambda x: 85 <= x[0] <= 250, zip(freq, amplitude))
sum1, sum2 = (sum([a for _, a in zipped[:len(zipped)/2]]), sum([a for _, a in zipped[len(zipped)/2:]]))
print "M" if sum2/sum1 < 2 else "K"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment