Created
January 30, 2015 17:39
-
-
Save dos1/27580fc0ffb94ecad333 to your computer and use it in GitHub Desktop.
Gender recognition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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