{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "05464a29",
            "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": "b7f3bf54",
            "cell_type": "markdown",
            "source": "Generate a noisy signal to be filtered:\n",
            "metadata": {}
        },
        {
            "id": "8128937e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import signal\nimport matplotlib.pyplot as plt\nrng = np.random.default_rng()\nt = np.linspace(-1, 1, 201)\nx = (np.sin(2*np.pi*0.75*t*(1-t) + 2.1) +\n     0.1*np.sin(2*np.pi*1.25*t + 1) +\n     0.18*np.cos(2*np.pi*3.85*t))\nxn = x + rng.standard_normal(len(t)) * 0.08",
            "outputs": []
        },
        {
            "id": "7c9c52c7",
            "cell_type": "markdown",
            "source": "Create an order 3 lowpass butterworth filter:\n",
            "metadata": {}
        },
        {
            "id": "214ac18b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b, a = signal.butter(3, 0.05)",
            "outputs": []
        },
        {
            "id": "44c0f88e",
            "cell_type": "markdown",
            "source": "Apply the filter to xn.  Use lfilter_zi to choose the initial condition of\nthe filter:\n",
            "metadata": {}
        },
        {
            "id": "6552a820",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "zi = signal.lfilter_zi(b, a)\nz, _ = signal.lfilter(b, a, xn, zi=zi*xn[0])",
            "outputs": []
        },
        {
            "id": "60adc3ba",
            "cell_type": "markdown",
            "source": "Apply the filter again, to have a result filtered at an order the same as\nfiltfilt:\n",
            "metadata": {}
        },
        {
            "id": "69008f73",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "z2, _ = signal.lfilter(b, a, z, zi=zi*z[0])",
            "outputs": []
        },
        {
            "id": "5ad3844f",
            "cell_type": "markdown",
            "source": "Use filtfilt to apply the filter:\n",
            "metadata": {}
        },
        {
            "id": "f2ca29ca",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "y = signal.filtfilt(b, a, xn)",
            "outputs": []
        },
        {
            "id": "dbd43dff",
            "cell_type": "markdown",
            "source": "Plot the original signal and the various filtered versions:\n",
            "metadata": {}
        },
        {
            "id": "016ab66a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.figure\nplt.plot(t, xn, 'b', alpha=0.75)\nplt.plot(t, z, 'r--', t, z2, 'r', t, y, 'k')\nplt.legend(('noisy signal', 'lfilter, once', 'lfilter, twice',\n            'filtfilt'), loc='best')\nplt.grid(True)\nplt.show()",
            "outputs": []
        }
    ]
}