.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/beats_onsets/plot_onset_detection.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_gallery_beats_onsets_plot_onset_detection.py: Onset detection =============== The onset strength envelope and the peaks picked from it, as times in seconds. .. GENERATED FROM PYTHON SOURCE LINES 7-14 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import audio_samples as aus from audio_samples import effects .. GENERATED FROM PYTHON SOURCE LINES 15-16 Four decaying clicks half a second apart. .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python sr = 22050 times = np.array([0.25, 0.75, 1.25, 1.75]) y = effects.clicks(times=times, sr=sr, length=2 * sr, click_duration=0.05) audio = aus.AudioSamples.new_mono(y.astype(np.float32), sr) audio .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.9587 (-0.4 dBFS)
RMS0.0600 (-24.4 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 23-34 .. code-block:: Python envelope = audio.onset_strength()[0] onsets = audio.onset_detect(units="time") print(onsets) assert np.max(np.abs(onsets - times)) < 0.05 t = np.arange(envelope.size) * 512 / sr fig, ax = plt.subplots(figsize=(7, 3)) ax.plot(t, envelope, label="Onset strength") ax.vlines(onsets, 0, envelope.max(), color="C3", ls="--", label="Detected onsets") ax.set(xlabel="Time (s)", title="Onset detection") ax.legend() .. image-sg:: /gallery/beats_onsets/images/sphx_glr_plot_onset_detection_001.png :alt: Onset detection :srcset: /gallery/beats_onsets/images/sphx_glr_plot_onset_detection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [0.2554195 0.7662585 1.25387755 1.76471655] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.082 seconds) .. _sphx_glr_download_gallery_beats_onsets_plot_onset_detection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_onset_detection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_onset_detection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_onset_detection.zip `