{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "38ead255",
            "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": "084713fc",
            "cell_type": "markdown",
            "source": "The following example detrends the function $x(t) = \\sin(\\pi t) + 1/4$:\n",
            "metadata": {}
        },
        {
            "id": "ead5cd50",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.signal import detrend\n\nt = np.linspace(-0.5, 0.5, 21)\nx = np.sin(np.pi*t) + 1/4\n\nx_d_const = detrend(x, type='constant')\nx_d_linear = detrend(x, type='linear')\n\nfig1, ax1 = plt.subplots()\nax1.set_title(r\"Detrending $x(t)=\\sin(\\pi t) + 1/4$\")\nax1.set(xlabel=\"t\", ylabel=\"$x(t)$\", xlim=(t[0], t[-1]))\nax1.axhline(y=0, color='black', linewidth=.5)\nax1.axvline(x=0, color='black', linewidth=.5)\nax1.plot(t, x, 'C0.-',  label=\"No detrending\")\nax1.plot(t, x_d_const, 'C1x-', label=\"type='constant'\")\nax1.plot(t, x_d_linear, 'C2+-', label=\"type='linear'\")\nax1.legend()\nplt.show()",
            "outputs": []
        },
        {
            "id": "4c5a292e",
            "cell_type": "markdown",
            "source": "Alternatively, NumPy's `~numpy.polynomial.polynomial.Polynomial` can be used for\ndetrending as well:\n",
            "metadata": {}
        },
        {
            "id": "cbe6641d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "pp0 = np.polynomial.Polynomial.fit(t, x, deg=0)  # fit degree 0 polynomial\nnp.allclose(x_d_const, x - pp0(t))  # compare with constant detrend",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "97f02f83",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "pp1 = np.polynomial.Polynomial.fit(t, x, deg=1)  # fit degree 1 polynomial\nnp.allclose(x_d_linear, x - pp1(t))  # compare with linear detrend",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "ea77ba54",
            "cell_type": "markdown",
            "source": "Note that `~numpy.polynomial.polynomial.Polynomial` also allows fitting higher\ndegree polynomials. Consult its documentation on how to extract the polynomial\ncoefficients.\n",
            "metadata": {}
        }
    ]
}