{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "db85f155",
            "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": "75c041b0",
            "cell_type": "markdown",
            "source": "Generate a sinusoid:\n",
            "metadata": {}
        },
        {
            "id": "a5befa5e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nf1, f2, fs = 8, 10, 200  # Hz\nt = np.linspace(0, 1, fs, endpoint=False)\nx = np.sin(2*np.pi*t*f2)\nimport matplotlib.pyplot as plt\nplt.plot(t, x)\nplt.axis([0, 1, -1.1, 1.1])\nplt.show()",
            "outputs": []
        },
        {
            "id": "cc375fa9",
            "cell_type": "markdown",
            "source": "Its discrete Fourier transform has all of its energy in a single frequency\nbin:\n",
            "metadata": {}
        },
        {
            "id": "bd635bd6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.fft import rfft, rfftfreq\nfrom scipy.signal import czt, czt_points\nplt.plot(rfftfreq(fs, 1/fs), abs(rfft(x)))\nplt.margins(0, 0.1)\nplt.show()",
            "outputs": []
        },
        {
            "id": "54792bb8",
            "cell_type": "markdown",
            "source": "However, if the sinusoid is logarithmically-decaying:\n",
            "metadata": {}
        },
        {
            "id": "79348fb6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.exp(-t*f1) * np.sin(2*np.pi*t*f2)\nplt.plot(t, x)\nplt.axis([0, 1, -1.1, 1.1])\nplt.show()",
            "outputs": []
        },
        {
            "id": "790c9a8d",
            "cell_type": "markdown",
            "source": "the DFT will have spectral leakage:\n",
            "metadata": {}
        },
        {
            "id": "1e2f6fab",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(rfftfreq(fs, 1/fs), abs(rfft(x)))\nplt.margins(0, 0.1)\nplt.show()",
            "outputs": []
        },
        {
            "id": "41afd5f2",
            "cell_type": "markdown",
            "source": "While the DFT always samples the z-transform around the unit circle, the\nchirp z-transform allows us to sample the Z-transform along any\nlogarithmic spiral, such as a circle with radius smaller than unity:\n",
            "metadata": {}
        },
        {
            "id": "afbec9ed",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "M = fs // 2  # Just positive frequencies, like rfft\na = np.exp(-f1/fs)  # Starting point of the circle, radius < 1\nw = np.exp(-1j*np.pi/M)  # \"Step size\" of circle\npoints = czt_points(M + 1, w, a)  # M + 1 to include Nyquist\nplt.plot(points.real, points.imag, '.')\nplt.gca().add_patch(plt.Circle((0,0), radius=1, fill=False, alpha=.3))\nplt.axis('equal'); plt.axis([-1.05, 1.05, -0.05, 1.05])\nplt.show()",
            "outputs": []
        },
        {
            "id": "c0c5736e",
            "cell_type": "markdown",
            "source": "With the correct radius, this transforms the decaying sinusoid (and others\nwith the same decay rate) without spectral leakage:\n",
            "metadata": {}
        },
        {
            "id": "42f596ac",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "z_vals = czt(x, M + 1, w, a)  # Include Nyquist for comparison to rfft\nfreqs = np.angle(points)*fs/(2*np.pi)  # angle = omega, radius = sigma\nplt.plot(freqs, abs(z_vals))\nplt.margins(0, 0.1)\nplt.show()",
            "outputs": []
        }
    ]
}