.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pitch/plot_yin_and_pyin.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_pitch_plot_yin_and_pyin.py: Pitch tracking with YIN and pYIN ================================ ``yin`` gives one estimate per frame. ``pyin`` adds a voiced or unvoiced decision per frame, so silence is marked rather than given a meaningless pitch. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import audio_samples as aus .. GENERATED FROM PYTHON SOURCE LINES 15-16 A vibrato tone around 220 Hz, then silence. .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. code-block:: Python sr = 22050 t = np.arange(sr) / sr phase = 2 * np.pi * (220.0 * t + 4.0 * np.sin(2 * np.pi * 5.0 * t) / (2 * np.pi * 5.0) * 2.0) y = np.concatenate([0.5 * np.sin(phase), np.zeros(sr // 2)]).astype(np.float32) audio = aus.AudioSamples.new_mono(y, sr) audio .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration1.500 s (33075 samples per channel)
peak0.5000 (-6.0 dBFS)
RMS0.2887 (-10.8 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 24-35 .. code-block:: Python f0_yin = audio.yin(fmin=65.0, fmax=800.0)[0] f0_pyin, voiced, _ = audio.pyin(fmin=65.0, fmax=800.0) f0_pyin, voiced = f0_pyin[0], voiced[0] frames = np.arange(f0_yin.size) * 512 / sr fig, ax = plt.subplots(figsize=(7, 3.5)) ax.plot(frames, f0_yin, label="YIN", color="0.7") ax.plot(frames, f0_pyin, label="pYIN (voiced frames)", lw=2) ax.set(xlabel="Time (s)", ylabel="Frequency (Hz)", ylim=(0, 450), title="Pitch track") ax.legend() .. image-sg:: /gallery/pitch/images/sphx_glr_plot_yin_and_pyin_001.png :alt: Pitch track :srcset: /gallery/pitch/images/sphx_glr_plot_yin_and_pyin_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-37 pYIN marks the trailing silence as unvoiced and finds 220 Hz in the tone. .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python assert not voiced[-5:].any() assert abs(np.nanmedian(f0_pyin) - 220.0) < 5.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.257 seconds) .. _sphx_glr_download_gallery_pitch_plot_yin_and_pyin.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_yin_and_pyin.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_yin_and_pyin.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_yin_and_pyin.zip `