Guest User

Untitled

a guest
Mar 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import wave
  2. import struct
  3. import sys
  4. import numpy as np
  5. from math import sqrt
  6. import matplotlib
  7. matplotlib.use('Agg')
  8. from matplotlib import pylab
  9. import matplotlib.pyplot as plt
  10.  
  11. filename = sys.argv[1]
  12.  
  13. if __name__ == '__main__':
  14. # Open the wave file and get info
  15. wave_file = wave.open(filename, 'r')
  16. data_size = wave_file.getnframes()
  17. sample_rate = wave_file.getframerate()
  18. print "Sample rate: %s" % sample_rate
  19. sample_width = wave_file.getsampwidth()
  20. duration = data_size / float(sample_rate)
  21.  
  22. # Read in sample data
  23. sound_data = wave_file.readframes(data_size)
  24.  
  25. # Close the file, as we don't need it any more
  26. wave_file.close()
  27.  
  28. # Unpack the binary data into an array
  29. unpack_fmt = '%dh' % (data_size)
  30. sound_data = struct.unpack(unpack_fmt, sound_data)
  31.  
  32. # Process many samples
  33. fouriers_per_second = 24 # Frames per second
  34. fourier_spread = 1.0/fouriers_per_second
  35. fourier_width = fourier_spread
  36. fourier_width_index = fourier_width * float(sample_rate)
  37.  
  38. if len(sys.argv) < 3:
  39. length_to_process = int(duration)-1
  40. else:
  41. length_to_process = float(sys.argv[2])
  42.  
  43. print "Fourier width: %s" % str(fourier_width)
  44.  
  45. total_transforms = int(round(length_to_process * fouriers_per_second))
  46. fourier_spacing = round(fourier_spread * float(sample_rate))
  47.  
  48. print "Duration: %s" % duration
  49. print "For Fourier width of "+str(fourier_width)+" need "+str(fourier_width_index)+" samples each FFT"
  50. print "Doing "+str(fouriers_per_second)+" Fouriers per second"
  51. print "Total " + str(total_transforms * fourier_spread)
  52. print "Spacing: "+str(fourier_spacing)
  53. print "Total transforms "+str(total_transforms)
  54.  
  55. lastpoint=int(round(length_to_process*float(sample_rate)+fourier_width_index))-1
  56.  
  57. sample_size = fourier_width_index
  58. freq = sample_rate / sample_size * np.arange(sample_size)
  59.  
  60. x_axis = range(0, 12)
  61.  
  62. def getBandWidth():
  63. return (2.0/sample_size) * (sample_rate / 2.0)
  64.  
  65. def freqToIndex(f):
  66. # If f (frequency is lower than the bandwidth of spectrum[0]
  67. if f < getBandWidth()/2:
  68. return 0
  69. if f > (sample_rate / 2) - (getBandWidth() / 2):
  70. return sample_size -1
  71. fraction = float(f) / float(sample_rate)
  72. index = round(sample_size * fraction)
  73. return index
  74.  
  75. fft_averages = []
  76.  
  77. def average_fft_bands(fft_array):
  78. num_bands = 12 # The number of frequency bands (12 = 1 octave)
  79. del fft_averages[:]
  80. for band in range(0, num_bands):
  81. avg = 0.0
  82.  
  83. if band == 0:
  84. lowFreq = int(0)
  85. else:
  86. lowFreq = int(int(sample_rate / 2) / float(2 ** (num_bands - band)))
  87. hiFreq = int((sample_rate / 2) / float(2 ** ((num_bands-1) - band)))
  88. lowBound = int(freqToIndex(lowFreq))
  89. hiBound = int(freqToIndex(hiFreq))
  90. for j in range(lowBound, hiBound):
  91. avg += fft_array[j]
  92.  
  93. avg /= (hiBound - lowBound + 1)
  94. fft_averages.append(avg)
  95.  
  96. for offset in range(0, total_transforms):
  97. start = int(offset * sample_size)
  98. end = int((offset * sample_size) + sample_size -1)
  99.  
  100. print "Processing sample %i of %i (%d seconds)" % (offset + 1, total_transforms, end/float(sample_rate))
  101. sample_range = sound_data[start:end]
  102. ## FFT the data
  103. fft_data = abs(np.fft.fft(sample_range))
  104. # Normalise the data a second time, to make numbers sensible
  105. fft_data *= ((2**.5)/sample_size)
  106. plt.ylim(0, 1000)
  107. average_fft_bands(fft_data)
  108. y_axis = fft_averages
  109. """Stuff for bar graph"""
  110. width = 0.35
  111. p1 = plt.bar(x_axis, y_axis, width, color='r')
  112. """End bar graph stuff"""
  113. filename = str('frame_%05d' % offset) + '.png'
  114. plt.savefig(filename, dpi=100)
  115. plt.close()
  116. print "DONE!"
Add Comment
Please, Sign In to add comment