{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "bdd2aa18",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "5c5409cf",
            "cell_type": "markdown",
            "source": "For the first example, a linear chirp ranging from 6 Hz to 1 Hz over 10 seconds is\nplotted:\n",
            "metadata": {}
        },
        {
            "id": "89f93cf3",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom matplotlib.pyplot import tight_layout\nfrom scipy.signal import chirp, square, ShortTimeFFT\nfrom scipy.signal.windows import gaussian\nimport matplotlib.pyplot as plt\n\nN, T = 1000, 0.01  # number of samples and sampling interval for 10 s signal\nt = np.arange(N) * T  # timestamps\n\nx_lin = chirp(t, f0=6, f1=1, t1=10, method='linear')\n\nfg0, ax0 = plt.subplots()\nax0.set_title(r\"Linear Chirp from $f(0)=6\\,$Hz to $f(10)=1\\,$Hz\")\nax0.set(xlabel=\"Time $t$ in Seconds\", ylabel=r\"Amplitude $x_\\text{lin}(t)$\")\nax0.plot(t, x_lin)\nplt.show()",
            "outputs": []
        },
        {
            "id": "eb3684f2",
            "cell_type": "markdown",
            "source": "The following four plots each show the short-time Fourier transform of a chirp\nranging from 45 Hz to 5 Hz with different values for the parameter `method`\n(and `vertex_zero`):\n",
            "metadata": {}
        },
        {
            "id": "04b4ff42",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x_qu0 = chirp(t, f0=45, f1=5, t1=N*T, method='quadratic', vertex_zero=True)\nx_qu1 = chirp(t, f0=45, f1=5, t1=N*T, method='quadratic', vertex_zero=False)\nx_log = chirp(t, f0=45, f1=5, t1=N*T, method='logarithmic')\nx_hyp = chirp(t, f0=45, f1=5, t1=N*T, method='hyperbolic')\n\nwin = gaussian(50, std=12, sym=True)\nSFT = ShortTimeFFT(win, hop=2, fs=1/T, mfft=800, scale_to='magnitude')\nts = (\"'quadratic', vertex_zero=True\", \"'quadratic', vertex_zero=False\",\n      \"'logarithmic'\", \"'hyperbolic'\")\nfg1, ax1s = plt.subplots(2, 2, sharex='all', sharey='all',\n                         figsize=(6, 5),  layout=\"constrained\")\nfor x_, ax_, t_ in zip([x_qu0, x_qu1, x_log, x_hyp], ax1s.ravel(), ts):\n    aSx = abs(SFT.stft(x_))\n    im_ = ax_.imshow(aSx, origin='lower', aspect='auto', extent=SFT.extent(N),\n                     cmap='plasma')\n    ax_.set_title(t_)\n    if t_ == \"'hyperbolic'\":\n        fg1.colorbar(im_, ax=ax1s, label='Magnitude $|S_z(t,f)|$')\n_ = fg1.supxlabel(\"Time $t$ in Seconds\")  # `_ =` is needed to pass doctests\n_ = fg1.supylabel(\"Frequency $f$ in Hertz\")\nplt.show()",
            "outputs": []
        },
        {
            "id": "dbab8395",
            "cell_type": "markdown",
            "source": "Finally, the short-time Fourier transform of a complex-valued linear chirp\nranging from -30 Hz to 30 Hz is depicted:\n",
            "metadata": {}
        },
        {
            "id": "907b8026",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "z_lin = chirp(t, f0=-30, f1=30, t1=N*T, method=\"linear\", complex=True)\nSFT.fft_mode = 'centered'  # needed to work with complex signals\naSz = abs(SFT.stft(z_lin))\n\nfg2, ax2 = plt.subplots()\nax2.set_title(r\"Linear Chirp from $-30\\,$Hz to $30\\,$Hz\")\nax2.set(xlabel=\"Time $t$ in Seconds\", ylabel=\"Frequency $f$ in Hertz\")\nim2 = ax2.imshow(aSz, origin='lower', aspect='auto',\n                 extent=SFT.extent(N), cmap='viridis')\nfg2.colorbar(im2, label='Magnitude $|S_z(t,f)|$')\nplt.show()",
            "outputs": []
        },
        {
            "id": "19010178",
            "cell_type": "markdown",
            "source": "Note that using negative frequencies makes only sense with complex-valued signals.\nFurthermore, the magnitude of the complex exponential function is one whereas the\nmagnitude of the real-valued cosine function is only 1/2.\n",
            "metadata": {}
        }
    ]
}