code: def detrend(data, axis=-1, type="linear", bp=0, overwrite_data=False):
      """
      Remove linear trend along axis from data.
  
      Parameters
      ----------
      data : array_like
          The input data.
      axis : int, optional
          The axis along which to detrend the data. By default this is the
          last axis (-1).
      type : {'linear', 'constant'}, optional
          The type of detrending. If ``type == 'linear'`` (default),
          the result of a linear least-squares fit to `data` is subtracted
          from `data`.
          If ``type == 'constant'``, only the mean of `data` is subtracted.
      bp : array_like of ints, optional
          A sequence of break points. If given, an individual linear fit is
          performed for each part of `data` between two break points.
          Break points are specified as indices into `data`. This parameter
          only has an effect when ``type == 'linear'``.
      overwrite_data : bool, optional
          If True, perform in place detrending and avoid a copy. Default is False
  
      Returns
      -------
      ret : ndarray
          The detrended input data.
  
      Examples
      --------
      >>> from scipy import signal
      >>> from numpy.random import default_rng
      >>> rng = default_rng()
      >>> npoints = 1000
      >>> noise = rng.standard_normal(npoints)
      >>> x = 3 + 2*np.linspace(0, 1, npoints) + noise
      >>> (signal.detrend(x) - noise).max()
      0.06  # random
  
      """
      if type not in ["linear", "l", "constant", "c"]:
          raise ValueError("Trend type must be 'linear' or 'constant'.")
      data = np.asarray(data)
      dtype = data.dtype.char
      if dtype not in "dfDF":
          dtype = "d"
      if type in ["constant", "c"]:
          ret = data - np.mean(data, axis, keepdims=True)
          return ret
      else:
          dshape = data.shape
          N = dshape[axis]
          bp = np.sort(np.unique(np.r_[0, bp, N]))
          if np.any(bp > N):
              raise ValueError(
                  "Breakpoints must be less than length "
                  "of data along given axis."
              )
          Nreg = len(bp) - 1
          # Restructure data so that axis is along first dimension and
          #  all other dimensions are collapsed into second dimension
          rnk = len(dshape)
          if axis < 0:
              axis = axis + rnk
          newdims = np.r_[axis, 0:axis, axis + 1 : rnk]
          newdata = np.reshape(
              np.transpose(data, tuple(newdims)),
              (N, _prod(dshape) // N),
          )
          if not overwrite_data:
              newdata = newdata.copy()  # make sure we have a copy
          if newdata.dtype.char not in "dfDF":
              newdata = newdata.astype(dtype)
          # Find leastsq fit and remove it for each piece
          for m in range(Nreg):
              Npts = bp[m + 1] - bp[m]
              A = np.ones((Npts, 2), dtype)
              A[:, 0] = np.cast[dtype](
                  np.arange(1, Npts + 1) * 1.0 / Npts
              )
              sl = slice(bp[m], bp[m + 1])
              coef, resids, rank, s = linalg.lstsq(A, newdata[sl])
              newdata[sl] = newdata[sl] - np.dot(A, coef)
          # Put data back in original shape.
          tdshape = np.take(dshape, newdims, 0)
          ret = np.reshape(newdata, tuple(tdshape))
          vals = list(range(1, rnk))
          olddims = vals[:axis] + [0] + vals[axis:]
          ret = np.transpose(ret, tuple(olddims))
          return ret
  
imports: import operator
  import math
  import timeit
  from scipy.spatial import cKDTree
  from .None import sigtools, dlti
  from ._upfirdn import upfirdn, _output_len, _upfirdn_modes
  from scipy import linalg, fft
  from scipy.fft._helper import _init_nd_shape_and_axes
  from scipy._lib._util import prod
  import numpy
  from scipy.special import lambertw
  from .windows import get_window
  from ._arraytools import axis_slice, axis_reverse, odd_ext, even_ext, const_ext
  from .filter_design import cheby1, _validate_sos
  from .fir_filter_design import firwin
  from ._sosfilt import _sosfilt
  import warnings
label_desc: Use image node_id 44 for calling a global function with example usage: detrend(data, axis, type, bp, overwrite_data) and returns: ret, ret