code: def power_in_band(events, dat, s_freq, frequency):
      
      dat = diff(dat)  # remove 1/f
  
      pw = empty(events.shape[0])
      pw.fill(nan)
  
      for i, one_event in enumerate(events):
  
          x0 = one_event[0]
          x1 = one_event[2]
  
          if x0 < 0 or x1 >= len(dat):
              pw[i] = nan
          else:
              sf, Pxx = periodogram(dat[x0:x1], s_freq)
              # find nearest frequencies in sf
              b0 = asarray([abs(x - frequency[0]) for x in sf]).argmin()
              b1 = asarray([abs(x - frequency[1]) for x in sf]).argmin()
              pw[i] = mean(Pxx[b0:b1])
  
      return pw
docs: Define power of the signal within frequency band.
  
      Parameters
      ----------
      events : ndarray (dtype='int')
          N x 3 matrix with start, peak, end samples
      dat : ndarray (dtype='float')
          vector with the original data
      s_freq : float
          sampling frequency
      frequency : tuple of float
          low and high frequency of spindle band, for window
  
      Returns
      -------
      ndarray (dtype='float')
          vector with power