power_spectral_density

power_spectral_density#

class braintools.metric.power_spectral_density(lfp, dt, nperseg=None, noverlap=None, freq_range=None)#

Estimate the one-sided power spectral density (PSD) using Welch’s method.

The signal is split into overlapping Hann-windowed segments; the periodogram of each segment is averaged. Power is normalized by fs * sum(window**2) and the one-sided spectrum doubles every bin except DC (and Nyquist for even-length segments), so the PSD integrates to the signal variance.

Parameters:
  • lfp (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – LFP signal with shape (n_time,) or (n_time, n_channels). brainunit.Quantity inputs are accepted (the magnitude is used).

  • dt (float | Quantity) – Sampling interval. If a float, it is taken to be in seconds (so fs is in Hz); if a Quantity, it is converted to seconds.

  • nperseg (int | None) – Length of each segment. Default: n_time // 8.

  • noverlap (int | None) – Number of points to overlap between segments. Default: nperseg // 2.

  • freq_range (Tuple[float, float] | None) – (f_min, f_max) in Hz to retain. If None, returns all frequencies.

Return type:

Tuple[Array, Array]

Returns:

  • freqs (jax.Array) – One-sided sample frequencies in Hz, shape (nperseg // 2 + 1,).

  • psd (jax.Array) – Power spectral density: shape (n_freqs,) for 1-D input or (n_freqs, n_channels) for 2-D input.

Notes

Supplying freq_range uses boolean-mask indexing (data-dependent output length), so that path is not jit-compatible; the full-spectrum path is.

The window is jnp.hanning, which is the symmetric Hann window; scipy.signal.welch uses a periodic (sym=False) Hann window by default, so PSD values differ marginally when comparing against scipy.