
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/lines_bars_and_markers/spectrum_demo.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_lines_bars_and_markers_spectrum_demo.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_lines_bars_and_markers_spectrum_demo.py:


========================
Spectrum representations
========================

The plots show different spectrum representations of a sine signal with
additive noise. A (frequency) spectrum of a discrete-time signal is calculated
by utilizing the fast Fourier transform (FFT).

.. GENERATED FROM PYTHON SOURCE LINES 10-53

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    np.random.seed(0)

    dt = 0.01  # sampling interval
    Fs = 1 / dt  # sampling frequency
    t = np.arange(0, 10, dt)

    # generate noise:
    nse = np.random.randn(len(t))
    r = np.exp(-t / 0.05)
    cnse = np.convolve(nse, r) * dt
    cnse = cnse[:len(t)]

    s = 0.1 * np.sin(4 * np.pi * t) + cnse  # the signal

    fig = plt.figure(figsize=(7, 7), layout='constrained')
    axs = fig.subplot_mosaic([["signal", "signal"],
                              ["magnitude", "log_magnitude"],
                              ["phase", "angle"]])

    # plot time signal:
    axs["signal"].set_title("Signal")
    axs["signal"].plot(t, s, color='C0')
    axs["signal"].set_xlabel("Time (s)")
    axs["signal"].set_ylabel("Amplitude")

    # plot different spectrum types:
    axs["magnitude"].set_title("Magnitude Spectrum")
    axs["magnitude"].magnitude_spectrum(s, Fs=Fs, color='C1')

    axs["log_magnitude"].set_title("Log. Magnitude Spectrum")
    axs["log_magnitude"].magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')

    axs["phase"].set_title("Phase Spectrum ")
    axs["phase"].phase_spectrum(s, Fs=Fs, color='C2')

    axs["angle"].set_title("Angle Spectrum")
    axs["angle"].angle_spectrum(s, Fs=Fs, color='C2')

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_spectrum_demo_001.png
   :alt: Signal, Magnitude Spectrum, Log. Magnitude Spectrum, Phase Spectrum , Angle Spectrum
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_spectrum_demo_001.png, /gallery/lines_bars_and_markers/images/sphx_glr_spectrum_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 54-59

.. tags::

   domain: signal-processing
   plot-type: line
   level: beginner


.. _sphx_glr_download_gallery_lines_bars_and_markers_spectrum_demo.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

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

    .. container:: sphx-glr-download sphx-glr-download-python

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: spectrum_demo.zip <spectrum_demo.zip>`


.. only:: html

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

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
