code: def _spectral_helper(
      x,
      y=None,
      NFFT=None,
      Fs=None,
      detrend_func=None,
      window=None,
      noverlap=None,
      pad_to=None,
      sides=None,
      scale_by_freq=None,
      mode=None,
  ):
      """
      Private helper implementing the common parts between the psd, csd,
      spectrogram and complex, magnitude, angle, and phase spectrums.
      """
      if y is None:
          # if y is None use x for y
          same_data = True
      else:
          # The checks for if y is x are so that we can use the same function to
          # implement the core of psd(), csd(), and spectrogram() without doing
          # extra calculations.  We return the unaveraged Pxy, freqs, and t.
          same_data = y is x
  
      if Fs is None:
          Fs = 2
      if noverlap is None:
          noverlap = 0
      if detrend_func is None:
          detrend_func = detrend_none
      if window is None:
          window = window_hanning
  
      # if NFFT is set to None use the whole signal
      if NFFT is None:
          NFFT = 256
  
      if noverlap >= NFFT:
          raise ValueError("noverlap must be less than NFFT")
  
      if mode is None or mode == "default":
          mode = "psd"
      _api.check_in_list(
          ["default", "psd", "complex", "magnitude", "angle", "phase"],
          mode=mode,
      )
  
      if not same_data and mode != "psd":
          raise ValueError("x and y must be equal if mode is not 'psd'")
  
      # Make sure we're dealing with a numpy array. If y and x were the same
      # object to start with, keep them that way
      x = np.asarray(x)
      if not same_data:
          y = np.asarray(y)
  
      if sides is None or sides == "default":
          if np.iscomplexobj(x):
              sides = "twosided"
          else:
              sides = "onesided"
      _api.check_in_list(
          ["default", "onesided", "twosided"], sides=sides
      )
  
      # zero pad x and y up to NFFT if they are shorter than NFFT
      if len(x) < NFFT:
          n = len(x)
          x = np.resize(x, NFFT)
          x[n:] = 0
  
      if not same_data and len(y) < NFFT:
          n = len(y)
          y = np.resize(y, NFFT)
          y[n:] = 0
  
      if pad_to is None:
          pad_to = NFFT
  
      if mode != "psd":
          scale_by_freq = False
      elif scale_by_freq is None:
          scale_by_freq = True
  
      # For real x, ignore the negative frequencies unless told otherwise
      if sides == "twosided":
          numFreqs = pad_to
          if pad_to % 2:
              freqcenter = (pad_to - 1) // 2 + 1
          else:
              freqcenter = pad_to // 2
          scaling_factor = 1.0
      elif sides == "onesided":
          if pad_to % 2:
              numFreqs = (pad_to + 1) // 2
          else:
              numFreqs = pad_to // 2 + 1
          scaling_factor = 2.0
  
      if not np.iterable(window):
          window = window(np.ones(NFFT, x.dtype))
      if len(window) != NFFT:
          raise ValueError(
              "The window length must match the data's first dimension"
          )
  
      result = np.lib.stride_tricks.sliding_window_view(
          x, NFFT, axis=0
      )[:: NFFT - noverlap].T
      result = detrend(result, detrend_func, axis=0)
      result = result * window.reshape((-1, 1))
      result = np.fft.fft(result, n=pad_to, axis=0)[:numFreqs, :]
      freqs = np.fft.fftfreq(pad_to, 1 / Fs)[:numFreqs]
  
      if not same_data:
          # if same_data is False, mode must be 'psd'
          resultY = np.lib.stride_tricks.sliding_window_view(
              y, NFFT, axis=0
          )[:: NFFT - noverlap].T
          resultY = detrend(resultY, detrend_func, axis=0)
          resultY = resultY * window.reshape((-1, 1))
          resultY = np.fft.fft(resultY, n=pad_to, axis=0)[:numFreqs, :]
          result = np.conj(result) * resultY
      elif mode == "psd":
          result = np.conj(result) * result
      elif mode == "magnitude":
          result = np.abs(result) / window.sum()
      elif mode == "angle" or mode == "phase":
          # we unwrap the phase later to handle the onesided vs. twosided case
          result = np.angle(result)
      elif mode == "complex":
          result /= window.sum()
  
      if mode == "psd":
          # Also include scaling factors for one-sided densities and dividing by
          # the sampling frequency, if desired. Scale everything, except the DC
          # component and the NFFT/2 component:
  
          # if we have a even number of frequencies, don't scale NFFT/2
          if not NFFT % 2:
              slc = slice(1, -1, None)
          # if we have an odd number, just don't scale DC
          else:
              slc = slice(1, None, None)
  
          result[slc] *= scaling_factor
  
          # MATLAB divides by the sampling frequency so that density function
          # has units of dB/Hz and can be integrated by the plotted frequency
          # values. Perform the same scaling here.
          if scale_by_freq:
              result /= Fs
              # Scale the spectrum by the norm of the window to compensate for
              # windowing loss; see Bendat & Piersol Sec 11.5.2.
              result /= (window**2).sum()
          else:
              # In this case, preserve power in the segment, not amplitude
              result /= window.sum() ** 2
  
      t = (
          np.arange(NFFT / 2, len(x) - NFFT / 2 + 1, NFFT - noverlap)
          / Fs
      )
  
      if sides == "twosided":
          # center the frequency range at zero
          freqs = np.roll(freqs, -freqcenter, axis=0)
          result = np.roll(result, -freqcenter, axis=0)
      elif not pad_to % 2:
          # get the last value correctly, it is negative otherwise
          freqs[-1] *= -1
  
      # we unwrap the phase here to handle the onesided vs. twosided case
      if mode == "phase":
          result = np.unwrap(result, axis=0)
  
      return result, freqs, t
  
imports: import functools
  from numbers import Number
  import numpy
  from matplotlib import _api, _docstring, cbook
label_desc: Use image node_id 7 for calling a global function with example usage: _spectral_helper(x, y, NFFT, Fs, detrend_func, window, noverlap, pad_to, sides, scale_by_freq, mode) and returns: result,  freqs,  t