Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def plot_signal_and_segmentation(signal, sampling_rate, segments=[]):
- _time = np.arange(0., np.ceil(float(len(signal))) / sampling_rate, 1./sampling_rate )
- if len(_time) > len(signal):
- _time = _time[: len(signal) - len(_time)]
- pylab.subplot(211)
- for seg in segments:
- fc = seg.get("fc", "g")
- ec = seg.get("ec", "b")
- lw = seg.get("lw", 2)
- alpha = seg.get("alpha", 0.4)
- ts = seg["timestamps"]
- # plot first segmentation outside loop to show one single legend for this class
- p = pylab.axvspan(ts[0][0], ts[0][1], fc=fc, ec=ec, lw=lw, alpha=alpha, label = seg.get("title", ""))
- for start, end in ts[1:]:
- p = pylab.axvspan(start, end, fc=fc, ec=ec, lw=lw, alpha=alpha)
- pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
- borderaxespad=0., fontsize=16, ncol=2)
- pylab.plot(_time, signal)
- pylab.xlabel("Time (s)", fontsize=16)
- pylab.ylabel("Signal Amplitude", fontsize=16)
- pylab.show()
- # ช่วงเวลาในแต่ละคลาสที่โมเดลทำนาย
- classes = { 0: [(0.00, 2.00), ...], 1: [(2.01, 2.20), ...] }
- speech = { "fc" : "r", "ec" : "r", "lw" : 0, "alpha" : 0.4, "title" : "Speech", "timestamps" : classes[1]}
- music = { "fc" : "y", "ec" : "y", "lw" : 0, "alpha" : 0.4, "title" : "Music", "timestamps" : classes[0]}
- audio, sr = librosa.load(<PATH_TO_AUDIO>)
- plot_signal_and_segmentation(audio, sr, [music, speech])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement