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;
94 if(connectivitySettings.isEmpty()) {
95 qWarning() <<
"WeightedPhaseLagIndex::calculate - Input data is empty";
99 if(AbstractMetric::m_bStorageModeIsActive ==
false) {
100 connectivitySettings.clearIntermediateData();
105 #ifdef EIGEN_FFTW_DEFAULT
106 fftw_make_planner_thread_safe();
110 int rows = connectivitySettings.at(0).matData.rows();
111 RowVectorXf rowVert = RowVectorXf::Zero(3);
113 for(
int i = 0; i < rows; ++i) {
114 rowVert = RowVectorXf::Zero(3);
116 if(connectivitySettings.getNodePositions().rows() != 0 && i < connectivitySettings.getNodePositions().rows()) {
117 rowVert(0) = connectivitySettings.getNodePositions().row(i)(0);
118 rowVert(1) = connectivitySettings.getNodePositions().row(i)(1);
119 rowVert(2) = connectivitySettings.getNodePositions().row(i)(2);
126 int iSignalLength = connectivitySettings.at(0).matData.cols();
127 int iNfft = connectivitySettings.getFFTSize();
130 QPair<MatrixXd, VectorXd> tapers = Spectral::generateTapers(iSignalLength, connectivitySettings.getWindowType());
133 int iNRows = connectivitySettings.at(0).matData.rows();
134 int iNFreqs = int(floor(iNfft / 2.0)) + 1;
137 if(m_iNumberBinStart == -1 ||
138 m_iNumberBinAmount == -1 ||
139 m_iNumberBinStart > iNFreqs ||
140 m_iNumberBinAmount > iNFreqs ||
141 m_iNumberBinAmount + m_iNumberBinStart > iNFreqs) {
142 qDebug() <<
"WeightedPhaseLagIndex::calculate - Resetting to full spectrum";
143 AbstractMetric::m_iNumberBinStart = 0;
144 AbstractMetric::m_iNumberBinAmount = iNFreqs;
155 connectivitySettings.getIntermediateSumData().vecPairCsdSum,
156 connectivitySettings.getIntermediateSumData().vecPairCsdImagAbsSum,
169 QFuture<void> result = QtConcurrent::map(connectivitySettings.getTrialData(),
171 result.waitForFinished();
191 QVector<QPair<int,MatrixXcd> >& vecPairCsdSum,
192 QVector<QPair<int,MatrixXd> >& vecPairCsdImagAbsSum,
197 const QPair<MatrixXd, VectorXd>& tapers)
203 if(inputData.vecPairCsd.size() == iNRows &&
204 inputData.vecPairCsdImagAbs.size() == iNRows ) {
213 if(inputData.vecTapSpectra.size() != iNRows) {
214 inputData.vecTapSpectra.clear();
216 RowVectorXd vecInputFFT, rowData;
217 RowVectorXcd vecTmpFreq;
219 MatrixXcd matTapSpectrum(tapers.first.rows(), iNFreqs);
222 fft.SetFlag(fft.HalfSpectrum);
224 for (i = 0; i < iNRows; ++i) {
226 rowData.array() = inputData.matData.row(i).array() - inputData.matData.row(i).mean();
229 for(j = 0; j < tapers.first.rows(); j++) {
231 if (rowData.cols() < iNfft) {
232 vecInputFFT.setZero(iNfft);
233 vecInputFFT.block(0,0,1,rowData.cols()) = rowData.cwiseProduct(tapers.first.row(j));;
235 vecInputFFT = rowData.cwiseProduct(tapers.first.row(j));
239 fft.fwd(vecTmpFreq, vecInputFFT, iNfft);
240 matTapSpectrum.row(j) = vecTmpFreq * tapers.second(j);
243 inputData.vecTapSpectra.append(matTapSpectrum);
252 if(inputData.vecPairCsd.isEmpty()) {
253 double denomCSD = sqrt(tapers.second.cwiseAbs2().sum()) * sqrt(tapers.second.cwiseAbs2().sum()) / 2.0;
254 bool bNfftEven =
false;
259 MatrixXcd matCsd = MatrixXcd(iNRows, m_iNumberBinAmount);
261 for (i = 0; i < iNRows; ++i) {
262 for (j = i; j < iNRows; ++j) {
264 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;
267 if(m_iNumberBinStart == 0) {
268 matCsd.row(j)(0) /= 2.0;
271 if(bNfftEven && m_iNumberBinStart + m_iNumberBinAmount >= iNFreqs) {
272 matCsd.row(j).tail(1) /= 2.0;
276 inputData.vecPairCsd.append(QPair<int,MatrixXcd>(i,matCsd));
277 inputData.vecPairCsdImagAbs.append(QPair<int,MatrixXd>(i,matCsd.imag().cwiseAbs()));
286 if(vecPairCsdSum.isEmpty()) {
287 vecPairCsdSum = inputData.vecPairCsd;
288 vecPairCsdImagAbsSum = inputData.vecPairCsdImagAbs;
290 for (
int j = 0; j < vecPairCsdSum.size(); ++j) {
291 vecPairCsdSum[j].second += inputData.vecPairCsd.at(j).second;
292 vecPairCsdImagAbsSum[j].second += inputData.vecPairCsdImagAbs.at(j).second;
302 if (inputData.vecPairCsdImagAbs.isEmpty()) {
303 inputData.vecPairCsdImagAbs.clear();
304 for (i = 0; i < inputData.vecPairCsd.size(); ++i) {
305 inputData.vecPairCsdImagAbs.append(QPair<int,MatrixXd>(i,inputData.vecPairCsd.at(i).second.imag().cwiseAbs()));
310 if(vecPairCsdImagAbsSum.isEmpty()) {
311 vecPairCsdImagAbsSum = inputData.vecPairCsdImagAbs;
313 for (
int j = 0; j < vecPairCsdImagAbsSum.size(); ++j) {
314 vecPairCsdImagAbsSum[j].second += inputData.vecPairCsdImagAbs.at(j).second;
323 if(!m_bStorageModeIsActive) {
324 inputData.vecPairCsd.clear();
325 inputData.vecPairCsdImagAbs.clear();
326 inputData.vecTapSpectra.clear();
336 MatrixXd matDenom, matNom;
338 QSharedPointer<NetworkEdge> pEdge;
341 for (
int i = 0; i < connectivitySettings.getIntermediateSumData().vecPairCsdSum.size(); ++i) {
342 matDenom = connectivitySettings.getIntermediateSumData().vecPairCsdImagAbsSum.at(i).second;
343 matDenom = (matDenom.array() == 0.).select(INFINITY, matDenom);
345 matNom = connectivitySettings.getIntermediateSumData().vecPairCsdSum.at(i).second.imag().cwiseAbs().cwiseQuotient(matDenom);
347 for(j = i; j < matNom.rows(); ++j) {
348 matWeight = matNom.row(j).transpose();
350 pEdge = QSharedPointer<NetworkEdge>(
new NetworkEdge(i, j, matWeight));
352 finalNetwork.
getNodeAt(i)->append(pEdge);
353 finalNetwork.
getNodeAt(j)->append(pEdge);
354 finalNetwork.
append(pEdge);