Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import struct
- import bisect
- import numpy
- import wave
- import sys
- import math
- w = wave.open(sys.argv[1])
- buf = w.readframes(65536)
- res = [0, 0, 0, 0, 0, 0, 0, 0]
- i = 0
- blackman = numpy.blackman(65536)
- wss = sum(map(lambda x: x*x, blackman)) * 65536
- while 65536*i < w.getnframes():
- w.setpos(65536*i)
- def valFromPCM(l):
- f = lambda(x, y): ord(y)*256+ord(x)
- t = map(f, zip(l[0::2], l[1::2]))
- return map (lambda r: -(r ^ 65535) + 1 if r > 32768 else r, t)
- values = map(lambda(x, y): x*y, zip(valFromPCM(buf), blackman))
- i += 1
- t = numpy.fft.fft(values)
- k = -1
- deltaf = 44100./65536.
- for j in range (0, 65536):
- k = bisect.bisect_left([20, 40, 160, 315, 2500, 5000, 10000, 20000], j*deltaf)
- if k >= 0 and k <= 7:
- res[k] += (abs(t[j]) ** 2.) / wss
- res = map(lambda(x, y): x/y, zip(res, [20., 20., 120., 315.-160., 2500.-315., 2500., 5000., 10000.]))
- m = max(res)
- res = map(lambda(x, y): x/y, zip(res, 8*[m]))
- for n in range(0, 8):
- if res[n] != 0.:
- print 10*math.log(res[n]),
- else:
- print "-infty",
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement