73 printf(
"\tError during fiff setup raw read.\n");
88 printf(
"\tError during fiff setup raw read.\n");
121 cals = RowVectorXd();
128#include <QElapsedTimer>
134 const RowVectorXi& sel,
137 bool projAvailable =
true;
139 if (this->
proj.size() == 0) {
141 projAvailable =
false;
152 from = this->first_samp;
158 printf(
"No data in this range %d ... %d = %9.3f ... %9.3f secs...", from, to, ((
float)from)/this->
info.
sfreq, ((
float)to)/this->info.sfreq);
165 qint32 nchan = this->
info.nchan;
169 typedef Eigen::Triplet<double> T;
170 std::vector<T> tripletList;
171 tripletList.reserve(nchan);
172 for(i = 0; i < nchan; ++i)
173 tripletList.push_back(T(i, i, this->
cals[i]));
175 SparseMatrix<double> cal(nchan, nchan);
176 cal.setFromTriplets(tripletList.begin(), tripletList.end());
183 data = MatrixXd(nchan, to-from+1);
185 if (projAvailable || this->
comp.kind != -1)
188 mult_full = this->
comp.data->data*cal;
189 else if (this->
comp.kind == -1)
190 mult_full = this->
proj*cal;
192 mult_full = this->
proj*this->
comp.data->data*cal;
197 data = MatrixXd(sel.size(),to-from+1);
200 MatrixXd selVect(sel.size(), nchan);
204 if (!projAvailable && this->
comp.kind == -1)
207 tripletList.reserve(sel.size());
208 for(i = 0; i < sel.size(); ++i)
209 tripletList.push_back(T(i, i, this->
cals[sel[i]]));
210 cal = SparseMatrix<double>(sel.size(), sel.size());
211 cal.setFromTriplets(tripletList.begin(), tripletList.end());
217 qDebug() <<
"This has to be debugged! #1";
218 for( i = 0; i < sel.size(); ++i)
219 selVect.row(i) = this->
comp.data->data.block(sel[i],0,1,nchan);
220 mult_full = selVect*cal;
222 else if (this->
comp.kind == -1)
224 for( i = 0; i < sel.size(); ++i)
225 selVect.row(i) = this->
proj.block(sel[i],0,1,nchan);
227 mult_full = selVect*cal;
231 qDebug() <<
"This has to be debugged! #3";
232 for( i = 0; i < sel.size(); ++i)
233 selVect.row(i) = this->
proj.block(sel[i],0,1,nchan);
235 mult_full = selVect*this->
comp.data->data*cal;
244 tripletList.reserve(mult_full.rows()*mult_full.cols());
245 for(i = 0; i < mult_full.rows(); ++i)
246 for(k = 0; k < mult_full.cols(); ++k)
247 if(mult_full(i,k) != 0)
248 tripletList.push_back(T(i, k, mult_full(i,k)));
250 SparseMatrix<double> mult(mult_full.rows(),mult_full.cols());
251 if(tripletList.size() > 0)
252 mult.setFromTriplets(tripletList.begin(), tripletList.end());
256 if (!this->
file->device()->isOpen())
258 if (!this->
file->device()->open(QIODevice::ReadOnly))
260 printf(
"Cannot open file %s",this->
info.filename.toUtf8().constData());
269 MatrixXd one, newData, tmp_data;
270 FiffRawDir thisRawDir;
273 for(k = 0; k < this->
rawdir.size(); ++k)
275 thisRawDir = this->
rawdir[k];
279 if (thisRawDir.
last > from)
281 if (thisRawDir.
ent->kind == -1)
289 one.resize(nchan,thisRawDir.
nsamp);
291 one.resize(sel.cols(),thisRawDir.
nsamp);
297 fid->read_tag(t_pTag, thisRawDir.
ent->pos);
302 if (mult.cols() == 0)
307 one = cal*(Map< MatrixDau16 >( t_pTag->toDauPack16(),nchan, thisRawDir.
nsamp)).cast<double>();
309 one = cal*(Map< MatrixXi >( t_pTag->toInt(),nchan, thisRawDir.
nsamp)).cast<double>();
311 one = cal*(Map< MatrixXf >( t_pTag->toFloat(),nchan, thisRawDir.
nsamp)).cast<double>();
313 one = cal*(Map< MatrixShort >( t_pTag->toShort(),nchan, thisRawDir.
nsamp)).cast<double>();
315 printf(
"Data Storage Format not known yet [1]!! Type: %d\n", t_pTag->type);
320 newData.resize(sel.cols(), thisRawDir.
nsamp);
324 tmp_data = (Map< MatrixDau16 > ( t_pTag->toDauPack16(),nchan, thisRawDir.
nsamp)).cast<double>();
326 for(r = 0; r < sel.size(); ++r)
327 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
331 tmp_data = (Map< MatrixXi >( t_pTag->toInt(),nchan, thisRawDir.
nsamp)).cast<double>();
333 for(r = 0; r < sel.size(); ++r)
334 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
338 tmp_data = (Map< MatrixXf > ( t_pTag->toFloat(),nchan, thisRawDir.
nsamp)).cast<double>();
340 for(r = 0; r < sel.size(); ++r)
341 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
345 tmp_data = (Map< MatrixShort > ( t_pTag->toShort(),nchan, thisRawDir.
nsamp)).cast<double>();
347 for(r = 0; r < sel.size(); ++r)
348 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
352 printf(
"Data Storage Format not known yet [2]!! Type: %d\n", t_pTag->type);
361 one = mult*(Map< MatrixDau16 >( t_pTag->toDauPack16(),nchan, thisRawDir.
nsamp)).cast<double>();
363 one = mult*(Map< MatrixXi >( t_pTag->toInt(),nchan, thisRawDir.
nsamp)).cast<double>();
365 one = mult*(Map< MatrixXf >( t_pTag->toFloat(),nchan, thisRawDir.
nsamp)).cast<double>();
367 printf(
"Data Storage Format not known yet [3]!! Type: %d\n", t_pTag->type);
373 if (to >= thisRawDir.
last && from <= thisRawDir.
first)
379 last_pick = thisRawDir.
nsamp - 1;
383 else if (from > thisRawDir.
first)
385 first_pick = from - thisRawDir.
first;
386 if(to < thisRawDir.
last)
392 last_pick = thisRawDir.
nsamp + to - thisRawDir.
last - 1;
401 last_pick = thisRawDir.
nsamp - 1;
412 last_pick = to - thisRawDir.
first;
419 picksamp = last_pick - first_pick + 1;
423 qDebug() <<
"first_pick: " << first_pick;
424 qDebug() <<
"last_pick: " << last_pick;
425 qDebug() <<
"picksamp: " << picksamp;
433 data.block(0,dest,data.rows(),picksamp) = one.block(0, first_pick, data.rows(), picksamp);
441 if (thisRawDir.
last >= to)
448 if (!this->
file->device()->isOpen()) {
449 this->
file->device()->close();
452 times = MatrixXd(1, to-from+1);
454 for (i = 0; i < times.cols(); ++i)
455 times(0, i) = ((float)(from+i)) / this->
info.sfreq;
464 SparseMatrix<double>& multSegment,
467 const RowVectorXi& sel,
470 bool projAvailable =
true;
472 if (this->
proj.size() == 0) {
474 projAvailable =
false;
491 printf(
"No data in this range\n");
498 qint32 nchan = this->
info.nchan;
502 typedef Eigen::Triplet<double> T;
503 std::vector<T> tripletList;
504 tripletList.reserve(nchan);
505 for(i = 0; i < nchan; ++i)
506 tripletList.push_back(T(i, i, this->
cals[i]));
508 SparseMatrix<double> cal(nchan, nchan);
509 cal.setFromTriplets(tripletList.begin(), tripletList.end());
516 data = MatrixXd(nchan, to-from+1);
518 if (projAvailable || this->
comp.kind != -1)
521 mult_full = this->
comp.data->data*cal;
522 else if (this->
comp.kind == -1)
523 mult_full = this->
proj*cal;
525 mult_full = this->
proj*this->
comp.data->data*cal;
530 data = MatrixXd(sel.size(),to-from+1);
533 MatrixXd selVect(sel.size(), nchan);
537 if (!projAvailable && this->
comp.kind == -1)
540 tripletList.reserve(sel.size());
541 for(i = 0; i < sel.size(); ++i)
542 tripletList.push_back(T(i, i, this->
cals[sel[i]]));
543 cal = SparseMatrix<double>(sel.size(), sel.size());
544 cal.setFromTriplets(tripletList.begin(), tripletList.end());
550 qDebug() <<
"This has to be debugged! #1";
551 for( i = 0; i < sel.size(); ++i)
552 selVect.row(i) = this->
comp.data->data.block(sel[i],0,1,nchan);
553 mult_full = selVect*cal;
555 else if (this->
comp.kind == -1)
557 for( i = 0; i < sel.size(); ++i)
558 selVect.row(i) = this->
proj.block(sel[i],0,1,nchan);
560 mult_full = selVect*cal;
564 qDebug() <<
"This has to be debugged! #3";
565 for( i = 0; i < sel.size(); ++i)
566 selVect.row(i) = this->
proj.block(sel[i],0,1,nchan);
568 mult_full = selVect*this->
comp.data->data*cal;
577 tripletList.reserve(mult_full.rows()*mult_full.cols());
578 for(i = 0; i < mult_full.rows(); ++i)
579 for(k = 0; k < mult_full.cols(); ++k)
580 if(mult_full(i,k) != 0)
581 tripletList.push_back(T(i, k, mult_full(i,k)));
583 SparseMatrix<double> mult(mult_full.rows(),mult_full.cols());
584 if(tripletList.size() > 0)
585 mult.setFromTriplets(tripletList.begin(), tripletList.end());
591 if (!this->
file->device()->isOpen())
593 if (!this->
file->device()->open(QIODevice::ReadOnly))
595 printf(
"Cannot open file %s",this->
info.filename.toUtf8().constData());
606 for(k = 0; k < this->
rawdir.size(); ++k)
608 FiffRawDir thisRawDir = this->
rawdir[k];
612 if (thisRawDir.
last > from)
614 if (thisRawDir.
ent->kind == -1)
622 one.resize(nchan,thisRawDir.
nsamp);
624 one.resize(sel.cols(),thisRawDir.
nsamp);
631 fid->read_tag(t_pTag, thisRawDir.
ent->pos);
636 if (mult.cols() == 0)
641 one = cal*(Map< MatrixDau16 >( t_pTag->toDauPack16(),nchan, thisRawDir.
nsamp)).cast<double>();
643 one = cal*(Map< MatrixXi >( t_pTag->toInt(),nchan, thisRawDir.
nsamp)).cast<double>();
645 one = cal*(Map< MatrixXf >( t_pTag->toFloat(),nchan, thisRawDir.
nsamp)).cast<double>();
647 one = cal*(Map< MatrixShort >( t_pTag->toShort(),nchan, thisRawDir.
nsamp)).cast<double>();
649 printf(
"Data Storage Format not known yet [1]!! Type: %d\n", t_pTag->type);
655 MatrixXd newData(sel.cols(), thisRawDir.
nsamp);
659 MatrixXd tmp_data = (Map< MatrixDau16 > ( t_pTag->toDauPack16(),nchan, thisRawDir.
nsamp)).cast<double>();
661 for(r = 0; r < sel.size(); ++r)
662 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
666 MatrixXd tmp_data = (Map< MatrixXi >( t_pTag->toInt(),nchan, thisRawDir.
nsamp)).cast<double>();
668 for(r = 0; r < sel.size(); ++r)
669 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
673 MatrixXd tmp_data = (Map< MatrixXf > ( t_pTag->toFloat(),nchan, thisRawDir.
nsamp)).cast<double>();
675 for(r = 0; r < sel.size(); ++r)
676 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
680 MatrixXd tmp_data = (Map< MatrixShort > ( t_pTag->toShort(),nchan, thisRawDir.
nsamp)).cast<double>();
682 for(r = 0; r < sel.size(); ++r)
683 newData.block(r,0,1,thisRawDir.
nsamp) = tmp_data.block(sel[r],0,1,thisRawDir.
nsamp);
687 printf(
"Data Storage Format not known yet [2]!! Type: %d\n", t_pTag->type);
696 one = mult*(Map< MatrixDau16 >( t_pTag->toDauPack16(),nchan, thisRawDir.
nsamp)).cast<double>();
698 one = mult*(Map< MatrixXi >( t_pTag->toInt(),nchan, thisRawDir.
nsamp)).cast<double>();
700 one = mult*(Map< MatrixXf >( t_pTag->toFloat(),nchan, thisRawDir.
nsamp)).cast<double>();
702 printf(
"Data Storage Format not known yet [3]!! Type: %d\n", t_pTag->type);
708 if (to >= thisRawDir.
last && from <= thisRawDir.
first)
714 last_pick = thisRawDir.
nsamp - 1;
718 else if (from > thisRawDir.
first)
720 first_pick = from - thisRawDir.
first;
721 if(to < thisRawDir.
last)
727 last_pick = thisRawDir.
nsamp + to - thisRawDir.
last - 1;
736 last_pick = thisRawDir.
nsamp - 1;
747 last_pick = to - thisRawDir.
first;
754 picksamp = last_pick - first_pick + 1;
758 qDebug() <<
"first_pick: " << first_pick;
759 qDebug() <<
"last_pick: " << last_pick;
760 qDebug() <<
"picksamp: " << picksamp;
768 data.block(0,dest,data.rows(),picksamp) = one.block(0, first_pick, data.rows(), picksamp);
776 if (thisRawDir.
last >= to)
788 if (!this->
file->device()->isOpen()) {
789 this->
file->device()->close();
792 times = MatrixXd(1, to-from+1);
794 for (i = 0; i < times.cols(); ++i)
795 times(0, i) = ((float)(from+i)) / this->
info.sfreq;
806 const RowVectorXi& sel)
const
811 from = floor(from*this->
info.sfreq);
812 to = ceil(to*this->
info.sfreq);
822 const RowVectorXi &picks,
827 if (decim < 1) decim = 1;
829 int firstSamp = (from >= 0) ? from :
first_samp;
830 int lastSamp = (to >= 0) ? to :
last_samp;
832 if (firstSamp > lastSamp) {
833 qWarning() <<
"[FiffRawData::save] Invalid sample range.";
839 if (picks.size() > 0) {
840 outInfo =
info.pick_info(picks);
847 outInfo.
sfreq =
info.sfreq /
static_cast<float>(decim);
854 qWarning() <<
"[FiffRawData::save] Cannot start writing raw file.";
859 const int blockSize = 2000;
860 int blockSamples = decim * blockSize;
862 for (
int samp = firstSamp; samp <= lastSamp; samp += blockSamples) {
863 int nsamp = qMin(blockSamples, lastSamp - samp + 1);
868 qWarning() <<
"[FiffRawData::save] Error reading data at sample" << samp;
869 pStream->finish_writing_raw();
875 int nOut = (nsamp + decim - 1) / decim;
876 MatrixXd decimData(segData.rows(), nOut);
877 for (
int s = 0, idx = 0; s < nsamp && idx < nOut; s += decim, ++idx) {
878 decimData.col(idx) = segData.col(s);
883 pStream->write_raw_buffer(segData, calsOut);
886 pStream->finish_writing_raw();
888 qInfo() <<
"[FiffRawData::save] Saved raw data from sample" << firstSamp
889 <<
"to" << lastSamp <<
"(decim=" << decim <<
")";
FiffTag class declaration, which provides fiff tag I/O and processing methods.
FiffStream class declaration.
FiffRawData class declaration.
FiffEvents class declaration.
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
FIFF measurement file information.
bool read_raw_segment(Eigen::MatrixXd &data, Eigen::MatrixXd ×, fiff_int_t from=-1, fiff_int_t to=-1, const Eigen::RowVectorXi &sel=defaultRowVectorXi, bool do_debug=false) const
bool save(QIODevice &p_IODevice, const Eigen::RowVectorXi &picks=Eigen::RowVectorXi(), int decim=1, int from=-1, int to=-1) const
bool read_raw_segment_times(Eigen::MatrixXd &data, Eigen::MatrixXd ×, float from, float to, const Eigen::RowVectorXi &sel=defaultRowVectorXi) const
QList< FiffRawDir > rawdir
static bool setup_read_raw(QIODevice &p_IODevice, FiffRawData &data, bool allow_maxshield=true, bool is_littleEndian=false)
static FiffStream::SPtr start_writing_raw(QIODevice &p_IODevice, const FiffInfo &info, Eigen::RowVectorXd &cals, Eigen::MatrixXi sel=defaultMatrixXi, bool bResetRange=true)
QSharedPointer< FiffStream > SPtr
QSharedPointer< FiffTag > SPtr