Advertisement
wandrake

Untitled

May 12th, 2012
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import struct
  4. import bisect
  5. import numpy
  6. import wave
  7. import sys
  8. import math
  9.  
  10. w = wave.open(sys.argv[1])
  11.  
  12. buf = w.readframes(65536)
  13. res = [0, 0, 0, 0, 0, 0, 0, 0]
  14. i = 0
  15. x = []
  16. blackman = numpy.blackman(65536)
  17. wss = 0.
  18.  
  19. for k in range(0, 65536):
  20.     wss += blackman[i]*blackman[i]
  21. wss *= 65536
  22.  
  23. while 65536*i < w.getnframes():
  24.     w.setpos(65536*i)
  25.     x = []
  26.     for j in range(0, len(buf)/2):
  27.         v = ord(buf[2*j+1]) * 256 + ord(buf[2*j])
  28.         if v > 32768: v = -(v ^ 65535) + 1
  29.         x.append(v*blackman[j])
  30.  
  31.     i += 1
  32.     t = numpy.fft.fft(x)
  33.  
  34.     k = -1
  35.     deltaf = 44100./65536.
  36.     for j in range (0, 65536):
  37.         k = bisect.bisect_left([20, 40, 160, 315, 2500, 5000, 10000, 20000], j*deltaf)
  38.         if k >= 0 and k <= 7:
  39.             res[k] += (abs(t[j]) ** 2.) / wss
  40.  
  41. res[0] /= 20.
  42. res[1] /= 20.
  43. res[2] /= 120.
  44. res[3] /= 315.-160.
  45. res[4] /= 2500.-315.
  46. res[5] /= 2500.
  47. res[6] /= 5000.
  48. res[7] /= 10000.
  49.  
  50. m = max(res)
  51. for n in range(0, 8):
  52.     res[n] /= m
  53.  
  54. for n in range(0, 8):
  55.     if res[n] != 0.:
  56.         print 10*math.log(res[n]),
  57.     else:
  58.         print "-infty",
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement