def plot_results(sr, audio, onsets, savefile):

    if audio.ndim > 1:
        audio = np.mean(audio, axis=1)
    t = np.linspace(0, len(audio) / sr, len(audio))

    plt.plot(t, audio, color='dodgerblue')

    if len(onsets) > 0:
        for o in onsets:
            plt.axvline(o / sr, color='red', linestyle='--')

    plt.xlabel('Time (s)')
    plt.grid()

    sp = SavePlot(True, savefile=savefile, auto_overwrite=True)
    sp.plot(plt)
示例#2
0
ax2.set_yticks(ytx)

ax1.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax1.xaxis.tick_top()
ax1.tick_params(labeltop=False)  # don't put tick labels at the top
ax2.xaxis.tick_bottom()

d = .015  # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
grad = ratio[1] / ratio[0]
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((-d, +d), (-grad * d, +grad * d), **kwargs)  # top-left diagonal
ax1.plot((1 - d, 1 + d), (-grad * d, +grad * d),
         **kwargs)  # top-right diagonal

kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)  # bottom-left diagonal
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)  # bottom-right diagonal

handles, labels = ax1.get_legend_handles_labels()
labels, handles = zip(*sorted(zip(map(float, labels), handles)))
labels = map(formatLabel, labels)
ax2.legend(handles, labels, title='Damping factor', bbox_to_anchor=(0.96, 1.2))

ax2.set_xlabel('Time (s)')
ax2.set_ylabel('|z|', rotation='horizontal')
ax2.yaxis.set_label_coords(-0.11, (ax1_size + ax2_size) / 2)

sp.plot(plt)
def plot_segments(file, threshold=None, save=False, audio_file=None):

    sns.set_style('whitegrid')

    plot_onsets = True  # False #

    plot_ms = False  #True #

    savepath = '/home/keziah/onsets/hsj/Visualisation/Iowa/'
    #    if plot_onsets:
    #        figdir = '{}_octaves_with_onsets'.format('xylophone') #'piano'
    #    else:
    #        figdir = '{}_octaves_without_onsets'.format('xylophone') #'piano'
    figdir = ''
    figdir = os.path.join(savepath, figdir)
    if not os.path.exists(figdir):
        os.makedirs(figdir)

    arr = np.loadtxt(file)

    #    arr = arr[:-1]

    # plot first 500 segments
    i0 = 0
    i1 = len(arr)  # 500 # 20 #

    seg_size = 20e-3

    title = file.split(os.path.sep)[-2]

    sr, manual_onsets = get_manual_onsets(file)
    sr, found_onsets = get_found_onsets(file)
    found_onsets /= sr

    if audio_file is not None:
        sp = SavePlot(True,
                      os.path.join(savepath, title + '_waveform.pdf'),
                      auto_overwrite=True)
        plot_audio(audio_file, sp)  #, manual_onsets, found_onsets)

    t = np.arange(0, len(arr), dtype=float)
    t *= seg_size
    if plot_ms:
        t *= 1000

    plt.plot(t[i0:i1], arr[i0:i1])

    correct, diff = CheckOnsets.compare(manual_onsets, found_onsets, 50)

    if plot_onsets:
        if correct.size > 0:
            for n, idx in enumerate(correct):
                # correctly found onset
                onset = found_onsets[idx]
                # remove corresponding onset from manual_onsets
                man = manual_onsets + (diff[n] / 1000)
                w = np.where(np.isclose(man, onset))[0]
                manual_onsets = np.delete(manual_onsets, w)
                # plot onset
                if onset <= i1 * seg_size:
                    if plot_ms:
                        onset *= 1000
                    plt.axvline(onset, color='lime', label='True positive')

            # remove correct onsets from found
            found_onsets = np.delete(found_onsets, correct)
#        else:
#            for onset in manual_onsets:
#                plt.axvline(onset, color='red', linestyle='--')

# plot false negatives
        for onset in manual_onsets:
            if onset <= i1 * seg_size:
                if plot_ms:
                    onset *= 1000
                plt.axvline(onset,
                            color='indigo',
                            linestyle='--',
                            label='False negative')
        # plot false positives
        for onset in found_onsets:
            if onset <= i1 * seg_size:  # t[i1-1]:
                if plot_ms:
                    onset *= 1000
                plt.axvline(onset,
                            color='mediumorchid',
                            linestyle='--',
                            label='False positive')

    if threshold is not None:
        threshold = np.log(threshold)
        plt.axhline(threshold, color='green', linestyle='--')

#    plt.title(title)

    plt.grid(True)
    if plot_ms:
        xlabel = 'Time (ms)'
    else:
        xlabel = 'Time (s)'
    plt.xlabel(xlabel)
    plt.ylabel('Mean log')

    #    plt.legend()

    sp = SavePlot(
        save,
        os.path.join(figdir, title + '.pdf'),  #+'_w_legend.pdf'),
        auto_overwrite=True)
    sp.plot(plt)