Skip to main content

Resample

Namespace: RTPROCESSINGLIB  ·  Library: DSP Library

Python equivalent

mne.filter.resample in MNE-Python.

#include <dsp/resample.h>

class UTILSLIB::Resample

Polyphase anti-aliased rational resampling for MEG/EEG data.

src/examples/ex_dsp_spectral/main.cpp
const RowVectorXd downsampled = Resample::resample(signal, 100.0, kSFreq); // 200 Hz -> 100 Hz

Static Methods​

resample(vecData, dNewSFreq, dOldSFreq, iNZeros)​

Resample a single-channel row vector from dOldSFreq to dNewSFreq.

The ratio is reduced to its lowest terms p/q via GCD, then a polyphase Hamming-windowed sinc filter is applied. The output length is ceil(nSamples · p / q).

Parameters:

  • vecData : const Eigen::RowVectorXd & Input row vector (calibrated, SI units).

  • dNewSFreq : double Target sampling frequency in Hz.

  • dOldSFreq : double Original sampling frequency in Hz.

  • iNZeros : int Number of sinc zero-crossings on each side of the kernel (default 10). Higher values give better stopband attenuation at the cost of speed.

Returns:

  • Eigen::RowVectorXd — Resampled row vector.

resampleMatrix(matData, dNewSFreq, dOldSFreq, vecPicks, iNZeros)​

Resample every row of a data matrix.

Parameters:

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

  • dNewSFreq : double Target sampling frequency in Hz.

  • dOldSFreq : double Original sampling frequency in Hz.

  • vecPicks : const Eigen::RowVectorXi & Optional channel indices to resample; if empty all rows are processed. Non-selected rows are returned at their original length — in practice always pass picks or leave empty for a uniform matrix result.

  • iNZeros : int Sinc zero-crossings per side (default 10).

Returns:

  • Eigen::MatrixXd — Resampled matrix (n_channels × n_new_samples).

Example​

Used in examples​

ex_dsp_spectral​

Spectral and filtering tools of the dsp library on signals with known content.

src/examples/ex_dsp_spectral/main.cpp, line 180
const RowVectorXd downsampled = Resample::resample(signal, 100.0, kSFreq); // 200 Hz -> 100 Hz
// 55 Hz is above the new Nyquist (50 Hz) and must be removed; the 20 Hz sine stays
RowVectorXd expected20(downsampled.size());
for (int t = 0; t < downsampled.size(); ++t) {
expected20(t) = std::sin(2.0 * kPi * 20.0 * t / 100.0);
}
const double resampleErr = (downsampled - expected20).segment(20, downsampled.size() - 40).cwiseAbs().maxCoeff();
ok &= expect(downsampled.size() == n / 2 && resampleErr < 0.05,
QString("resampled to 100 Hz: 20 Hz kept, 55 Hz removed (max error %1)").arg(resampleErr));

Authors of this file​