v2.0.0
Loading...
Searching...
No Matches
multitaper_psd.cpp
Go to the documentation of this file.
1//=============================================================================================================
12
13//=============================================================================================================
14// INCLUDES
15//=============================================================================================================
16
17#include "multitaper_psd.h"
18#include "dpss.h"
19
20//=============================================================================================================
21// EIGEN INCLUDES
22//=============================================================================================================
23
24#include <Eigen/Core>
25//#ifndef EIGEN_FFTW_DEFAULT
26//#define EIGEN_FFTW_DEFAULT
27//#endif
28#include <unsupported/Eigen/FFT>
29
30//=============================================================================================================
31// STD INCLUDES
32//=============================================================================================================
33
34#include <cmath>
35
36//=============================================================================================================
37// USED NAMESPACES
38//=============================================================================================================
39
40using namespace UTILSLIB;
41using namespace Eigen;
42
43//=============================================================================================================
44// DEFINE MEMBER METHODS
45//=============================================================================================================
46
48 double sfreq,
49 double halfBandwidth,
50 int nTapers,
51 int nFft)
52{
53 const int nChannels = static_cast<int>(matData.rows());
54 const int nTimes = static_cast<int>(matData.cols());
55
56 if (nFft < 0)
57 nFft = nTimes;
58
59 const int nFreqs = nFft / 2 + 1;
60
61 // 1. Compute DPSS tapers
62 DpssResult dpss = Dpss::compute(nTimes, halfBandwidth, nTapers);
63 const int nTap = static_cast<int>(dpss.matTapers.rows());
64
65 // Compute eigenvalue weights for weighted averaging
66 double weightSum = 0.0;
67 for (int t = 0; t < nTap; ++t)
68 weightSum += dpss.vecEigenvalues[t];
69
71 result.matPsd = MatrixXd::Zero(nChannels, nFreqs);
72
73 // Build frequency axis
74 result.vecFreqs.resize(nFreqs);
75 for (int k = 0; k < nFreqs; ++k)
76 result.vecFreqs[k] = static_cast<double>(k) * sfreq / static_cast<double>(nFft);
77
78 Eigen::FFT<double> fft;
79
80 // 2. For each channel, apply each taper, FFT, accumulate weighted |X|^2
81 for (int ch = 0; ch < nChannels; ++ch) {
82 RowVectorXd psd = RowVectorXd::Zero(nFreqs);
83
84 for (int t = 0; t < nTap; ++t) {
85 // Apply taper element-wise
86 VectorXd tapered = matData.row(ch).transpose().array()
87 * dpss.matTapers.row(t).transpose().array();
88
89 // Zero-pad to nFft if needed
90 VectorXd padded = VectorXd::Zero(nFft);
91 const int copyLen = std::min(nTimes, nFft);
92 padded.head(copyLen) = tapered.head(copyLen);
93
94 // Forward FFT
95 VectorXcd spec;
96 fft.fwd(spec, padded);
97
98 // Accumulate weighted |X[k]|^2
99 const double w = dpss.vecEigenvalues[t];
100 for (int k = 0; k < nFreqs; ++k)
101 psd[k] += w * std::norm(spec[k]);
102 }
103
104 // Normalise: divide by (weightSum × sfreq)
105 const double dNorm = 1.0 / (weightSum * sfreq);
106 psd *= dNorm;
107
108 // One-sided: double all non-DC, non-Nyquist bins
109 for (int k = 1; k < nFreqs - 1; ++k)
110 psd[k] *= 2.0;
111
112 result.matPsd.row(ch) = psd;
113 }
114
115 return result;
116}
Thomson multitaper power-spectral-density estimator.
Discrete Prolate Spheroidal Sequences (Slepian tapers) for multitaper spectral estimation.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Result of a DPSS taper computation.
Definition dpss.h:54
Eigen::MatrixXd matTapers
nTapers × N, each row is a unit-norm taper
Definition dpss.h:55
Eigen::VectorXd vecEigenvalues
Concentration ratios, length nTapers.
Definition dpss.h:56
static DpssResult compute(int N, double halfBandwidth, int nTapers=-1)
Definition dpss.cpp:48
Result of a multitaper PSD computation.
Eigen::RowVectorXd vecFreqs
Frequency axis in Hz, length nFft/2+1.
Eigen::MatrixXd matPsd
n_channels × n_freqs; one-sided PSD in unit²/Hz
static MultitaperPsdResult compute(const Eigen::MatrixXd &matData, double sfreq, double halfBandwidth=4.0, int nTapers=-1, int nFft=-1)