

.. _sphx_glr_gallery_pylab_examples_specgram_demo.py:


=============
Specgram Demo
=============





.. image:: /gallery/pylab_examples/images/sphx_glr_specgram_demo_001.png
    :align: center





.. code-block:: python

    import matplotlib.pyplot as plt
    import numpy as np

    # Fixing random state for reproducibility
    np.random.seed(19680801)


    dt = 0.0005
    t = np.arange(0.0, 20.0, dt)
    s1 = np.sin(2*np.pi*100*t)
    s2 = 2*np.sin(2*np.pi*400*t)

    # create a transient "chirp"
    mask = np.where(np.logical_and(t > 10, t < 12), 1.0, 0.0)
    s2 = s2 * mask

    # add some noise into the mix
    nse = 0.01*np.random.random(size=len(t))

    x = s1 + s2 + nse  # the signal
    NFFT = 1024       # the length of the windowing segments
    Fs = int(1.0/dt)  # the sampling frequency

    # Pxx is the segments x freqs array of instantaneous power, freqs is
    # the frequency vector, bins are the centers of the time bins in which
    # the power is computed, and im is the matplotlib.image.AxesImage
    # instance

    ax1 = plt.subplot(211)
    plt.plot(t, x)
    plt.subplot(212, sharex=ax1)
    Pxx, freqs, bins, im = plt.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
    plt.show()

**Total running time of the script:** ( 0 minutes  0.070 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: specgram_demo.py <specgram_demo.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: specgram_demo.ipynb <specgram_demo.ipynb>`

.. rst-class:: sphx-glr-signature

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
