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() <<
"UnbiasedSquaredPhaseLagIndex::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() <<
"UnbiasedSquaredPhaseLagIndex::calculate - Resetting to full spectrum";
142 AbstractMetric::m_iNumberBinStart = 0;
143 AbstractMetric::m_iNumberBinAmount = iNFreqs;
154 connectivitySettings.getIntermediateSumData().vecPairCsdSum,
155 connectivitySettings.getIntermediateSumData().vecPairCsdImagSignSum,
168 QFuture<void> result = QtConcurrent::map(connectivitySettings.getTrialData(),
170 result.waitForFinished();
177 computeUSPLI(connectivitySettings,
190 QVector<QPair<int,MatrixXcd> >& vecPairCsdSum,
191 QVector<QPair<int,MatrixXd> >& vecPairCsdImagSignSum,
196 const QPair<MatrixXd, VectorXd>& tapers)
198 if(inputData.vecPairCsdImagSign.size() == iNRows) {
207 if(inputData.vecTapSpectra.size() != iNRows) {
208 inputData.vecTapSpectra.clear();
210 RowVectorXd vecInputFFT, rowData;
211 RowVectorXcd vecTmpFreq;
213 MatrixXcd matTapSpectrum(tapers.first.rows(), iNFreqs);
216 fft.SetFlag(fft.HalfSpectrum);
218 for (i = 0; i < iNRows; ++i) {
220 rowData.array() = inputData.matData.row(i).array() - inputData.matData.row(i).mean();
223 for(j = 0; j < tapers.first.rows(); j++) {
225 if (rowData.cols() < iNfft) {
226 vecInputFFT.setZero(iNfft);
227 vecInputFFT.block(0,0,1,rowData.cols()) = rowData.cwiseProduct(tapers.first.row(j));;
229 vecInputFFT = rowData.cwiseProduct(tapers.first.row(j));
233 fft.fwd(vecTmpFreq, vecInputFFT, iNfft);
234 matTapSpectrum.row(j) = vecTmpFreq * tapers.second(j);
237 inputData.vecTapSpectra.append(matTapSpectrum);
242 if(inputData.vecPairCsd.isEmpty()) {
243 double denomCSD = sqrt(tapers.second.cwiseAbs2().sum()) * sqrt(tapers.second.cwiseAbs2().sum()) / 2.0;
245 bool bNfftEven =
false;
250 MatrixXcd matCsd = MatrixXcd(iNRows, m_iNumberBinAmount);
252 for (i = 0; i < iNRows; ++i) {
253 for (j = i; j < iNRows; ++j) {
255 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;
258 if(m_iNumberBinStart == 0) {
259 matCsd.row(j)(0) /= 2.0;
262 if(bNfftEven && m_iNumberBinStart + m_iNumberBinAmount >= iNFreqs) {
263 matCsd.row(j).tail(1) /= 2.0;
267 inputData.vecPairCsd.append(QPair<int,MatrixXcd>(i,matCsd));
268 inputData.vecPairCsdImagSign.append(QPair<int,MatrixXd>(i,matCsd.imag().cwiseSign()));
273 if(vecPairCsdSum.isEmpty()) {
274 vecPairCsdSum = inputData.vecPairCsd;
275 vecPairCsdImagSignSum = inputData.vecPairCsdImagSign;
277 for (
int j = 0; j < vecPairCsdSum.size(); ++j) {
278 vecPairCsdSum[j].second += inputData.vecPairCsd.at(j).second;
279 vecPairCsdImagSignSum[j].second += inputData.vecPairCsdImagSign.at(j).second;
285 if(inputData.vecPairCsdImagSign.isEmpty()) {
286 for (i = 0; i < inputData.vecPairCsd.size(); ++i) {
287 inputData.vecPairCsdImagSign.append(QPair<int,MatrixXd>(i,inputData.vecPairCsd.at(i).second.imag().cwiseSign()));
292 if(vecPairCsdImagSignSum.isEmpty()) {
293 vecPairCsdImagSignSum = inputData.vecPairCsdImagSign;
295 for (
int j = 0; j < vecPairCsdImagSignSum.size(); ++j) {
296 vecPairCsdImagSignSum[j].second += inputData.vecPairCsdImagSign.at(j).second;
304 if(!m_bStorageModeIsActive) {
305 inputData.vecPairCsd.clear();
306 inputData.vecTapSpectra.clear();
307 inputData.vecPairCsdImagSign.clear();
319 QSharedPointer<NetworkEdge> pEdge;
321 double dNTrials = double(connectivitySettings.size() - 1.0);
323 for (
int i = 0; i < connectivitySettings.getIntermediateSumData().vecPairCsdImagSignSum.size(); ++i) {
324 matNom = connectivitySettings.getIntermediateSumData().vecPairCsdImagSignSum.at(i).second.cwiseAbs() / connectivitySettings.size();
325 matNom = (connectivitySettings.size() * matNom.array().square() - 1.0) / dNTrials;
327 for(j = i; j < matNom.rows(); ++j) {
328 matWeight = matNom.row(j).transpose();
330 pEdge = QSharedPointer<NetworkEdge>(
new NetworkEdge(i, j, matWeight));
332 finalNetwork.
getNodeAt(i)->append(pEdge);
333 finalNetwork.
getNodeAt(j)->append(pEdge);
334 finalNetwork.
append(pEdge);