v2.0.0
Loading...
Searching...
No Matches
morlet_tfr.cpp
Go to the documentation of this file.
1//=============================================================================================================
12
13//=============================================================================================================
14// INCLUDES
15//=============================================================================================================
16
17#include "morlet_tfr.h"
18
19//=============================================================================================================
20// EIGEN INCLUDES
21//=============================================================================================================
22
23#include <Eigen/Core>
24//#ifndef EIGEN_FFTW_DEFAULT
25//#define EIGEN_FFTW_DEFAULT
26//#endif
27#include <unsupported/Eigen/FFT>
28
29//=============================================================================================================
30// STD INCLUDES
31//=============================================================================================================
32
33#include <cmath>
34#include <complex>
35#include <vector>
36
37//=============================================================================================================
38// USED NAMESPACES
39//=============================================================================================================
40
41using namespace UTILSLIB;
42using namespace Eigen;
43
44namespace
45{
46constexpr double MORLET_PI = 3.14159265358979323846;
47}
48
49//=============================================================================================================
50// DEFINE MEMBER METHODS
51//=============================================================================================================
52
53int MorletTfr::nextPow2(int n)
54{
55 int p = 1;
56 while (p < n)
57 p <<= 1;
58 return p;
59}
60
61//=============================================================================================================
62
63VectorXcd MorletTfr::buildWavelet(double dFreq, double dSFreq, double dNCycles, int& halfLen)
64{
65 // Time-domain standard deviation: σ_t = nCycles / (2π·f)
66 const double sigma_t = dNCycles / (2.0 * MORLET_PI * dFreq);
67
68 // Truncate at ±4σ — captures > 99.99 % of energy
69 halfLen = static_cast<int>(std::round(4.0 * sigma_t * dSFreq));
70
71 const int nWav = 2 * halfLen + 1;
72 VectorXcd wavelet(nWav);
73
74 // L2-energy normalisation: A = (σ_t · √(2π))^(-0.5)
75 const double A = std::pow(sigma_t * std::sqrt(2.0 * MORLET_PI), -0.5);
76
77 for (int i = 0; i < nWav; ++i) {
78 const double t = static_cast<double>(i - halfLen) / dSFreq;
79 const double gauss = std::exp(-t * t / (2.0 * sigma_t * sigma_t));
80 const double phase = 2.0 * MORLET_PI * dFreq * t;
81 wavelet[i] = std::complex<double>(A * gauss * std::cos(phase),
82 A * gauss * std::sin(phase));
83 }
84 return wavelet;
85}
86
87//=============================================================================================================
88
89MorletTfrResult MorletTfr::compute(const RowVectorXd& vecData,
90 double dSFreq,
91 const RowVectorXd& vecFreqs,
92 double dNCycles)
93{
94 const int nTimes = static_cast<int>(vecData.cols());
95 const int nFreqs = static_cast<int>(vecFreqs.cols());
96
97 MorletTfrResult result;
98 result.matPower.resize(nFreqs, nTimes);
99 result.vecFreqs = vecFreqs;
100
101 // Pre-compute forward FFT of the (real) signal at the maximum needed convolution length.
102 // For each frequency the wavelet may differ in length; recompute convolution per frequency.
103 Eigen::FFT<double> fft;
104
105 for (int fi = 0; fi < nFreqs; ++fi) {
106 int halfLen = 0;
107 const VectorXcd wavelet = buildWavelet(vecFreqs[fi], dSFreq, dNCycles, halfLen);
108
109 const int nWav = static_cast<int>(wavelet.size());
110 const int nConv = nextPow2(nTimes + nWav - 1);
111
112 // --- FFT of zero-padded real signal ---
113 VectorXd sigPad = VectorXd::Zero(nConv);
114 sigPad.head(nTimes) = vecData.transpose();
115 VectorXcd sigSpec;
116 fft.fwd(sigSpec, sigPad);
117
118 // --- FFT of zero-padded complex wavelet (real & imag parts separately) ---
119 VectorXd wavReal = VectorXd::Zero(nConv);
120 VectorXd wavImag = VectorXd::Zero(nConv);
121 for (int k = 0; k < nWav; ++k) {
122 wavReal[k] = wavelet[k].real();
123 wavImag[k] = wavelet[k].imag();
124 }
125 VectorXcd wavSpecR, wavSpecI;
126 fft.fwd(wavSpecR, wavReal);
127 fft.fwd(wavSpecI, wavImag);
128 // Combine: FFT(real + i·imag) = FFT(real) + i·FFT(imag)
129 VectorXcd wavSpec = wavSpecR + std::complex<double>(0.0, 1.0) * wavSpecI;
130
131 // --- Multiply spectra and inverse FFT ---
132 VectorXcd product = sigSpec.array() * wavSpec.array();
133 VectorXcd conv;
134 fft.inv(conv, product);
135
136 // Trim to "same" length: skip the first halfLen samples (linear → same convolution)
137 for (int t = 0; t < nTimes; ++t)
138 result.matPower(fi, t) = std::norm(conv[t + halfLen]);
139 }
140
141 return result;
142}
143
144//=============================================================================================================
145
146QVector<MorletTfrResult> MorletTfr::computeMultiChannel(const MatrixXd& matData,
147 double dSFreq,
148 const RowVectorXd& vecFreqs,
149 double dNCycles,
150 const RowVectorXi& vecPicks)
151{
152 std::vector<int> picks;
153 if (vecPicks.size() > 0) {
154 picks.reserve(static_cast<std::size_t>(vecPicks.size()));
155 for (int i = 0; i < vecPicks.size(); ++i)
156 picks.push_back(vecPicks[i]);
157 } else {
158 picks.reserve(static_cast<std::size_t>(matData.rows()));
159 for (int i = 0; i < static_cast<int>(matData.rows()); ++i)
160 picks.push_back(i);
161 }
162
163 QVector<MorletTfrResult> results;
164 results.reserve(static_cast<int>(picks.size()));
165 for (int ch : picks)
166 results.append(compute(matData.row(ch), dSFreq, vecFreqs, dNCycles));
167 return results;
168}
Complex Morlet wavelet time-frequency representation (TFR).
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Result of a Morlet TFR computation for one channel.
Definition morlet_tfr.h:64
Eigen::RowVectorXd vecFreqs
Centre frequencies in Hz, length n_freqs.
Definition morlet_tfr.h:66
Eigen::MatrixXd matPower
n_freqs × n_times, instantaneous power (amplitude²)
Definition morlet_tfr.h:65
static MorletTfrResult compute(const Eigen::RowVectorXd &vecData, double dSFreq, const Eigen::RowVectorXd &vecFreqs, double dNCycles=7.0)
static QVector< MorletTfrResult > computeMultiChannel(const Eigen::MatrixXd &matData, double dSFreq, const Eigen::RowVectorXd &vecFreqs, double dNCycles=7.0, const Eigen::RowVectorXi &vecPicks=Eigen::RowVectorXi())