code: def peak_to_peak_period(t, f, amplitude_threshold=1E-2):
      
      if hasattr(t, 'unit'):
          t_unit = t.unit
          t = t.value
      else:
          t_unit = u.dimensionless_unscaled
  
      # find peaks
      max_ix = argrelmax(f, mode='wrap')[0]
      max_ix = max_ix[(max_ix != 0) & (max_ix != (len(f)-1))]
  
      # find troughs
      min_ix = argrelmin(f, mode='wrap')[0]
      min_ix = min_ix[(min_ix != 0) & (min_ix != (len(f)-1))]
  
      # neglect minor oscillations
      if abs(np.mean(f[max_ix]) - np.mean(f[min_ix])) < amplitude_threshold:
          return np.nan
  
      # compute mean peak-to-peak
      if len(max_ix) > 0:
          T_max = np.mean(t[max_ix[1:]] - t[max_ix[:-1]])
      else:
          T_max = np.nan
  
      # now compute mean trough-to-trough
      if len(min_ix) > 0:
          T_min = np.mean(t[min_ix[1:]] - t[min_ix[:-1]])
      else:
          T_min = np.nan
  
      # then take the mean of these two
      return np.mean([T_max, T_min]) * t_unit
docs: Estimate the period of the input time series by measuring the average
      peak-to-peak time.
  
      Parameters
      ----------
      t : array_like
          Time grid aligned with the input time series.
      f : array_like
          A periodic time series.
      amplitude_threshold : numeric (optional)
          A tolerance parameter. Fails if the mean amplitude of oscillations
          isn't larger than this tolerance.
  
      Returns
      -------
      period : float
          The mean peak-to-peak period.