.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_superflux.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_superflux.py: ================ Superflux onsets ================ This notebook demonstrates how to recover the Superflux onset detection algorithm of `Boeck and Widmer, 2013 `_ from librosa. This algorithm improves onset detection accuracy in the presence of vibrato. .. GENERATED FROM PYTHON SOURCE LINES 13-17 .. code-block:: default # Code source: Brian McFee # License: ISC .. GENERATED FROM PYTHON SOURCE LINES 18-19 We'll need numpy and matplotlib for this example .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: default from __future__ import print_function import numpy as np import matplotlib.pyplot as plt import librosa import librosa.display .. GENERATED FROM PYTHON SOURCE LINES 27-31 We'll load in a five-second clip of a track that has noticeable vocal vibrato. The method works fine for longer signals, but the results are harder to visualize. .. GENERATED FROM PYTHON SOURCE LINES 31-37 .. code-block:: default y, sr = librosa.load('audio/Karissa_Hobbs_-_09_-_Lets_Go_Fishin.mp3', sr=44100, duration=5, offset=35) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /tmp/tmpfl4ra6qp/b0064fe7dbe8048b1d4148e61a568b6fe3fca91b/librosa/core/audio.py:161: UserWarning: PySoundFile failed. Trying audioread instead. warnings.warn('PySoundFile failed. Trying audioread instead.') .. GENERATED FROM PYTHON SOURCE LINES 38-39 These parameters are taken directly from the paper .. GENERATED FROM PYTHON SOURCE LINES 39-48 .. code-block:: default n_fft = 1024 hop_length = int(librosa.time_to_samples(1./200, sr=sr)) lag = 2 n_mels = 138 fmin = 27.5 fmax = 16000. max_size = 3 .. GENERATED FROM PYTHON SOURCE LINES 49-51 The paper uses a log-frequency representation, but for simplicity, we'll use a Mel spectrogram instead. .. GENERATED FROM PYTHON SOURCE LINES 51-65 .. code-block:: default S = librosa.feature.melspectrogram(y, sr=sr, n_fft=n_fft, hop_length=hop_length, fmin=fmin, fmax=fmax, n_mels=n_mels) plt.figure(figsize=(6, 4)) librosa.display.specshow(librosa.power_to_db(S, ref=np.max), y_axis='mel', x_axis='time', sr=sr, hop_length=hop_length, fmin=fmin, fmax=fmax) plt.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_plot_superflux_001.png :alt: plot superflux :srcset: /auto_examples/images/sphx_glr_plot_superflux_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /tmp/tmpfl4ra6qp/b0064fe7dbe8048b1d4148e61a568b6fe3fca91b/librosa/display.py:862: MatplotlibDeprecationWarning: The 'basey' parameter of __init__() has been renamed 'base' since Matplotlib 3.3; support for the old name will be dropped two minor releases later. scaler(mode, **kwargs) /tmp/tmpfl4ra6qp/b0064fe7dbe8048b1d4148e61a568b6fe3fca91b/librosa/display.py:862: MatplotlibDeprecationWarning: The 'linthreshy' parameter of __init__() has been renamed 'linthresh' since Matplotlib 3.3; support for the old name will be dropped two minor releases later. scaler(mode, **kwargs) .. GENERATED FROM PYTHON SOURCE LINES 66-68 Now we'll compute the onset strength envelope and onset events using the librosa defaults. .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: default odf_default = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length) onset_default = librosa.onset.onset_detect(y=y, sr=sr, hop_length=hop_length, units='time') .. GENERATED FROM PYTHON SOURCE LINES 74-75 And similarly with the superflux method .. GENERATED FROM PYTHON SOURCE LINES 75-86 .. code-block:: default odf_sf = librosa.onset.onset_strength(S=librosa.power_to_db(S, ref=np.max), sr=sr, hop_length=hop_length, lag=lag, max_size=max_size) onset_sf = librosa.onset.onset_detect(onset_envelope=odf_sf, sr=sr, hop_length=hop_length, units='time') .. GENERATED FROM PYTHON SOURCE LINES 87-93 If you look carefully, the default onset detector (top sub-plot) has several false positives in high-vibrato regions, eg around 0.62s or 1.80s. The superflux method (middle plot) is less susceptible to vibrato, and does not detect onset events at those points. .. GENERATED FROM PYTHON SOURCE LINES 93-125 .. code-block:: default # sphinx_gallery_thumbnail_number = 2 plt.figure(figsize=(6, 6)) frame_time = librosa.frames_to_time(np.arange(len(odf_default)), sr=sr, hop_length=hop_length) ax = plt.subplot(2, 1, 2) librosa.display.specshow(librosa.power_to_db(S, ref=np.max), y_axis='mel', x_axis='time', sr=sr, hop_length=hop_length, fmin=fmin, fmax=fmax) plt.xlim([0, 5.0]) plt.axis('tight') plt.subplot(4, 1, 1, sharex=ax) plt.plot(frame_time, odf_default, label='Spectral flux') plt.vlines(onset_default, 0, odf_default.max(), label='Onsets') plt.xlim([0, 5.0]) plt.legend() plt.subplot(4, 1, 2, sharex=ax) plt.plot(frame_time, odf_sf, color='g', label='Superflux') plt.vlines(onset_sf, 0, odf_sf.max(), label='Onsets') plt.xlim([0, 5.0]) plt.legend() plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_superflux_002.png :alt: plot superflux :srcset: /auto_examples/images/sphx_glr_plot_superflux_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /tmp/tmpfl4ra6qp/b0064fe7dbe8048b1d4148e61a568b6fe3fca91b/librosa/display.py:862: MatplotlibDeprecationWarning: The 'basey' parameter of __init__() has been renamed 'base' since Matplotlib 3.3; support for the old name will be dropped two minor releases later. scaler(mode, **kwargs) /tmp/tmpfl4ra6qp/b0064fe7dbe8048b1d4148e61a568b6fe3fca91b/librosa/display.py:862: MatplotlibDeprecationWarning: The 'linthreshy' parameter of __init__() has been renamed 'linthresh' since Matplotlib 3.3; support for the old name will be dropped two minor releases later. scaler(mode, **kwargs) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.691 seconds) .. _sphx_glr_download_auto_examples_plot_superflux.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_superflux.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_superflux.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_