Skip to main content

WelchPsd

Namespace: RTPROCESSINGLIB  ·  Library: DSP Library

Python equivalent

mne.time_frequency.psd_welch in MNE-Python.

#include <dsp/welch_psd.h>

class UTILSLIB::WelchPsd

Welch's averaged-periodogram power spectral density estimator.

Divides each channel into overlapping windowed segments, computes the FFT of each segment, and averages the squared magnitudes. The result is a one-sided PSD normalised so that integrating over frequency recovers the mean signal power.

src/examples/ex_dsp_analysis/main.cpp
const WelchPsdResult welch = WelchPsd::compute(tones, sFreq, 250, 0.5, WelchPsd::Hann); // 1 Hz bins, 50 % overlap
// welch.matPsd: n_channels x 126 one-sided PSD (unit^2/Hz); welch.vecFreqs: 0 ... 125 Hz

Static Methods​

compute(matData, dSFreq, iNfft, dOverlap, window, vecPicks, bRemoveDc)​

Compute Welch PSD for every (selected) channel of a data matrix.

Parameters:

  • matData : const Eigen::MatrixXd & Data matrix (n_channels × n_samples).

  • dSFreq : double Sampling frequency in Hz.

  • iNfft : int FFT length / segment length in samples (default 256).

  • dOverlap : double Fractional overlap between adjacent segments, in [0, 1) (default 0.5).

  • window : WindowType Window function (default Hann).

  • vecPicks : const Eigen::RowVectorXi & Channel row indices to process; empty = all channels.

  • bRemoveDc : bool Subtract each segment's mean first (remove_dc of mne.time_frequency.psd_array_welch).

Returns:


computeVector(vecData, dSFreq, iNfft, dOverlap, window, bRemoveDc)​

Compute Welch PSD for a single channel row vector.

Parameters:

  • vecData : const Eigen::RowVectorXd & Single-channel data (1 × n_samples row vector).

  • dSFreq : double Sampling frequency in Hz.

  • iNfft : int FFT / segment length in samples.

  • dOverlap : double Fractional segment overlap in [0, 1).

  • window : WindowType Window function (periodic, as scipy's get_window).

  • bRemoveDc : bool Subtract each segment's mean first (remove_dc of mne.time_frequency.psd_array_welch).

Returns:

  • Eigen::RowVectorXd — One-sided PSD row vector of length iNfft/2+1.

freqAxis(iNfft, dSFreq)​

Build the frequency axis for a given FFT length and sampling frequency.

Parameters:

  • iNfft : int FFT length.

  • dSFreq : double Sampling frequency in Hz.

Returns:

  • Eigen::RowVectorXd — Row vector of length iNfft/2+1 with frequencies in Hz.

Example​

Used in examples​

ex_dsp_analysis​

DSP library: Welch PSD, FIR filtering, peak finding, envelope correlation, SPHARA and simulation.

src/examples/ex_dsp_analysis/main.cpp, line 107
const WelchPsdResult welch = WelchPsd::compute(tones, sFreq, 250, 0.5, WelchPsd::Hann); // 1 Hz bins, 50 % overlap
// welch.matPsd: n_channels x 126 one-sided PSD (unit^2/Hz); welch.vecFreqs: 0 ... 125 Hz
// mne.time_frequency.psd_array_welch(x, 250, n_fft=250, n_overlap=125, window="hann")
ok &= expect(welch.vecFreqs.size() == 126 && std::fabs(welch.matPsd(0, 10) - 0.3333333333333334) < 1e-9 && std::fabs(welch.matPsd(0, 40) - 0.08333333333333323) < 1e-9 && std::fabs(welch.matPsd.sum() - 0.6250000000000001) < 1e-9,
QString("WelchPsd: P(10 Hz) = %1, P(40 Hz) = %2 match mne psd_array_welch").arg(welch.matPsd(0, 10)).arg(welch.matPsd(0, 40)));

Authors of this file​