.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/mel_mfcc/plot_chroma.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_mel_mfcc_plot_chroma.py: Chroma ====== Energy per pitch class, folded across octaves. A chord shows up as its three notes whatever the octave. ``estimate_key`` uses the same idea. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import matplotlib.pyplot as plt import audio_samples as aus from audio_samples import utils .. GENERATED FROM PYTHON SOURCE LINES 15-16 A C major chord followed by an A minor chord. .. GENERATED FROM PYTHON SOURCE LINES 16-27 .. code-block:: Python sr = 22050 def chord(names, seconds): tones = [aus.generation.sine_wave(utils.note_to_frequency(n), seconds, sr).as_f32() for n in names] return aus.AudioSamples.mix(tones) audio = aus.AudioSamples.concatenate([chord(["C4", "E4", "G4"], 1.0), chord(["A3", "C4", "E4"], 1.0)]) audio .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.9995 (-0.0 dBFS)
RMS0.4082 (-7.8 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 28-29 ``chroma_stft`` returns ``(channels, 12, frames)``, row 0 being C. .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python chroma = audio.chroma_stft()[0] fig, ax = plt.subplots(figsize=(7, 3)) ax.imshow(chroma, origin="lower", aspect="auto", extent=[0, audio.duration_seconds, 0, 12]) ax.set_yticks([i + 0.5 for i in range(12)]) ax.set_yticklabels(["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]) ax.set(xlabel="Time (s)", title="Chroma") .. image-sg:: /gallery/mel_mfcc/images/sphx_glr_plot_chroma_001.png :alt: Chroma :srcset: /gallery/mel_mfcc/images/sphx_glr_plot_chroma_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [Text(0.5, 3.833333333333327, 'Time (s)'), Text(0.5, 1.0, 'Chroma')] .. GENERATED FROM PYTHON SOURCE LINES 38-39 The three strongest pitch classes of the first second are C, E and G. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python first = chroma[:, : chroma.shape[1] // 2 - 2].mean(axis=1) assert set(first.argsort()[-3:]) == {0, 4, 7} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.073 seconds) .. _sphx_glr_download_gallery_mel_mfcc_plot_chroma.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_chroma.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_chroma.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_chroma.zip `