Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. """
  2. multimedia project
  3. editor : ys kim
  4. cutoff frequency above(or below) threshold
  5. """
  6.  
  7. from numpy import *
  8. from scipy import fftpack
  9.  
  10. from utility.sound_util import *
  11. from utility.plotly_util import showSignalFFT
  12.  
  13. input_name = 'saw'
  14. output_name = 'saw_cutoff_frequency'
  15.  
  16. input_path = file_DIR + input_name + '.wav'
  17. output_path = file_DIR + output_name + '.wav'
  18.  
  19. input = wave.open(input_path, 'r')
  20. nChannels = input.getnchannels()
  21. FrameRate = input.getframerate()
  22. sampWidth = input.getsampwidth()
  23. nFrames = input.getnframes()
  24.  
  25. if nChannels == 2:
  26. source = stereo2mono(stereo2str(input))
  27. nChannels = 1
  28. else:
  29. source = wav2str(input)
  30.  
  31. # generate window function
  32.  
  33. N = 4096
  34. sWin = zeros((N, 2))
  35. sWin[:, 0] = hanning(N)
  36. sWin[:, 1] = hanning(N)
  37.  
  38. # parameters
  39.  
  40. _nFrame = int(nFrames / N)
  41. n = int(len(source) / nChannels)
  42. k = np.arange(n)
  43. T = n / FrameRate
  44. frq = k / T
  45. frq = frq[range(int(n / 2))]
  46.  
  47. han_stereo = zeros((N, 2))
  48. han_stereo[:, 0] = hanning(N)
  49. han_stereo[:, 1] = hanning(N)
  50.  
  51. outVec = np.array([])
  52.  
  53. # adjust window function to frequency in each frame
  54. for i in range(_nFrame):
  55. if nChannels == 1:
  56. subSignal = source[i * N: (i + 1) * N]
  57. subSignal = subSignal * hanning(N)
  58. else:
  59. break
  60.  
  61. xv = fftpack.rfft(subSignal) * hanning(N)
  62. xout = fftpack.irfft(xv)
  63. outVec = np.append(outVec, xout)
  64.  
  65. signal = fftpack.rfft(source)
  66. cut_freq_signal_ = signal
  67.  
  68. threshold = 100000
  69. for i in range(len(cut_freq_signal_)):
  70. if i < threshold:
  71. cut_freq_signal_[i] = 0
  72.  
  73. signal_ret = fftpack.irfft(cut_freq_signal_)
  74.  
  75. cut_freq_signal_ = cut_freq_signal_[range(int(n / 2))]
  76.  
  77. output = wave.open(output_path, 'w')
  78. output.setparams((nChannels, sampWidth, FrameRate, nFrames, 'NONE', 'not compressed'))
  79.  
  80. copyWav(output, cut_freq_signal_)
  81.  
  82. input.close()
  83. output.close()
  84.  
  85. # edit volume
  86. incVolume(output_name)
  87.  
  88. showSignalFFT(signal_ret, frq, cut_freq_signal_)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement