{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "4d10bfaa",
            "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": "05903367",
            "cell_type": "markdown",
            "source": "Plot the phase-normalized frequency response, showing the relationship\nto the Butterworth's cutoff frequency (green):\n",
            "metadata": {}
        },
        {
            "id": "5ad172fa",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import signal\nimport matplotlib.pyplot as plt\nimport numpy as np",
            "outputs": []
        },
        {
            "id": "7c94d5aa",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b, a = signal.butter(4, 100, 'low', analog=True)\nw, h = signal.freqs(b, a)\nplt.semilogx(w, 20 * np.log10(np.abs(h)), color='silver', ls='dashed')\nb, a = signal.bessel(4, 100, 'low', analog=True, norm='phase')\nw, h = signal.freqs(b, a)\nplt.semilogx(w, 20 * np.log10(np.abs(h)))\nplt.title('Bessel filter magnitude response (with Butterworth)')\nplt.xlabel('Frequency [rad/s]')\nplt.ylabel('Amplitude [dB]')\nplt.margins(0, 0.1)\nplt.grid(which='both', axis='both')\nplt.axvline(100, color='green')  # cutoff frequency\nplt.show()",
            "outputs": []
        },
        {
            "id": "0c12f7d5",
            "cell_type": "markdown",
            "source": "and the phase midpoint:\n",
            "metadata": {}
        },
        {
            "id": "900e6ab8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.figure()\nplt.semilogx(w, np.unwrap(np.angle(h)))\nplt.axvline(100, color='green')  # cutoff frequency\nplt.axhline(-np.pi, color='red')  # phase midpoint\nplt.title('Bessel filter phase response')\nplt.xlabel('Frequency [rad/s]')\nplt.ylabel('Phase [rad]')\nplt.margins(0, 0.1)\nplt.grid(which='both', axis='both')\nplt.show()",
            "outputs": []
        },
        {
            "id": "646c82c2",
            "cell_type": "markdown",
            "source": "Plot the magnitude-normalized frequency response, showing the -3 dB cutoff:\n",
            "metadata": {}
        },
        {
            "id": "0a995f7b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b, a = signal.bessel(3, 10, 'low', analog=True, norm='mag')\nw, h = signal.freqs(b, a)\nplt.semilogx(w, 20 * np.log10(np.abs(h)))\nplt.axhline(-3, color='red')  # -3 dB magnitude\nplt.axvline(10, color='green')  # cutoff frequency\nplt.title('Amplitude-normalized Bessel filter frequency response')\nplt.xlabel('Frequency [rad/s]')\nplt.ylabel('Amplitude [dB]')\nplt.margins(0, 0.1)\nplt.grid(which='both', axis='both')\nplt.show()",
            "outputs": []
        },
        {
            "id": "608d577b",
            "cell_type": "markdown",
            "source": "Plot the delay-normalized filter, showing the maximally-flat group delay\nat 0.1 seconds:\n",
            "metadata": {}
        },
        {
            "id": "cb83f1a2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b, a = signal.bessel(5, 1/0.1, 'low', analog=True, norm='delay')\nw, h = signal.freqs(b, a)\nplt.figure()\nplt.semilogx(w[1:], -np.diff(np.unwrap(np.angle(h)))/np.diff(w))\nplt.axhline(0.1, color='red')  # 0.1 seconds group delay\nplt.title('Bessel filter group delay')\nplt.xlabel('Frequency [rad/s]')\nplt.ylabel('Group delay [s]')\nplt.margins(0, 0.1)\nplt.grid(which='both', axis='both')\nplt.show()",
            "outputs": []
        }
    ]
}