code: def filtfilt(
      b,
      a,
      x,
      axis=-1,
      padtype="odd",
      padlen=None,
      method="pad",
      irlen=None,
  ):
      """
      Apply a digital filter forward and backward to a signal.
  
      This function applies a linear digital filter twice, once forward and
      once backwards.  The combined filter has zero phase and a filter order
      twice that of the original.
  
      The function provides options for handling the edges of the signal.
  
      The function `sosfiltfilt` (and filter design using ``output='sos'``)
      should be preferred over `filtfilt` for most filtering tasks, as
      second-order sections have fewer numerical problems.
  
      Parameters
      ----------
      b : (N,) array_like
          The numerator coefficient vector of the filter.
      a : (N,) array_like
          The denominator coefficient vector of the filter.  If ``a[0]``
          is not 1, then both `a` and `b` are normalized by ``a[0]``.
      x : array_like
          The array of data to be filtered.
      axis : int, optional
          The axis of `x` to which the filter is applied.
          Default is -1.
      padtype : str or None, optional
          Must be 'odd', 'even', 'constant', or None.  This determines the
          type of extension to use for the padded signal to which the filter
          is applied.  If `padtype` is None, no padding is used.  The default
          is 'odd'.
      padlen : int or None, optional
          The number of elements by which to extend `x` at both ends of
          `axis` before applying the filter.  This value must be less than
          ``x.shape[axis] - 1``.  ``padlen=0`` implies no padding.
          The default value is ``3 * max(len(a), len(b))``.
      method : str, optional
          Determines the method for handling the edges of the signal, either
          "pad" or "gust".  When `method` is "pad", the signal is padded; the
          type of padding is determined by `padtype` and `padlen`, and `irlen`
          is ignored.  When `method` is "gust", Gustafsson's method is used,
          and `padtype` and `padlen` are ignored.
      irlen : int or None, optional
          When `method` is "gust", `irlen` specifies the length of the
          impulse response of the filter.  If `irlen` is None, no part
          of the impulse response is ignored.  For a long signal, specifying
          `irlen` can significantly improve the performance of the filter.
  
      Returns
      -------
      y : ndarray
          The filtered output with the same shape as `x`.
  
      See Also
      --------
      sosfiltfilt, lfilter_zi, lfilter, lfiltic, savgol_filter, sosfilt
  
      Notes
      -----
      When `method` is "pad", the function pads the data along the given axis
      in one of three ways: odd, even or constant.  The odd and even extensions
      have the corresponding symmetry about the end point of the data.  The
      constant extension extends the data with the values at the end points. On
      both the forward and backward passes, the initial condition of the
      filter is found by using `lfilter_zi` and scaling it by the end point of
      the extended data.
  
      When `method` is "gust", Gustafsson's method [1]_ is used.  Initial
      conditions are chosen for the forward and backward passes so that the
      forward-backward filter gives the same result as the backward-forward
      filter.
  
      References
      ----------
      .. [1] F. Gustaffson, "Determining the initial states in forward-backward
             filtering", Transactions on Signal Processing, Vol. 46, pp. 988-992,
             1996.
      """
      b = cupy.atleast_1d(b)
      a = cupy.atleast_1d(a)
      x = cupy.asarray(x)
  
      if method not in {"pad", "gust"}:
          raise ValueError("method must be 'pad' or 'gust'.")
  
      const_dtype = cupy.dtype(a.dtype)
      if const_dtype.kind == "u":
          const_dtype = cupy.dtype(const_dtype.char.lower())
          a = a.astype(const_dtype)
  
      if method == "gust":
          y, z1, z2 = _filtfilt_gust(b, a, x, axis=axis, irlen=irlen)
          return y
  
      # method == "pad"
      edge, ext = _validate_pad(
          padtype, padlen, x, axis, ntaps=max(len(a), len(b))
      )
  
      # Get the steady state of the filter's step response.
      zi = lfilter_zi(b, a)
  
      # Reshape zi and create x0 so that zi*x0 broadcasts
      # to the correct value for the 'zi' keyword argument
      # to lfilter.
      zi_shape = [1] * x.ndim
      zi_shape[axis] = zi.size
      zi = cupy.reshape(zi, zi_shape)
      x0 = axis_slice(ext, stop=1, axis=axis)
  
      # Forward filter.
      (y, zf) = lfilter(b, a, ext, axis=axis, zi=zi * x0)
  
      # Backward filter.
      # Create y0 so zi*y0 broadcasts appropriately.
      y0 = axis_slice(y, start=-1, axis=axis)
      (y, zf) = lfilter(
          b, a, axis_reverse(y, axis=axis), axis=axis, zi=zi * y0
      )
  
      # Reverse y.
      y = axis_reverse(y, axis=axis)
  
      if edge > 0:
          # Slice the actual signal from the extended signal.
          y = axis_slice(y, start=edge, stop=-edge, axis=axis)
  
      return y
  
imports: import warnings
  import cupy
  from cupy._core import internal
  from cupy.linalg import lstsq
  import cupyx.scipy.fft
  from cupyx.scipy.ndimage import _util
  from cupyx.scipy.ndimage import _filters
  from cupyx.scipy.signal import _signaltools_core
  from cupyx.scipy.signal._arraytools import const_ext, even_ext, odd_ext, axis_reverse, axis_slice, axis_assign
  from cupyx.scipy.signal._iir_utils import apply_iir, apply_iir_sos, compute_correction_factors, compute_correction_factors_sos
label_desc: Use image node_id 21 for calling a global function with example usage: filtfilt(b, a, x, axis, padtype, padlen, method, irlen) and returns: y, y