Skip to content

Estimate PSD across NaN gaps

What it does

Computes a one-sided power spectral density from complete analysis windows without crossing preserved gaps. Each window contributes equally to the final average.

When to use it

Use it when a regularly sampled signal contains missing spans and you want a spectrum based on valid stretches, optionally reconstructing short gaps first.

Canonical ID: tools.nanpwelch

Inputs

Name Meaning Type Unit Requirements
signal Real signal vector or matrix with channels in columns and NaNs marking gaps. real vector or matrix a.u. NaN allowed; finite
window Positive integer rectangular-window length or exact finite window coefficients. real scalar or vector 1 no NaN; finite
overlap Number of overlapping samples between consecutive windows. integer scalar sample minimum: 0; no NaN; finite
nfft Positive integer DFT length; shorter transforms use modulo folding. integer scalar sample minimum: 1; no NaN; finite
sampling_frequency Finite positive sampling frequency in Hz. real scalar Hz greater than: 0; no NaN; finite

Parameters

Name Meaning Type Unit Default Requirements
max_gap Maximum internal NaN run to interpolate, in samples; zero disables interpolation. integer scalar sample 0 minimum: 0

Outputs

Name Meaning Type Unit
psd Mean window PSD, preserving matrix channel columns. real vector or matrix a.u.^2/Hz
frequencies Shared one-sided frequency grid in Hz. real vector Hz
window_psd Individual-window spectra in window order, grouped by channel for matrices. real vector a.u.^2/Hz

How it works

Boundary NaNs are trimmed. Eligible internal gaps may be linearly interpolated; all remaining gaps separate independent stretches. The window grid restarts in each stretch. Only complete windows contribute to the mean PSD. A scalar window chooses rectangular weights; a vector supplies the actual weights. No detrending is performed, so a constant signal retains its DC power.

Interpretation and limitations

PSD units are squared signal amplitude per Hz. Longer stretches contribute more if they provide more complete windows. Per-window spectra expose those contributions. Matrices keep channels in columns, including NaN PSD columns when a channel has no estimate. An FFT shorter than the window folds weighted samples modulo the FFT length rather than truncating them. Interpolation changes the analyzed signal and should be chosen for the intended analysis.

Implementations and technical resources

Python source | MATLAB source

Normative JSON | Validation cases