{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Detrending can be interpreted as subtracting a least squares fit polynomial: Setting the parameter "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "type"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to 'constant' corresponds to fitting a zeroth degree polynomial, 'linear' to a first degree polynomial. Consult the example below."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Strong",
              "__tag": 4048,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "Array API Standard Support"
                }
              ]
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "detrend",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "scipy.signal._signaltools:detrend"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "SCIPY_ARRAY_API=1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported."
            }
          ]
        },
        {
          "__type": "Code",
          "__tag": 4050,
          "value": "====================  ====================  ====================\nLibrary               CPU                   GPU\n====================  ====================  ====================\nNumPy                 ✅                     n/a                 \nCuPy                  n/a                   ✅                   \nPyTorch               ✅                     ⛔                   \nJAX                   ✅                     ✅                   \nDask                  ⚠️ computes graph     n/a                 \n====================  ====================  ====================",
          "execution_status": null
        },
        {
          "__type": "Blockquote",
          "__tag": 4059,
          "children": [
            {
              "__type": "Paragraph",
              "__tag": 4045,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "See "
                },
                {
                  "__type": "InlineRole",
                  "__tag": 4003,
                  "value": "dev-arrayapi",
                  "domain": null,
                  "role": "ref",
                  "inventory": null
                },
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": " for more information."
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "ret",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The detrended input data."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Remove linear or constant trend along axis from data."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "data",
              "annotation": "array_like",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The input data."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "axis",
              "annotation": "int, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The axis along which to detrend the data. By default this is the last axis (-1)."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "type",
              "annotation": "{'linear', 'constant'}, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The type of detrending. If "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "type == 'linear'"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " (default), the result of a linear least-squares fit to "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "data"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is subtracted from "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "data"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". If "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "type == 'constant'"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", only the mean of "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "data"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is subtracted."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "bp",
              "annotation": "array_like of ints, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "A sequence of break points. If given, an individual linear fit is performed for each part of "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "data"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " between two break points. Break points are specified as indices into "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "data"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". This parameter only has an effect when "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "type == 'linear'"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "overwrite_data: bool, optional",
              "annotation": "",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If True, allow in place detrending and avoid a copy. Default is False. In place modification applies only if "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "type == 'linear'"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "data"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is of the floating point dtype "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "float32"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "float64"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "complex64"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " or "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "complex128"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/signal/_signaltools.py",
  "item_line": 4178,
  "item_type": "function",
  "aliases": [
    "scipy.signal.detrend"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "The following example detrends the function :math:`x(t) = \\sin(\\pi t) + 1/4`:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.signal import detrend\nt = np.linspace(-0.5, 0.5, 21)\nx = np.sin(np.pi*t) + 1/4\nx_d_const = detrend(x, type='constant')\nx_d_linear = detrend(x, type='linear')\nfig1, ax1 = plt.subplots()\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax1.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()\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "plt.show()\n",
        "execution_status": "success"
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "1.17.1",
          "kind": "assets",
          "path": "fig-30d8827a8efe3897.png"
        }
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nAlternatively, NumPy's `~numpy.polynomial.polynomial.Polynomial` can be used for\ndetrending as well:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "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\npp1 = np.polynomial.Polynomial.fit(t, x, deg=1)  # fit degree 1 polynomial\nnp.allclose(x_d_linear, x - pp1(t))  # compare with linear detrend\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nNote that `~numpy.polynomial.polynomial.Polynomial` also allows fitting higher\ndegree polynomials. Consult its documentation on how to extract the polynomial\ncoefficients."
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "numpy.polynomial.polynomial.Polynomial.fit",
        "reference": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "current-module",
          "version": "current-version",
          "kind": "to-resolve",
          "path": "numpy.polynomial.polynomial.Polynomial.fit"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Create least squares fit polynomial."
            }
          ]
        }
      ],
      "type": "meth"
    }
  ],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "data",
        "annotation": "np.ndarray",
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "axis",
        "annotation": "int",
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "-1"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "type",
        "annotation": "Literal['linear', 'constant']",
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "linear"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "bp",
        "annotation": "ArrayLike | int",
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "0"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "overwrite_data",
        "annotation": "bool",
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "False"
      }
    ],
    "return_annotation": "np.ndarray",
    "target_name": "detrend"
  },
  "references": null,
  "qa": "scipy.signal._signaltools:detrend",
  "arbitrary": [],
  "local_refs": [
    "axis",
    "bp",
    "data",
    "optional",
    "overwrite_data: bool",
    "ret",
    "type"
  ]
}