{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Spectrogram, power spectral density\n",
    "\n",
    "Demo spectrogram and power spectral density on a frequency chirp.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate a chirp signal\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x11ee9a8a0>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 113596 characters]",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Seed the random number generator\n",
    "np.random.seed(0)\n",
    "\n",
    "time_step = 0.01\n",
    "time_vec = np.arange(0, 70, time_step)\n",
    "\n",
    "# A signal with a small frequency chirp\n",
    "sig = np.sin(0.5 * np.pi * time_vec * (1 + 0.1 * time_vec))\n",
    "\n",
    "plt.figure(figsize=(8, 5))\n",
    "plt.plot(time_vec, sig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compute and plot the spectrogram\n",
    "\n",
    " The spectrum of the signal on consecutive time windows\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28504 characters]",
      "text/plain": [
       "<Figure size 500x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import scipy as sp\n",
    "\n",
    "freqs, times, spectrogram = sp.signal.spectrogram(sig)\n",
    "\n",
    "plt.figure(figsize=(5, 4))\n",
    "plt.imshow(spectrogram, aspect=\"auto\", cmap=\"hot_r\", origin=\"lower\")\n",
    "plt.title(\"Spectrogram\")\n",
    "plt.ylabel(\"Frequency band\")\n",
    "plt.xlabel(\"Time window\")\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compute and plot the power spectral density (PSD)\n",
    "\n",
    " The power of the signal per frequency band\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22532 characters]",
      "text/plain": [
       "<Figure size 500x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "freqs, psd = sp.signal.welch(sig)\n",
    "\n",
    "plt.figure(figsize=(5, 4))\n",
    "plt.semilogx(freqs, psd)\n",
    "plt.title(\"PSD: power spectral density\")\n",
    "plt.xlabel(\"Frequency\")\n",
    "plt.ylabel(\"Power\")\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
