Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import numpy as np
  2. import scipy.io.wavfile as wave
  3. import python_speech_features as psf
  4. from pydub import AudioSegment
  5.  
  6. #your sound file
  7. filepath = 'my-sound.wav'
  8.  
  9. def convert(path):
  10.  
  11. #open file (supports all ffmpeg supported filetypes)
  12. audio = AudioSegment.from_file(path, path.split('.')[-1].lower())
  13.  
  14. #set to mono
  15. audio = audio.set_channels(1)
  16.  
  17. #set to 44.1 KHz
  18. audio = audio.set_frame_rate(44100)
  19.  
  20. #save as wav
  21. audio.export(path, format="wav")
  22.  
  23. def getSpectrogram(path, winlen=0.025, winstep=0.01, NFFT=512):
  24.  
  25. #open wav file
  26. (rate,sig) = wave.read(path)
  27.  
  28. #get frames
  29. winfunc=lambda x:np.ones((x,))
  30. frames = psf.sigproc.framesig(sig, winlen*rate, winstep*rate, winfunc)
  31.  
  32. #Magnitude Spectrogram
  33. magspec = np.rot90(psf.sigproc.magspec(frames, NFFT))
  34.  
  35. #noise reduction (mean substract)
  36. magspec -= magspec.mean(axis=0)
  37.  
  38. #normalize values between 0 and 1
  39. magspec -= magspec.min(axis=0)
  40. magspec /= magspec.max(axis=0)
  41.  
  42. #show spec dimensions
  43. print magspec.shape
  44.  
  45. return magspec
  46.  
  47. #convert file if you need to
  48. convert(filepath)
  49.  
  50. #get spectrogram
  51. spec = getSpectrogram(filepath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement