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
- x = []
- blackman = numpy.blackman(65536)
- wss = 0.
- for k in range(0, 65536):
- wss += blackman[i]*blackman[i]
- wss *= 65536
- while 65536*i < w.getnframes():
- w.setpos(65536*i)
- x = []
- for j in range(0, len(buf)/2):
- v = ord(buf[2*j+1]) * 256 + ord(buf[2*j])
- if v > 32768: v = -(v ^ 65535) + 1
- x.append(v*blackman[j])
- i += 1
- t = numpy.fft.fft(x)
- 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[0] /= 20.
- res[1] /= 20.
- res[2] /= 120.
- res[3] /= 315.-160.
- res[4] /= 2500.-315.
- res[5] /= 2500.
- res[6] /= 5000.
- res[7] /= 10000.
- m = max(res)
- for n in range(0, 8):
- res[n] /= 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