.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "intro/scipy/auto_examples/plot_spectrogram.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_intro_scipy_auto_examples_plot_spectrogram.py: ====================================== Spectrogram, power spectral density ====================================== Demo spectrogram and power spectral density on a frequency chirp. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: default import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 13-15 Generate a chirp signal ########################################################### .. GENERATED FROM PYTHON SOURCE LINES 15-28 .. code-block:: default # Seed the random number generator np.random.seed(0) time_step = .01 time_vec = np.arange(0, 70, time_step) # A signal with a small frequency chirp sig = np.sin(0.5 * np.pi * time_vec * (1 + .1 * time_vec)) plt.figure(figsize=(8, 5)) plt.plot(time_vec, sig) .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_spectrogram_001.png :alt: plot spectrogram :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_spectrogram_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 29-33 Compute and plot the spectrogram ########################################################### The spectrum of the signal on consecutive time windows .. GENERATED FROM PYTHON SOURCE LINES 33-45 .. code-block:: default import scipy as sp freqs, times, spectrogram = sp.signal.spectrogram(sig) plt.figure(figsize=(5, 4)) plt.imshow(spectrogram, aspect='auto', cmap='hot_r', origin='lower') plt.title('Spectrogram') plt.ylabel('Frequency band') plt.xlabel('Time window') plt.tight_layout() .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_spectrogram_002.png :alt: Spectrogram :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_spectrogram_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-50 Compute and plot the power spectral density (PSD) ########################################################### The power of the signal per frequency band .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: default freqs, psd = sp.signal.welch(sig) plt.figure(figsize=(5, 4)) plt.semilogx(freqs, psd) plt.title('PSD: power spectral density') plt.xlabel('Frequency') plt.ylabel('Power') plt.tight_layout() .. image-sg:: /intro/scipy/auto_examples/images/sphx_glr_plot_spectrogram_003.png :alt: PSD: power spectral density :srcset: /intro/scipy/auto_examples/images/sphx_glr_plot_spectrogram_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.273 seconds) .. _sphx_glr_download_intro_scipy_auto_examples_plot_spectrogram.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_spectrogram.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_spectrogram.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_