Spectral
Namespace: UTILSLIB · Library: Utilities Library · Source: math/spectral.h
#include <utils/spectral.h>
class UTILSLIB::Spectral
Static multi-taper spectral estimator producing tapered spectra, PSDs and cross-spectral density matrices over Eigen row inputs.
Designed for fan-out across channels via QtConcurrent and feeding CONNECTIVITYLIB's coherence / phase estimators.
Static multi-taper spectra, PSD and CSD estimator for MEG/EEG time series.
const int nSamples = 200;
const double sFreq = 100.0;
RowVectorXd signal(nSamples);
for (int t = 0; t < nSamples; ++t) {
signal(t) = 2.0 * std::sin(2.0 * kPi * 10.0 * t / sFreq); // 10 Hz, amplitude 2
}
const auto [tapers, weights] = Spectral::generateTapers(nSamples, std::string("hanning"));
const MatrixXcd spectra = Spectral::computeTaperedSpectraRow(signal, tapers, nSamples);
const RowVectorXd psd = Spectral::psdFromTaperedSpectra(spectra, weights, nSamples, sFreq);
const VectorXd freqs = Spectral::calculateFFTFreqs(nSamples, sFreq);
Public Methods
Spectral()
deleted default constructor (static class).
Static Methods
computeTaperedSpectraRow(vecData, matTaper, iNfft)
Calculates the full tapered spectra of a given input row data.
Parameters:
-
vecData : const Eigen::RowVectorXd & input roww data (time domain), for which the spectrum is computed.
-
matTaper : const Eigen::MatrixXd & tapers used to compute the spectra.
-
iNfft : int FFT length.
Returns:
- Eigen::MatrixXcd — tapered spectra of the input data.
computeTaperedSpectraMatrix(matData, matTaper, iNfft, bUseThreads)
Calculates the full tapered spectra of a given input matrix data.
This function calculates each row in parallel.
Parameters:
-
matData : const Eigen::MatrixXd & input matrix data (time domain), for which the spectrum is computed.
-
matTaper : const Eigen::MatrixXd & tapers used to compute the spectra.
-
iNfft : int FFT length.
-
bUseThreads : bool Whether to use multiple threads.
Returns:
- QVector< Eigen::MatrixXcd > — tapered spectra of the input data.
compute(inputData)
Computes the tapered spectra for a row vector.
This function gets called in parallel.
Parameters:
- inputData : const TaperedSpectraInputData & The input data.
Returns:
- Eigen::MatrixXcd — The tapered spectra for one data row.
reduce(finalData, resultData)
Reduces the taperedSpectra results to a final result.
This function gets called in parallel.
Parameters:
-
finalData : QVector< Eigen::MatrixXcd > & The final data data.
-
resultData : const Eigen::MatrixXcd & The resulting data from the computation step.
psdFromTaperedSpectra(matTapSpectrum, vecTapWeights, iNfft, dSampFreq)
Calculates the power spectral density of given tapered spectrum.
Parameters:
-
matTapSpectrum : const Eigen::MatrixXcd & tapered spectrum, for which the PSD is calculated.
-
vecTapWeights : const Eigen::VectorXd & taper weights.
-
iNfft : int FFT length.
-
dSampFreq : double sampling frequency of the input data.
Returns:
- Eigen::RowVectorXd — power spectral density of a given tapered spectrum.
csdFromTaperedSpectra(vecTapSpectrumSeed, vecTapSpectrumTarget, vecTapWeightsSeed, vecTapWeightsTarget, iNfft, dSampFreq)
Calculates the cross-spectral density of the tapered spectra of seed and target.
Parameters:
-
vecTapSpectrumSeed : const Eigen::MatrixXcd & tapered spectrum of the seed.
-
vecTapSpectrumTarget : const Eigen::MatrixXcd & tapered spectrum of the target.
-
vecTapWeightsSeed : const Eigen::VectorXd & taper weights of the seed.
-
vecTapWeightsTarget : const Eigen::VectorXd & taper weights of the target.
-
iNfft : int FFT length.
-
dSampFreq : double sampling frequency of the input data.
Returns:
- Eigen::RowVectorXcd — cross-spectral density of the tapered spectra of seed and target.
calculateFFTFreqs(iNfft, dSampFreq)
Calculates the FFT frequencies.
Parameters:
-
iNfft : int FFT length.
-
dSampFreq : double sampling frequency of the input data.
Returns:
- Eigen::VectorXd — FFT frequencies.
generateTapers(iSignalLength, sWindowType)
Calculates a hanning window of given length.
Parameters:
-
iSignalLength : int length of the hanning window.
-
sWindowType : const QString & type of the window function used to compute tapered spectra.
Returns:
- QPair< Eigen::MatrixXd, Eigen::VectorXd > — Qpair of tapers and taper weights.
generateTapers(iSignalLength, sWindowType)
Calculates a hanning window of given length.
Parameters:
-
iSignalLength : int length of the hanning window.
-
sWindowType : const std::string & type of the window function used to compute tapered spectra.
Returns:
- std::pair< Eigen::MatrixXd, Eigen::VectorXd > — Qpair of tapers and taper weights.
Example
- Start here: ex_math — Math library on inputs with known answers.
- Full source:
src/examples/ex_math/main.cpp - Build target:
ex_math(runs under CTest)
Used in examples
ex_spectral
Multitaper spectra, power spectral density and cross-spectral density of random test signals.
//Generate hanning window
QPair<MatrixXd, VectorXd> tapers = Spectral::generateTapers(iNSamples, QString("hanning"));
MatrixXd matTaps = tapers.first;
VectorXd vecTapWeights = tapers.second;
int iNfft = iNSamples;
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>