Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. def read_samplepoints(file_name):
  2.  
  3. sampFreq, snd1 = wavfile.read(file_name)
  4.  
  5. samp_points = len(snd1)
  6.  
  7. data_type = snd1.dtype
  8.  
  9. return samp_points, data_type
  10.  
  11. def maximum_amplitude(file_name):
  12.  
  13. sampFreq, snd = wavfile.read(file_name)
  14.  
  15. snd = snd / (2.**15)
  16.  
  17. s1 = snd[:,0]
  18.  
  19. s2 = snd[:,1]
  20.  
  21. max_s1 = np.max(s1)
  22.  
  23. max_s2 = np.max(s2)
  24.  
  25. return max_s1, max_s2
  26.  
  27.  
  28. def plot_signal(file_name):
  29.  
  30. sampFreq, snd = wavfile.read(file_name)
  31.  
  32. snd = snd / (2.**15)
  33.  
  34. s1 = snd[:,0]
  35.  
  36. s2 = snd[:,1]
  37.  
  38. timeArray = arange(0, len(snd), 1)
  39. timeArray = timeArray / sampFreq
  40. timeArray = timeArray * 1000 #scale to milliseconds
  41.  
  42. plt.plot(timeArray, s1, color='k')
  43. plt.ylabel('LeftChannel_Amplitude')
  44. plt.xlabel('Time (ms)')
  45. plt.show()
  46.  
  47. timeArray2 = arange(0, len(snd), 1)
  48. timeArray2 = timeArray2 / sampFreq
  49. timeArray2 = timeArray2 * 1000 #scale to milliseconds
  50.  
  51. plt.plot(timeArray2, s2, color='k')
  52. plt.ylabel('RightChannel_Amplitude')
  53. plt.xlabel('Time (ms)')
  54. plt.show()
  55.  
  56. n = len(s1)
  57. p = fft(s1) # take the fourier transform
  58.  
  59. m = len(s2)
  60. p2 = fft(s2)
  61.  
  62. nUniquePts = ceil((n+1)/2.0)
  63. p = p[0:nUniquePts]
  64. p = abs(p)
  65.  
  66. mUniquePts = ceil((m+1)/2.0)
  67. p2 = p2[0:mUniquePts]
  68. p2 = abs(p2)
  69.  
  70. '''
  71. Left Channel
  72. '''
  73. p = p / float(n) # scale by the number of points so that
  74. # the magnitude does not depend on the length
  75. # of the signal or on its sampling frequency
  76. p = p**2 # square it to get the power
  77.  
  78.  
  79.  
  80.  
  81. # multiply by two (see technical document for details)
  82. # odd nfft excludes Nyquist point
  83. if n % 2 > 0: # we've got odd number of points fft
  84. p[1:len(p)] = p[1:len(p)] * 2
  85. else:
  86. p[1:len(p) -1] = p[1:len(p) - 1] * 2 # we've got even number of points fft
  87.  
  88. freqArray = arange(0, nUniquePts, 1.0) * (sampFreq / n);
  89. plt.plot(freqArray/1000, 10*log10(p), color='k')
  90. plt.xlabel('LeftChannel_Frequency (kHz)')
  91. plt.ylabel('LeftChannel_Power (dB)')
  92. plt.show()
  93.  
  94. '''
  95. Right Channel
  96. '''
  97. p2 = p2 / float(m) # scale by the number of points so that
  98. # the magnitude does not depend on the length
  99. # of the signal or on its sampling frequency
  100. p2 = p2**2 # square it to get the power
  101.  
  102.  
  103.  
  104.  
  105. # multiply by two (see technical document for details)
  106. # odd nfft excludes Nyquist point
  107. if m % 2 > 0: # we've got odd number of points fft
  108. p2[1:len(p2)] = p2[1:len(p2)] * 2
  109. else:
  110. p2[1:len(p2) -1] = p2[1:len(p2) - 1] * 2 # we've got even number of points fft
  111.  
  112. freqArray2 = arange(0, mUniquePts, 1.0) * (sampFreq / m);
  113. plt.plot(freqArray2/1000, 10*log10(p2), color='k')
  114. plt.xlabel('RightChannel_Frequency (kHz)')
  115. plt.ylabel('RightChannel_Power (dB)')
  116. plt.show()
  117.  
  118. max_p = np.max(10*log10(p))
  119.  
  120. max_p2 = np.max(10*log10(p2))
  121.  
  122. return max_p, max_p2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement