v2.0.0
Loading...
Searching...
No Matches
partial_directed_coherence.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
40#include "mvar_model.h"
43#include "../network/network.h"
45
46//=============================================================================================================
47// QT INCLUDES
48//=============================================================================================================
49
50#include <QDebug>
51
52//=============================================================================================================
53// EIGEN INCLUDES
54//=============================================================================================================
55
56#include <Eigen/Dense>
57
58//=============================================================================================================
59// USED NAMESPACES
60//=============================================================================================================
61
62using namespace CONNLIB;
63using namespace Eigen;
64
65//=============================================================================================================
66// DEFINE GLOBAL METHODS
67//=============================================================================================================
68
69//=============================================================================================================
70// DEFINE MEMBER METHODS
71//=============================================================================================================
72
76
77//=============================================================================================================
78
80{
81 Network finalNetwork("PDC");
82
83 if(connectivitySettings.isEmpty()) {
84 qDebug() << "PartialDirectedCoherence::calculate - Input data is empty";
85 return finalNetwork;
86 }
87
88 finalNetwork.setSamplingFrequency(connectivitySettings.getSamplingFrequency());
89
90 // Average trial data for MVAR fitting
91 const int nTrials = connectivitySettings.size();
92 MatrixXd matDataAvg = connectivitySettings.at(0).matData;
93 for(int t = 1; t < nTrials; ++t) {
94 matDataAvg += connectivitySettings.at(t).matData;
95 }
96 matDataAvg /= static_cast<double>(nTrials);
97
98 const int nCh = static_cast<int>(matDataAvg.rows());
99 const int iNfft = connectivitySettings.getFFTSize();
100 const int iNFreqs = static_cast<int>(std::floor(iNfft / 2.0)) + 1;
101
102 finalNetwork.setFFTSize(iNFreqs);
103 finalNetwork.setUsedFreqBins(iNFreqs);
104
105 // Create nodes
106 RowVectorXf rowVert = RowVectorXf::Zero(3);
107 for(int i = 0; i < nCh; ++i) {
108 rowVert = RowVectorXf::Zero(3);
109 if(connectivitySettings.getNodePositions().rows() != 0 && i < connectivitySettings.getNodePositions().rows()) {
110 rowVert(0) = connectivitySettings.getNodePositions().row(i)(0);
111 rowVert(1) = connectivitySettings.getNodePositions().row(i)(1);
112 rowVert(2) = connectivitySettings.getNodePositions().row(i)(2);
113 }
114 finalNetwork.append(NetworkNode::SPtr(new NetworkNode(i, rowVert)));
115 }
116
117 // Fit MVAR model
118 MvarModel model;
119 model.fit(matDataAvg);
120
121 // Compute A(f) = I - sum_{k=1}^{p} A_k * exp(-2*pi*i*f*k) at normalized frequencies
122 VectorXd vecFreqs = VectorXd::LinSpaced(iNFreqs, 0.0, 0.5);
123 QVector<MatrixXd> coeffs = model.coefficients();
124 int p = model.order();
125
126 const MatrixXcd matI = MatrixXcd::Identity(nCh, nCh);
127 const std::complex<double> jImag(0.0, 1.0);
128
129 // Compute PDC: PDC_{ij}(f) = |A_{ij}(f)| / sqrt(sum_k |A_{kj}(f)|^2)
130 // (normalized per column j)
131 for(int i = 0; i < nCh; ++i) {
132 for(int j = 0; j < nCh; ++j) {
133 MatrixXd matWeight(iNFreqs, 1);
134
135 for(int fi = 0; fi < iNFreqs; ++fi) {
136 // Compute A(f) at this frequency
137 MatrixXcd matAf = matI;
138 for(int k = 0; k < p; ++k) {
139 const double phase = -2.0 * M_PI * vecFreqs(fi) * (k + 1);
140 matAf -= coeffs[k].cast<std::complex<double>>() * std::exp(jImag * phase);
141 }
142
143 // Column normalization: sqrt(sum_k |A_{kj}(f)|^2)
144 double colNorm = 0.0;
145 for(int k = 0; k < nCh; ++k) {
146 colNorm += std::norm(matAf(k, j));
147 }
148 colNorm = std::sqrt(colNorm);
149
150 if(colNorm > 0.0) {
151 matWeight(fi, 0) = std::abs(matAf(i, j)) / colNorm;
152 } else {
153 matWeight(fi, 0) = 0.0;
154 }
155 }
156
157 QSharedPointer<NetworkEdge> pEdge =
158 QSharedPointer<NetworkEdge>(new NetworkEdge(j, i, matWeight));
159
160 finalNetwork.getNodeAt(j)->append(pEdge);
161 finalNetwork.getNodeAt(i)->append(pEdge);
162 finalNetwork.append(pEdge);
163 }
164 }
165
166 return finalNetwork;
167}
#define M_PI
ConnectivitySettings class declaration.
NetworkNode class declaration.
NetworkEdge class declaration.
Network class declaration.
MvarModel class declaration.
PartialDirectedCoherence class declaration.
Functional connectivity metrics (coherence, PLV, cross-correlation, etc.).
This class is a container for connectivity settings.
const IntermediateTrialData & at(int i) const
const Eigen::MatrixX3f & getNodePositions() const
MVAR model fitting for directed connectivity metrics.
Definition mvar_model.h:82
int order() const
void fit(const Eigen::MatrixXd &data, int p=0)
QVector< Eigen::MatrixXd > coefficients() const
static Network calculate(ConnectivitySettings &connectivitySettings)
This class holds information about a network, can compute a distance table and provide network metric...
Definition network.h:92
void append(QSharedPointer< NetworkEdge > newEdge)
void setUsedFreqBins(int iNumberFreqBins)
Definition network.cpp:513
void setFFTSize(int iFFTSize)
Definition network.cpp:520
void setSamplingFrequency(float fSFreq)
Definition network.cpp:499
QSharedPointer< NetworkNode > getNodeAt(int i)
Definition network.cpp:163
This class holds an object to describe the edge of a network.
Definition networkedge.h:80
This class holds an object to describe the node of a network.
Definition networknode.h:82
QSharedPointer< NetworkNode > SPtr
Definition networknode.h:85