.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/beats_onsets/plot_beat_tracking.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_beat_tracking.py: Beat tracking and tempo ======================= ``beat_track`` estimates the tempo and places beats on the onset envelope. The helper ``mpl.plot_beats`` draws them over the waveform. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import numpy as np import audio_samples as aus from audio_samples import effects, mpl .. GENERATED FROM PYTHON SOURCE LINES 15-16 A click track at 120 beats per minute. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python sr = 22050 times = np.arange(0.5, 8.0, 0.5) y = effects.clicks(times=times, sr=sr, length=8 * sr) audio = aus.AudioSamples.new_mono(y.astype(np.float32), sr) .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python tempo, beats = audio.beat_track(units="time") print(f"tempo {float(np.ravel(tempo)[0]):.1f} BPM, {beats.size} beats") assert abs(float(np.ravel(tempo)[0]) - 120.0) < 3.0 gaps = np.diff(beats) assert np.max(np.abs(gaps - 0.5)) < 0.03 fig, ax = mpl.plot_beats(audio) .. image-sg:: /gallery/beats_onsets/images/sphx_glr_plot_beat_tracking_001.png :alt: Beat Tracking — 120.0 BPM :srcset: /gallery/beats_onsets/images/sphx_glr_plot_beat_tracking_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none tempo 117.5 BPM, 15 beats .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.626 seconds) .. _sphx_glr_download_gallery_beats_onsets_plot_beat_tracking.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_beat_tracking.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_beat_tracking.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_beat_tracking.zip `