.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/pitch/plot_harmonics.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_harmonics.py: Harmonics of a tone =================== ``detect_pitch_yin`` gives one number for a whole signal, and ``mpl.plot_harmonics_bar`` shows the level of each harmonic at that pitch. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python import audio_samples as aus from audio_samples import mpl .. GENERATED FROM PYTHON SOURCE LINES 13-14 A sawtooth has every harmonic, each weaker than the one before. .. GENERATED FROM PYTHON SOURCE LINES 14-23 .. code-block:: Python sr = 22050 audio = aus.generation.sawtooth_wave_bandlimited(220.0, 1.0, sr).as_f32() f0 = audio.detect_pitch_yin(threshold=0.1, min_frequency=65.0, max_frequency=800.0) print(f"f0 = {f0:.1f} Hz") assert abs(f0 - 220.0) < 2.0 fig, ax = mpl.plot_harmonics_bar(audio, fundamental_freq=f0, num_harmonics=10) .. image-sg:: /gallery/pitch/images/sphx_glr_plot_harmonics_001.png :alt: Harmonic Magnitudes :srcset: /gallery/pitch/images/sphx_glr_plot_harmonics_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none f0 = 220.1 Hz .. GENERATED FROM PYTHON SOURCE LINES 24-25 .. code-block:: Python audio .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration1.000 s (22050 samples per channel)
peak1.1593 (1.3 dBFS)
RMS0.5739 (-4.8 dBFS)

Player clips samples outside [-1, 1].



.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.168 seconds) .. _sphx_glr_download_gallery_pitch_plot_harmonics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_harmonics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_harmonics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_harmonics.zip `