.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/spectrograms/plot_constant_q.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_spectrograms_plot_constant_q.py: Constant-Q transform ==================== A log-frequency transform with the same number of bins per octave, which suits music: a scale becomes evenly spaced steps. .. GENERATED FROM PYTHON SOURCE LINES 8-15 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import audio_samples as aus from audio_samples import spectral, utils .. GENERATED FROM PYTHON SOURCE LINES 16-17 A one-octave C major scale of harmonic notes, half a second per note. .. GENERATED FROM PYTHON SOURCE LINES 17-28 .. code-block:: Python sr = 22050 notes = ["C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5"] freqs = [utils.note_to_frequency(n) for n in notes] scale = aus.AudioSamples.concatenate( [ aus.generation.compound_tone([(f, 0.6), (2 * f, 0.3), (3 * f, 0.1)], 0.5, sr).as_f32() for f in freqs ] ) scale .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration4.000 s (88200 samples per channel)
peak0.8052 (-1.9 dBFS)
RMS0.4795 (-6.4 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 29-30 ``cqt`` returns ``(channels, bins, frames)``. .. GENERATED FROM PYTHON SOURCE LINES 30-38 .. code-block:: Python fmin = 65.41 c = scale.cqt(fmin=fmin, n_bins=60, bins_per_octave=12)[0] db = spectral.amplitude_to_db(np.abs(c), ref="max", top_db=60.0) fig, ax = plt.subplots(figsize=(7, 3.5)) ax.imshow(db, origin="lower", aspect="auto", extent=[0, scale.duration_seconds, 0, 60]) ax.set(xlabel="Time (s)", ylabel="Semitones above C2", title="Constant-Q transform (dB)") .. image-sg:: /gallery/spectrograms/images/sphx_glr_plot_constant_q_001.png :alt: Constant-Q transform (dB) :srcset: /gallery/spectrograms/images/sphx_glr_plot_constant_q_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [Text(0.5, 9.333333333333327, 'Time (s)'), Text(54.222222222222214, 0.5, 'Semitones above C2'), Text(0.5, 1.0, 'Constant-Q transform (dB)')] .. GENERATED FROM PYTHON SOURCE LINES 39-40 The strongest bin of the first note is C4, 24 semitones above C2. .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python first_note = np.abs(c[:, 5:15]).mean(axis=1) assert int(first_note.argmax()) == 24 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.092 seconds) .. _sphx_glr_download_gallery_spectrograms_plot_constant_q.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_constant_q.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_constant_q.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_constant_q.zip `