Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. def plot_signal_and_segmentation(signal, sampling_rate, segments=[]):
  2. _time = np.arange(0., np.ceil(float(len(signal))) / sampling_rate, 1./sampling_rate )
  3. if len(_time) > len(signal):
  4. _time = _time[: len(signal) - len(_time)]
  5.  
  6. pylab.subplot(211)
  7.  
  8. for seg in segments:
  9.  
  10. fc = seg.get("fc", "g")
  11. ec = seg.get("ec", "b")
  12. lw = seg.get("lw", 2)
  13. alpha = seg.get("alpha", 0.4)
  14.  
  15. ts = seg["timestamps"]
  16.  
  17. # plot first segmentation outside loop to show one single legend for this class
  18. p = pylab.axvspan(ts[0][0], ts[0][1], fc=fc, ec=ec, lw=lw, alpha=alpha, label = seg.get("title", ""))
  19.  
  20. for start, end in ts[1:]:
  21. p = pylab.axvspan(start, end, fc=fc, ec=ec, lw=lw, alpha=alpha)
  22.  
  23.  
  24. pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
  25. borderaxespad=0., fontsize=16, ncol=2)
  26.  
  27. pylab.plot(_time, signal)
  28.  
  29. pylab.xlabel("Time (s)", fontsize=16)
  30. pylab.ylabel("Signal Amplitude", fontsize=16)
  31. pylab.show()
  32.  
  33. # ช่วงเวลาในแต่ละคลาสที่โมเดลทำนาย
  34. classes = { 0: [(0.00, 2.00), ...], 1: [(2.01, 2.20), ...] }
  35. speech = { "fc" : "r", "ec" : "r", "lw" : 0, "alpha" : 0.4, "title" : "Speech", "timestamps" : classes[1]}
  36. music = { "fc" : "y", "ec" : "y", "lw" : 0, "alpha" : 0.4, "title" : "Music", "timestamps" : classes[0]}
  37.  
  38. audio, sr = librosa.load(<PATH_TO_AUDIO>)
  39. plot_signal_and_segmentation(audio, sr, [music, speech])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement