.. 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 :ref:`Go to the end ` 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 `_ [1]_ from librosa. This algorithm improves onset detection accuracy in the presence of vibrato. .. [1] Böck, Sebastian, and Gerhard Widmer. Maximum filter vibrato suppression for onset detection. In Proc. of the 16th Int. Conf. on Digital Audio Effects (DAFx). Maynooth, Ireland (Sept 2013), vol. 7, p. 4. 2013. .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: Python # Code source: Brian McFee # License: ISC .. GENERATED FROM PYTHON SOURCE LINES 24-25 We'll need numpy and matplotlib for this example .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt import librosa .. GENERATED FROM PYTHON SOURCE LINES 31-33 The method works fine for longer signals, but the results are harder to visualize. .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python y, sr = librosa.load(librosa.ex('trumpet', hq=True), sr=44100) .. GENERATED FROM PYTHON SOURCE LINES 38-39 These parameters are taken directly from the paper .. GENERATED FROM PYTHON SOURCE LINES 39-48 .. code-block:: Python 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-64 .. code-block:: Python S = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=n_fft, hop_length=hop_length, fmin=fmin, fmax=fmax, n_mels=n_mels) fig, ax = plt.subplots() 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, ax=ax) .. 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 .. GENERATED FROM PYTHON SOURCE LINES 65-67 Now we'll compute the onset strength envelope and onset events using the librosa defaults. .. GENERATED FROM PYTHON SOURCE LINES 67-72 .. code-block:: Python 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 73-74 And similarly with the superflux method .. GENERATED FROM PYTHON SOURCE LINES 74-85 .. code-block:: Python 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 86-92 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 92-117 .. code-block:: Python # sphinx_gallery_thumbnail_number = 2 fig, ax = plt.subplots(nrows=3, sharex=True) frame_time = librosa.frames_to_time(np.arange(len(odf_default)), sr=sr, hop_length=hop_length) 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, ax=ax[2]) ax[2].set(xlim=[0, 5.0]) ax[0].plot(frame_time, odf_default, label='Spectral flux') ax[0].vlines(onset_default, 0, odf_default.max(), color='r', label='Onsets') ax[0].legend() ax[0].label_outer() ax[1].plot(frame_time, odf_sf, color='g', label='Superflux') ax[1].vlines(onset_sf, 0, odf_sf.max(), color='r', label='Onsets') ax[1].legend() ax[0].label_outer() .. 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-timing **Total running time of the script:** (0 minutes 0.643 seconds) .. _sphx_glr_download_auto_examples_plot_superflux.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_superflux.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_superflux.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_