{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Spectrum representations\n\nThe plots show different spectrum representations of a sine signal with\nadditive noise. A (frequency) spectrum of a discrete-time signal is calculated\nby utilizing the fast Fourier transform (FFT).\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nnp.random.seed(0)\n\ndt = 0.01  # sampling interval\nFs = 1 / dt  # sampling frequency\nt = np.arange(0, 10, dt)\n\n# generate noise:\nnse = np.random.randn(len(t))\nr = np.exp(-t / 0.05)\ncnse = np.convolve(nse, r) * dt\ncnse = cnse[:len(t)]\n\ns = 0.1 * np.sin(4 * np.pi * t) + cnse  # the signal\n\nfig = plt.figure(figsize=(7, 7), layout='constrained')\naxs = fig.subplot_mosaic([[\"signal\", \"signal\"],\n                          [\"magnitude\", \"log_magnitude\"],\n                          [\"phase\", \"angle\"]])\n\n# plot time signal:\naxs[\"signal\"].set_title(\"Signal\")\naxs[\"signal\"].plot(t, s, color='C0')\naxs[\"signal\"].set_xlabel(\"Time (s)\")\naxs[\"signal\"].set_ylabel(\"Amplitude\")\n\n# plot different spectrum types:\naxs[\"magnitude\"].set_title(\"Magnitude Spectrum\")\naxs[\"magnitude\"].magnitude_spectrum(s, Fs=Fs, color='C1')\n\naxs[\"log_magnitude\"].set_title(\"Log. Magnitude Spectrum\")\naxs[\"log_magnitude\"].magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')\n\naxs[\"phase\"].set_title(\"Phase Spectrum \")\naxs[\"phase\"].phase_spectrum(s, Fs=Fs, color='C2')\n\naxs[\"angle\"].set_title(\"Angle Spectrum\")\naxs[\"angle\"].angle_spectrum(s, Fs=Fs, color='C2')\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. tags::\n\n   domain: signal-processing\n   plot-type: line\n   level: beginner\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}