44 #include "../network/networknode.h"
45 #include "../network/networkedge.h"
46 #include "../network/network.h"
55 #include <QtConcurrent>
61 #include <unsupported/Eigen/FFT>
67 using namespace CONNECTIVITYLIB;
68 using namespace Eigen;
69 using namespace UTILSLIB;
93 if(connectivitySettings.isEmpty()) {
94 qDebug() <<
"DebiasedSquaredWeightedPhaseLagIndex::calculate - Input data is empty";
98 if(AbstractMetric::m_bStorageModeIsActive ==
false) {
99 connectivitySettings.clearIntermediateData();
104 #ifdef EIGEN_FFTW_DEFAULT
105 fftw_make_planner_thread_safe();
109 int rows = connectivitySettings.at(0).matData.rows();
110 RowVectorXf rowVert = RowVectorXf::Zero(3);
112 for(
int i = 0; i < rows; ++i) {
113 rowVert = RowVectorXf::Zero(3);
115 if(connectivitySettings.getNodePositions().rows() != 0 && i < connectivitySettings.getNodePositions().rows()) {
116 rowVert(0) = connectivitySettings.getNodePositions().row(i)(0);
117 rowVert(1) = connectivitySettings.getNodePositions().row(i)(1);
118 rowVert(2) = connectivitySettings.getNodePositions().row(i)(2);
125 int iSignalLength = connectivitySettings.at(0).matData.cols();
126 int iNfft = connectivitySettings.getFFTSize();
129 QPair<MatrixXd, VectorXd> tapers = Spectral::generateTapers(iSignalLength, connectivitySettings.getWindowType());
132 int iNRows = connectivitySettings.at(0).matData.rows();
133 int iNFreqs = int(floor(iNfft / 2.0)) + 1;
136 if(m_iNumberBinStart == -1 ||
137 m_iNumberBinAmount == -1 ||
138 m_iNumberBinStart > iNFreqs ||
139 m_iNumberBinAmount > iNFreqs ||
140 m_iNumberBinAmount + m_iNumberBinStart > iNFreqs) {
141 qDebug() <<
"DebiasedSquaredWeightedPhaseLagIndex::calculate - Resetting to full spectrum";
142 AbstractMetric::m_iNumberBinStart = 0;
143 AbstractMetric::m_iNumberBinAmount = iNFreqs;
153 return compute(inputData,
154 connectivitySettings.getIntermediateSumData().vecPairCsdSum,
155 connectivitySettings.getIntermediateSumData().vecPairCsdImagAbsSum,
156 connectivitySettings.getIntermediateSumData().vecPairCsdImagSqrdSum,
169 QFuture<void> result = QtConcurrent::map(connectivitySettings.getTrialData(),
171 result.waitForFinished();
178 computeDSWPLI(connectivitySettings,
191 QVector<QPair<int,MatrixXcd> >& vecPairCsdSum,
192 QVector<QPair<int,MatrixXd> >& vecPairCsdImagAbsSum,
193 QVector<QPair<int,MatrixXd> >& vecPairCsdImagSqrdSum,
198 const QPair<MatrixXd, VectorXd>& tapers)
200 if(inputData.vecPairCsd.size() == iNRows &&
201 inputData.vecPairCsdImagSqrd.size() == iNRows &&
202 inputData.vecPairCsdImagAbs.size() == iNRows) {
211 if(inputData.vecTapSpectra.isEmpty()) {
212 RowVectorXd vecInputFFT, rowData;
213 RowVectorXcd vecTmpFreq;
215 MatrixXcd matTapSpectrum(tapers.first.rows(), iNFreqs);
218 fft.SetFlag(fft.HalfSpectrum);
220 for (i = 0; i < iNRows; ++i) {
222 rowData.array() = inputData.matData.row(i).array() - inputData.matData.row(i).mean();
225 for(j = 0; j < tapers.first.rows(); j++) {
227 if (rowData.cols() < iNfft) {
228 vecInputFFT.setZero(iNfft);
229 vecInputFFT.block(0,0,1,rowData.cols()) = rowData.cwiseProduct(tapers.first.row(j));;
231 vecInputFFT = rowData.cwiseProduct(tapers.first.row(j));
235 fft.fwd(vecTmpFreq, vecInputFFT, iNfft);
236 matTapSpectrum.row(j) = vecTmpFreq * tapers.second(j);
239 inputData.vecTapSpectra.append(matTapSpectrum);
244 if(inputData.vecPairCsd.isEmpty()) {
245 MatrixXcd matCsd(iNRows, m_iNumberBinAmount);
247 bool bNfftEven =
false;
252 double denomCSD = sqrt(tapers.second.cwiseAbs2().sum()) * sqrt(tapers.second.cwiseAbs2().sum()) / 2.0;
254 for (i = 0; i < iNRows; ++i) {
255 for (j = i; j < iNRows; ++j) {
257 matCsd.row(j) = inputData.vecTapSpectra.at(i).block(0,m_iNumberBinStart,inputData.vecTapSpectra.at(i).rows(),m_iNumberBinAmount).cwiseProduct(inputData.vecTapSpectra.at(j).block(0,m_iNumberBinStart,inputData.vecTapSpectra.at(j).rows(),m_iNumberBinAmount).conjugate()).colwise().sum() / denomCSD;
260 if(m_iNumberBinStart == 0) {
261 matCsd.row(j)(0) /= 2.0;
264 if(bNfftEven && m_iNumberBinStart + m_iNumberBinAmount >= iNFreqs) {
265 matCsd.row(j).tail(1) /= 2.0;
269 inputData.vecPairCsd.append(QPair<int,MatrixXcd>(i,matCsd));
270 inputData.vecPairCsdImagSqrd.append(QPair<int,MatrixXd>(i,matCsd.imag().array().square()));
271 inputData.vecPairCsdImagAbs.append(QPair<int,MatrixXd>(i,matCsd.imag().cwiseAbs()));
276 if(vecPairCsdSum.isEmpty()) {
277 vecPairCsdSum = inputData.vecPairCsd;
278 vecPairCsdImagSqrdSum = inputData.vecPairCsdImagSqrd;
279 vecPairCsdImagAbsSum = inputData.vecPairCsdImagAbs;
281 for (
int j = 0; j < vecPairCsdSum.size(); ++j) {
282 vecPairCsdSum[j].second += inputData.vecPairCsd.at(j).second;
283 vecPairCsdImagSqrdSum[j].second += inputData.vecPairCsdImagSqrd.at(j).second;
284 vecPairCsdImagAbsSum[j].second += inputData.vecPairCsdImagAbs.at(j).second;
290 if(inputData.vecPairCsdImagSqrd.isEmpty()) {
291 for (i = 0; i < inputData.vecPairCsd.size(); ++i) {
292 inputData.vecPairCsdImagSqrd.append(QPair<int,MatrixXd>(i,inputData.vecPairCsd.at(i).second.imag().array().square()));
297 if(vecPairCsdImagSqrdSum.isEmpty()) {
298 vecPairCsdImagSqrdSum = inputData.vecPairCsdImagSqrd;
300 for (
int j = 0; j < vecPairCsdSum.size(); ++j) {
301 vecPairCsdImagSqrdSum[j].second += inputData.vecPairCsdImagSqrd.at(j).second;
308 if(inputData.vecPairCsdImagAbs.isEmpty()) {
309 for (i = 0; i < inputData.vecPairCsd.size(); ++i) {
310 inputData.vecPairCsdImagAbs.append(QPair<int,MatrixXd>(i,inputData.vecPairCsd.at(i).second.imag().cwiseAbs()));
315 if(vecPairCsdImagAbsSum.isEmpty()) {
316 vecPairCsdImagAbsSum = inputData.vecPairCsdImagAbs;
318 for (
int j = 0; j < vecPairCsdSum.size(); ++j) {
319 vecPairCsdImagAbsSum[j].second += inputData.vecPairCsdImagAbs.at(j).second;
327 if(!m_bStorageModeIsActive) {
328 inputData.vecPairCsd.clear();
329 inputData.vecTapSpectra.clear();
330 inputData.vecPairCsdImagAbs.clear();
331 inputData.vecPairCsdImagSqrd.clear();
341 MatrixXd matNom, matDenom;
343 QSharedPointer<NetworkEdge> pEdge;
346 for (
int i = 0; i < connectivitySettings.at(0).matData.rows(); ++i) {
348 matNom = connectivitySettings.getIntermediateSumData().vecPairCsdSum.at(i).second.imag().array().square();
349 matNom -= connectivitySettings.getIntermediateSumData().vecPairCsdImagSqrdSum.at(i).second;
351 matDenom = connectivitySettings.getIntermediateSumData().vecPairCsdImagAbsSum.at(i).second.array().square();
352 matDenom -= connectivitySettings.getIntermediateSumData().vecPairCsdImagSqrdSum.at(i).second;
354 matDenom = (matDenom.array() == 0.).select(INFINITY, matDenom);
355 matDenom = matNom.cwiseQuotient(matDenom);
357 for(j = i; j < connectivitySettings.at(0).matData.rows(); ++j) {
358 matWeight = matDenom.row(j).transpose();
360 pEdge = QSharedPointer<NetworkEdge>(
new NetworkEdge(i, j, matWeight));
362 finalNetwork.
getNodeAt(i)->append(pEdge);
363 finalNetwork.
getNodeAt(j)->append(pEdge);
364 finalNetwork.
append(pEdge);