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() <<
"PhaseLagIndex::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 iNRows = connectivitySettings.at(0).matData.rows();
110 RowVectorXf rowVert = RowVectorXf::Zero(3);
112 for(
int i = 0; i < iNRows; ++i) {
113 rowVert = RowVectorXf::Zero(3);
114 if(connectivitySettings.getNodePositions().rows() != 0 && i < connectivitySettings.getNodePositions().rows()) {
115 rowVert(0) = connectivitySettings.getNodePositions().row(i)(0);
116 rowVert(1) = connectivitySettings.getNodePositions().row(i)(1);
117 rowVert(2) = connectivitySettings.getNodePositions().row(i)(2);
124 int iSignalLength = connectivitySettings.at(0).matData.cols();
125 int iNfft = connectivitySettings.getFFTSize();
128 QPair<MatrixXd, VectorXd> tapers = Spectral::generateTapers(iSignalLength, connectivitySettings.getWindowType());
131 int iNFreqs = int(floor(iNfft / 2.0)) + 1;
134 if(m_iNumberBinStart == -1 ||
135 m_iNumberBinAmount == -1 ||
136 m_iNumberBinStart > iNFreqs ||
137 m_iNumberBinAmount > iNFreqs ||
138 m_iNumberBinAmount + m_iNumberBinStart > iNFreqs) {
139 qDebug() <<
"PhaseLagIndex::calculate - Resetting to full spectrum";
140 AbstractMetric::m_iNumberBinStart = 0;
141 AbstractMetric::m_iNumberBinAmount = iNFreqs;
152 connectivitySettings.getIntermediateSumData().vecPairCsdSum,
153 connectivitySettings.getIntermediateSumData().vecPairCsdImagSignSum,
166 QFuture<void> result = QtConcurrent::map(connectivitySettings.getTrialData(),
168 result.waitForFinished();
175 computePLI(connectivitySettings,
188 QVector<QPair<int,MatrixXcd> >& vecPairCsdSum,
189 QVector<QPair<int,MatrixXd> >& vecPairCsdImagSignSum,
194 const QPair<MatrixXd, VectorXd>& tapers)
196 if(inputData.vecPairCsdImagSign.size() == iNRows) {
205 if(inputData.vecTapSpectra.isEmpty()) {
206 RowVectorXd vecInputFFT, rowData;
207 RowVectorXcd vecTmpFreq;
209 MatrixXcd matTapSpectrum(tapers.first.rows(), iNFreqs);
212 fft.SetFlag(fft.HalfSpectrum);
214 for (i = 0; i < iNRows; ++i) {
216 rowData.array() = inputData.matData.row(i).array() - inputData.matData.row(i).mean();
219 for(j = 0; j < tapers.first.rows(); j++) {
221 if (rowData.cols() < iNfft) {
222 vecInputFFT.setZero(iNfft);
223 vecInputFFT.block(0,0,1,rowData.cols()) = rowData.cwiseProduct(tapers.first.row(j));;
225 vecInputFFT = rowData.cwiseProduct(tapers.first.row(j));
229 fft.fwd(vecTmpFreq, vecInputFFT, iNfft);
230 matTapSpectrum.row(j) = vecTmpFreq * tapers.second(j);
233 inputData.vecTapSpectra.append(matTapSpectrum);
238 if(inputData.vecPairCsd.isEmpty()) {
239 MatrixXcd matCsd = MatrixXcd(iNRows, m_iNumberBinAmount);
241 double denomCSD = sqrt(tapers.second.cwiseAbs2().sum()) * sqrt(tapers.second.cwiseAbs2().sum()) / 2.0;
243 bool bNfftEven =
false;
248 for (i = 0; i < iNRows; ++i) {
249 for (j = i; j < iNRows; ++j) {
251 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;
254 if(m_iNumberBinStart == 0) {
255 matCsd.row(j)(0) /= 2.0;
258 if(bNfftEven && m_iNumberBinStart + m_iNumberBinAmount >= iNFreqs) {
259 matCsd.row(j).tail(1) /= 2.0;
263 inputData.vecPairCsd.append(QPair<int,MatrixXcd>(i,matCsd));
264 inputData.vecPairCsdImagSign.append(QPair<int,MatrixXd>(i,matCsd.imag().cwiseSign()));
269 if(vecPairCsdSum.isEmpty()) {
270 vecPairCsdSum = inputData.vecPairCsd;
271 vecPairCsdImagSignSum = inputData.vecPairCsdImagSign;
273 for (
int j = 0; j < vecPairCsdSum.size(); ++j) {
274 vecPairCsdSum[j].second += inputData.vecPairCsd.at(j).second;
275 vecPairCsdImagSignSum[j].second += inputData.vecPairCsdImagSign.at(j).second;
281 if(inputData.vecPairCsdImagSign.isEmpty()) {
282 for (i = 0; i < inputData.vecPairCsd.size(); ++i) {
283 inputData.vecPairCsdImagSign.append(QPair<int,MatrixXd>(i,inputData.vecPairCsd.at(i).second.imag().cwiseSign()));
288 if(vecPairCsdImagSignSum.isEmpty()) {
289 vecPairCsdImagSignSum = inputData.vecPairCsdImagSign;
291 for (
int j = 0; j < vecPairCsdImagSignSum.size(); ++j) {
292 vecPairCsdImagSignSum[j].second += inputData.vecPairCsdImagSign.at(j).second;
300 if(!m_bStorageModeIsActive) {
301 inputData.vecPairCsd.clear();
302 inputData.vecTapSpectra.clear();
303 inputData.vecPairCsdImagSign.clear();
315 QSharedPointer<NetworkEdge> pEdge;
318 for (
int i = 0; i < connectivitySettings.getIntermediateSumData().vecPairCsdImagSignSum.size(); ++i) {
319 matNom = connectivitySettings.getIntermediateSumData().vecPairCsdImagSignSum.at(i).second.cwiseAbs() / connectivitySettings.size();
321 for(j = i; j < matNom.rows(); ++j) {
322 matWeight = matNom.row(j).transpose();
324 pEdge = QSharedPointer<NetworkEdge>(
new NetworkEdge(i, j, matWeight));
326 finalNetwork.
getNodeAt(i)->append(pEdge);
327 finalNetwork.
getNodeAt(j)->append(pEdge);
328 finalNetwork.
append(pEdge);