47 #define _USE_MATH_DEFINES
54 using namespace Eigen;
55 using namespace FIFFLIB;
56 using namespace MNELIB;
74 #define MALLOC_36(x,t) (t *)malloc((x)*sizeof(t))
75 #define REALLOC_36(x,y,t) (t *)((x == NULL) ? malloc((y)*sizeof(t)) : realloc((x),(y)*sizeof(t)))
77 #define ALLOC_CMATRIX_36(x,y) mne_cmatrix_36((x),(y))
79 #if defined(_WIN32) || defined(_WIN64)
80 #define snprintf _snprintf
81 #define vsnprintf _vsnprintf
82 #define strcasecmp _stricmp
83 #define strncasecmp _strnicmp
86 static void matrix_error(
int kind,
int nr,
int nc)
90 printf(
"Failed to allocate memory pointers for a %d x %d matrix\n",nr,nc);
92 printf(
"Failed to allocate memory for a %d x %d matrix\n",nr,nc);
94 printf(
"Allocation error for a %d x %d matrix\n",nr,nc);
95 if (
sizeof(
void *) == 4) {
96 printf(
"This is probably because you seem to be using a computer with 32-bit architecture.\n");
97 printf(
"Please consider moving to a 64-bit platform.");
99 printf(
"Cannot continue. Sorry.\n");
103 float **mne_cmatrix_36(
int nr,
int nc)
110 m = MALLOC_36(nr,
float *);
111 if (!m) matrix_error(1,nr,nc);
112 whole = MALLOC_36(nr*nc,
float);
113 if (!whole) matrix_error(2,nr,nc);
120 #define FREE_36(x) if ((char *)(x) != NULL) free((char *)(x))
122 #define FREE_CMATRIX_36(m) mne_free_cmatrix_36((m))
124 void mne_free_cmatrix_36 (
float **m)
149 void mne_free_ring_buffer_36(
void *thisp)
158 for (
k = 0;
k < this_buf->nbuf;
k++)
159 FREE_36(this_buf->bufs[
k]->data);
160 FREE_36(this_buf->bufs);
180 for (
k = 0;
k < list->nevent;
k++)
181 mne_free_event(list->events[
k]);
182 FREE_36(list->events);
187 void mne_free_name_list_36(
char **list,
int nlist)
193 if (list == NULL || nlist == 0)
195 for (
k = 0;
k < nlist;
k++) {
197 printf(
"%d %s\n",
k,list[
k]);
207 void mne_string_to_name_list(
const QString& s, QStringList& listp,
int &nlistp)
214 if (!s.isEmpty() && s.size() > 0) {
219 nlistp = list.size();
223 QString mne_name_list_to_string(
const QStringList& list)
228 int nlist = list.size();
230 if (nlist == 0 || list.isEmpty())
233 for (
int k = 0;
k < nlist-1;
k++) {
237 res += list[nlist-1];
241 QString mne_channel_names_to_string(
const QList<FIFFLIB::FiffChInfo>& chs,
251 for (
int k = 0;
k < nch;
k++)
252 names.append(chs[
k].ch_name);
253 res = mne_name_list_to_string(names);
257 void mne_channel_names_to_name_list(
const QList<FIFFLIB::FiffChInfo>& chs,
263 QString s = mne_channel_names_to_string(chs,nch);
264 mne_string_to_name_list(s,listp,nlistp);
275 float *eog_freq_resp;
283 static void filter_data_free(
void *datap)
289 FREE_36(data->freq_resp);
290 FREE_36(data->eog_freq_resp);
291 FREE_36(data->precalc);
301 data->freq_resp = NULL;
302 data->eog_freq_resp = NULL;
303 data->precalc = NULL;
314 if (f1->filter_on != f2->filter_on ||
315 std::fabs(f1->lowpass - f2->lowpass) > 0.1 ||
316 std::fabs(f1->lowpass_width - f2->lowpass_width) > 0.1 ||
317 std::fabs(f1->highpass - f2->highpass) > 0.1 ||
318 std::fabs(f1->highpass_width - f2->highpass_width) > 0.1 ||
319 std::fabs(f1->eog_lowpass - f2->eog_lowpass) > 0.1 ||
320 std::fabs(f1->eog_lowpass_width - f2->eog_lowpass_width) > 0.1 ||
321 std::fabs(f1->eog_highpass - f2->eog_highpass) > 0.1 ||
322 std::fabs(f1->eog_highpass_width - f2->eog_highpass_width) > 0.1)
330 void mne_fft_ana(
float *data,
int np,
float **precalcp)
337 printf(
"##################### DEBUG Error: FFT analysis needs to be implemented");
353 void mne_fft_syn(
float *data,
int np,
float **precalcp)
361 printf(
"##################### DEBUG Error: FFT synthesis needs to be implemented");
383 int mne_apply_filter(
mneFilterDef filter,
void *datap,
float *data,
int ns,
int zero_pad,
float dc_offset,
int kind)
392 if (ns != filter->size + 2*filter->taper_size) {
393 printf(
"Incorrect data length in apply_filter");
400 for (
k = 0;
k < filter->taper_size;
k++)
402 for (
k = filter->taper_size + filter->size;
k < ns;
k++)
405 if (!filter->filter_on)
410 if (dc_offset != 0.0) {
411 for (
k = filter->taper_size; k < filter->taper_size + filter->size;
k++)
412 data[
k] = data[
k] - dc_offset;
421 mne_fft_ana(data,ns,&d->precalc);
426 n = ns % 2 == 0 ? ns/2 : (ns+1)/2;
431 if (kind == FIFFV_EOG_CH)
432 freq_resp = d->eog_freq_resp;
434 freq_resp = d->freq_resp;
435 data[p] = data[p]*freq_resp[0]; p++;
439 for (
k = 1 ;
k < n ;
k++) {
440 data[p] = data[p]*freq_resp[
k]; p++;
441 data[p] = data[p]*freq_resp[
k]; p++;
447 data[p] = data[p]*freq_resp[
k];
449 mne_fft_syn(data,ns,&d->precalc);
457 mneUserFreeFunc *filter_data_freep,
458 int *highpass_effective)
465 int highpasss,lowpasss;
466 int highpass_widths,lowpass_widths;
467 float lowpass,highpass,lowpass_width,highpass_width;
469 float pi4 = M_PI/4.0;
473 resp_size = (filter->size + 2*filter->taper_size)/2 + 1;
475 filter_data = new_filter_data();
476 filter_data->freq_resp = MALLOC_36(resp_size,
float);
477 filter_data->eog_freq_resp = MALLOC_36(resp_size,
float);
478 filter_data->np = resp_size;
480 for (
k = 0;
k < resp_size;
k++) {
481 filter_data->freq_resp[
k] = 1.0;
482 filter_data->eog_freq_resp[
k] = 1.0;
484 *highpass_effective = FALSE;
486 for (f = 0; f < 2; f++) {
487 highpass = f == 0 ? filter->highpass : filter->eog_highpass;
488 highpass_width = f == 0 ? filter->highpass_width : filter->eog_highpass_width;
489 lowpass = f == 0 ? filter->lowpass : filter->eog_lowpass;
490 lowpass_width = f == 0 ? filter->lowpass_width : filter->eog_lowpass_width;
491 freq_resp = f == 0 ? filter_data->freq_resp : filter_data->eog_freq_resp;
495 highpasss = ((resp_size-1)*highpass)/(0.5*sfreq);
496 lowpasss = ((resp_size-1)*lowpass)/(0.5*sfreq);
498 lowpass_widths = ((resp_size-1)*lowpass_width)/(0.5*sfreq);
499 lowpass_widths = (lowpass_widths+1)/2;
501 if (filter->highpass_width > 0.0) {
502 highpass_widths = ((resp_size-1)*highpass_width)/(0.5*sfreq);
503 highpass_widths = (highpass_widths+1)/2;
508 if (filter->filter_on) {
509 printf(
"filter : %7.3f ... %6.1f Hz bins : %d ... %d of %d hpw : %d lpw : %d\n",
518 if (highpasss > highpass_widths + 1) {
522 for (
k = 0;
k < highpasss-w+1;
k++)
524 for (
k = -w+1, s = highpasss-w+1;
k < w;
k++, s++) {
525 if (s >= 0 && s < resp_size) {
526 c = cos(pi4*(
k*mult+add));
527 freq_resp[s] = freq_resp[s]*c*c;
530 *highpass_effective = TRUE;
533 *highpass_effective = *highpass_effective || (filter->highpass == 0.0);
535 if (lowpass_widths > 0) {
539 for (
k = -w+1, s = lowpasss-w+1;
k < w;
k++, s++) {
540 if (s >= 0 && s < resp_size) {
541 c = cos(pi4*(
k*mult+add));
542 freq_resp[s] = freq_resp[s]*c*c;
545 for (
k = s;
k < resp_size;
k++)
549 for (
k = lowpasss;
k < resp_size;
k++)
552 if (filter->filter_on) {
553 if (*highpass_effective)
554 printf(
"Highpass filter will work as specified.\n");
556 printf(
"NOTE: Highpass filter omitted due to a too low corner frequency.\n");
559 printf(
"NOTE: Filter is presently switched off.\n");
561 *filter_datap = filter_data;
562 *filter_data_freep = filter_data_free;
585 void mne_free_ring_buffer(
void *thisp)
594 for (
k = 0;
k < this_buf->nbuf;
k++)
595 FREE_36(this_buf->bufs[
k]->data);
596 FREE_36(this_buf->bufs);
601 void *mne_initialize_ring(
int nbuf)
611 for (
k = 0;
k < nbuf;
k++) {
613 ring->bufs[
k]->size = 0;
614 ring->bufs[
k]->data = NULL;
615 ring->bufs[
k]->datap = NULL;
620 printf(
"Ring buffer structure with %d entries initialized\n",ring->nbuf);
626 void mne_allocate_from_ring(
void *ringp,
int nrow,
int ncol,
float ***res)
636 if (ring->next > ring->nbuf-1)
640 printf(
"Allocating buf # %d\n",ring->next);
643 buf = ring->bufs[ring->next++];
646 FREE_36(*buf->datap);
649 *res = mat = MALLOC_36(nrow,
float *);
650 if (buf->size < nrow*ncol)
651 buf->data = REALLOC_36(buf->data,nrow*ncol,
float);
653 for (j = 0; j < nrow; j++)
654 mat[j] = buf->data + j*ncol;
663 int mne_read_raw_buffer_t(
669 const QList<FIFFLIB::FiffChInfo>& chs,
676 fiff_short_t *this_samples;
677 fiff_float_t *this_samplef;
678 fiff_int_t *this_sample;
687 pickno = MALLOC_36(nchan,
int);
688 for (c = 0; c < nchan; c++)
696 mult = MALLOC_36(npick,
float);
697 for (c = 0; c < npick; c++)
698 mult[c] = chs[pickno[c]].cal*chs[pickno[c]].range;
702 if (!stream->read_tag(t_pTag,ent->pos))
705 if (ent->type == FIFFT_FLOAT) {
706 if ((
int)(t_pTag->size()/(
sizeof(fiff_float_t)*nchan)) != nsamp) {
707 printf(
"Incorrect number of samples in buffer.");
710 qDebug() <<
"ToDo: Check whether this_samplef contains the right stuff!!! - use VectorXf instead";
711 this_samplef = t_pTag->toFloat();
712 for (s = 0; s < nsamp; s++, this_samplef += nchan) {
713 for (c = 0; c < npick; c++)
714 data[c][s] = mult[c]*this_samplef[pickno[c]];
717 else if (ent->type == FIFFT_SHORT || ent->type == FIFFT_DAU_PACK16) {
718 if ((
int)(t_pTag->size()/(
sizeof(fiff_short_t)*nchan)) != nsamp) {
719 printf(
"Incorrect number of samples in buffer.");
722 qDebug() <<
"ToDo: Check whether this_samples contains the right stuff!!! - use VectorXi instead";
723 this_samples = (fiff_short_t *)t_pTag->data();
724 for (s = 0; s < nsamp; s++, this_samples += nchan) {
725 for (c = 0; c < npick; c++)
726 data[c][s] = mult[c]*this_samples[pickno[c]];
729 else if (ent->type == FIFFT_INT) {
730 if ((
int)(t_pTag->size()/(
sizeof(fiff_int_t)*nchan)) != nsamp) {
731 printf(
"Incorrect number of samples in buffer.");
734 qDebug() <<
"ToDo: Check whether this_sample contains the right stuff!!! - use VectorXi instead";
735 this_sample = t_pTag->toInt();
736 for (s = 0; s < nsamp; s++, this_sample += nchan) {
737 for (c = 0; c < npick; c++)
738 data[c][s] = mult[c]*this_sample[pickno[c]];
742 printf(
"We are not prepared to handle raw data type: %d",ent->type);
765 QList<FiffDirNode::SPtr> temp;
771 if (pNode->isEmpty())
772 node = stream->dirtree();
776 temp = node->dir_tree_find(FIFFB_MNE_BAD_CHANNELS);
777 if (temp.size() > 0) {
780 bad->find_tag(stream, FIFF_MNE_CH_NAME_LIST, t_pTag);
782 names = t_pTag->toString();
783 mne_string_to_name_list(names,list,nlist);
791 int mne_read_bad_channel_list(
const QString& name, QStringList& listp,
int& nlistp)
802 res = mne_read_bad_channel_list_from_node(stream,stream->dirtree(),listp,nlistp);
818 if (mat->
coding == FIFFTS_MC_RCS) {
819 for (i = 0; i < mat->
m; i++) {
821 for (j = mat->
ptrs[i]; j < mat->ptrs[i+1]; j++)
822 res[i] += mat->
data[j]*vector[mat->
inds[j]];
826 else if (mat->
coding == FIFFTS_MC_CCS) {
827 for (i = 0; i < mat->
m; i++)
829 for (i = 0; i < mat->
n; i++)
830 for (j = mat->
ptrs[i]; j < mat->ptrs[i+1]; j++)
831 res[mat->
inds[j]] += mat->
data[j]*vector[i];
835 printf(
"mne_sparse_vec_mult2: unknown sparse matrix storage type: %d",mat->
coding);
851 if (mat->
coding == FIFFTS_MC_RCS) {
852 for (i = 0; i < mat->
m; i++) {
853 for (
k = 0;
k < ncol;
k++) {
855 for (j = mat->
ptrs[i]; j < mat->ptrs[i+1]; j++)
856 val += mat->
data[j]*mult[mat->
inds[j]][
k];
861 else if (mat->
coding == FIFFTS_MC_CCS) {
862 for (
k = 0;
k < ncol;
k++) {
863 for (i = 0; i < mat->
m; i++)
865 for (i = 0; i < mat->
n; i++)
866 for (j = mat->
ptrs[i]; j < mat->ptrs[i+1]; j++)
867 res[mat->
inds[j]][
k] += mat->
data[j]*mult[i][
k];
871 printf(
"mne_sparse_mat_mult2: unknown sparse matrix storage type: %d",mat->
coding);
877 #define APPROX_RING_BUF_SIZE (600*1024*1024)
879 static int approx_ring_buf_size = APPROX_RING_BUF_SIZE;
885 MneRawData::MneRawData()
896 ,first_sample_val(NULL)
900 ,comp_file(MNE_CTFV_NOGRAD)
901 ,comp_now(MNE_CTFV_NOGRAD)
904 ,filter_data_free(NULL)
924 this->stream->close();
925 this->filename.clear();
926 this->ch_names.clear();
928 MneRawBufDef::free_bufs(this->bufs,this->nbuf);
929 mne_free_ring_buffer_36(this->ring);
931 MneRawBufDef::free_bufs(this->filt_bufs,this->nfilt_buf);
932 mne_free_ring_buffer_36(this->filt_ring);
936 this->badlist.clear();
937 FREE_36(this->first_sample_val);
939 FREE_36(this->offsets);
945 if (this->filter_data_free)
946 this->filter_data_free(this->filter_data);
948 this->user_free(this->user);
949 this->dig_trigger.clear();
950 mne_free_event_list_36(this->event_list);
956 delete this->deriv_matched;
957 FREE_36(this->deriv_offsets);
962 void MneRawData::mne_raw_add_filter_response(
MneRawData *data,
int *highpass_effective)
972 if (data->filter_data_free)
973 data->filter_data_free(data->filter_data);
974 data->filter_data = NULL;
975 data->filter_data_free = NULL;
984 mne_create_filter_response(data->filter,
987 &data->filter_data_free,
993 void MneRawData::setup_filter_bufs(
MneRawData *data)
1004 int highpass_effective;
1006 MneRawBufDef::free_bufs(data->filt_bufs,data->nfilt_buf);
1007 data->filt_bufs = NULL;
1008 data->nfilt_buf = 0;
1009 mne_free_ring_buffer(data->filt_ring);
1010 data->filt_ring = NULL;
1012 if (!data || !data->filter)
1014 filter = data->filter;
1016 for (nfilt_buf = 0, firstsamp = data->first_samp-filter->taper_size;
1017 firstsamp < data->nsamp + data->first_samp;
1018 firstsamp = firstsamp + filter->size)
1021 printf(
"%d filter buffers needed\n",nfilt_buf);
1024 for (
k = 0, firstsamp = data->first_samp-filter->taper_size;
k < nfilt_buf;
k++,
1025 firstsamp = firstsamp + filter->size) {
1026 bufs[
k].ns = filter->size + 2*filter->taper_size;
1027 bufs[
k].firsts = firstsamp;
1028 bufs[
k].lasts = firstsamp + bufs[
k].ns - 1;
1030 bufs[
k].nchan = data->info->nchan;
1031 bufs[
k].is_skip = FALSE;
1032 bufs[
k].vals = NULL;
1033 bufs[
k].valid = FALSE;
1034 bufs[
k].ch_filtered = MALLOC_36(data->info->nchan,
int);
1035 bufs[
k].comp_status = MNE_CTFV_NOGRAD;
1037 for (j = 0; j < data->info->nchan; j++)
1038 bufs[
k].ch_filtered[j] = FALSE;
1040 data->filt_bufs = bufs;
1041 data->nfilt_buf = nfilt_buf;
1042 nring_buf = approx_ring_buf_size/((2*filter->taper_size+filter->size)*
1043 data->info->nchan*
sizeof(
float));
1044 data->filt_ring = mne_initialize_ring(nring_buf);
1045 mne_raw_add_filter_response(data,&highpass_effective);
1058 printf(
"Cannot load a skip");
1063 mne_allocate_from_ring(data->ring, buf->nchan,buf->ns,&buf->vals);
1069 printf(
"Read buffer %d .. %d\n",buf->firsts,buf->lasts);
1072 if (mne_read_raw_buffer_t(data->stream,
1083 buf->comp_status = data->comp_file;
1098 if (!data->comp->undo && !data->comp->current)
1100 if (buf->comp_status == data->comp_now)
1107 if (data->comp->undo) {
1108 temp = data->comp->current;
1109 data->comp->current = data->comp->undo;
1110 data->comp->undo = temp;
1114 if (MneCTFCompDataSet::mne_apply_ctf_comp_t(data->comp,FALSE,buf->vals,data->info->nchan,buf->ns) != OK) {
1115 temp = data->comp->undo;
1116 data->comp->undo = data->comp->current;
1117 data->comp->current = temp;
1120 temp = data->comp->undo;
1121 data->comp->undo = data->comp->current;
1122 data->comp->current = temp;
1124 if (data->comp->current) {
1128 if (MneCTFCompDataSet::mne_apply_ctf_comp_t(data->comp,TRUE,buf->vals,data->info->nchan,buf->ns) != OK)
1131 buf->comp_status = data->comp_now;
1145 int k,s,p,start,c,fills;
1151 float **deriv_vals = NULL;
1155 if (firsts < data->first_samp) {
1156 for (s = 0, p = firsts; p < data->first_samp; s++, p++) {
1158 for (c = 0; c < sel->nchan; c++)
1161 for (c = 0; c < data->info->nchan; c++)
1165 firsts = data->first_samp;
1173 for (c = 0, need_some = FALSE; c < sel->nchan; c++) {
1174 if (sel->pick[c] >= 0 || sel->pick_deriv[c] >= 0) {
1185 for (
k = 0, this_buf = data->bufs, s = 0; k < data->nbuf;
k++, this_buf++) {
1186 if (this_buf->lasts >= firsts) {
1187 start = firsts - this_buf->firsts;
1190 if (this_buf->is_skip) {
1191 for (p = start; p < this_buf->ns && ns > 0; p++, ns--, s++) {
1193 for (c = 0; c < sel->nchan; c++)
1194 if (sel->pick[c] >= 0)
1198 for (c = 0; c < data->info->nchan; c++)
1207 if (load_one_buffer(data,this_buf) != OK)
1212 if (compensate_buffer(data,this_buf) != OK)
1219 if (sel->nderiv > 0 && data->deriv_matched) {
1220 if (deriv_ns < this_buf->ns || nderiv != data->deriv_matched->deriv_data->nrow) {
1221 FREE_CMATRIX_36(deriv_vals);
1222 deriv_vals = ALLOC_CMATRIX_36(data->deriv_matched->deriv_data->nrow,this_buf->ns);
1223 nderiv = data->deriv_matched->deriv_data->nrow;
1224 deriv_ns = this_buf->ns;
1226 if (mne_sparse_mat_mult2(data->deriv_matched->deriv_data->data,this_buf->vals,this_buf->ns,deriv_vals) == FAIL) {
1227 FREE_CMATRIX_36(deriv_vals);
1231 for (c = 0; c < sel->nchan; c++) {
1235 if (sel->pick[c] >= 0) {
1236 values = this_buf->vals[sel->pick[c]];
1237 for (p = start, s2 = s, ns2 = ns; p < this_buf->ns && ns2 > 0; p++, ns2--, s2++)
1238 picked[c][s2] = values[p];
1243 else if (sel->pick_deriv[c] >= 0 && data->deriv_matched) {
1244 values = deriv_vals[sel->pick_deriv[c]];
1245 for (p = start, s2 = s, ns2 = ns; p < this_buf->ns && ns2 > 0; p++, ns2--, s2++)
1246 picked[c][s2] = values[p];
1251 for (c = 0; c < data->info->nchan; c++)
1252 for (p = start, s2 = s, ns2 = ns; p < this_buf->ns && ns2 > 0; p++, ns2--, s2++)
1253 picked[c][s2] = this_buf->vals[c][p];
1267 for (; ns > 0; ns--, s++) {
1269 for (c = 0; c < sel->nchan; c++)
1270 picked[c][s] = picked[c][fills];
1272 for (c = 0; c < data->info->nchan; c++)
1273 picked[c][s] = picked[c][fills];
1277 for (; ns > 0; ns--, s++) {
1279 for (c = 0; c < sel->nchan; c++)
1282 for (c = 0; c < data->info->nchan; c++)
1286 FREE_CMATRIX_36(deriv_vals);
1297 int k,s,p,start,c,fills;
1301 float *deriv_pvalues = NULL;
1303 if (!data->proj || (sel && !MneProjOp::mne_proj_op_affect(data->proj,sel->chspick,sel->nchan) && !MneProjOp::mne_proj_op_affect(data->proj,sel->chspick_nospace,sel->nchan)))
1304 return mne_raw_pick_data(data,sel,firsts,ns,picked);
1306 if (firsts < data->first_samp) {
1307 for (s = 0, p = firsts; p < data->first_samp; s++, p++) {
1309 for (c = 0; c < sel->nchan; c++)
1312 for (c = 0; c < data->info->nchan; c++)
1316 firsts = data->first_samp;
1320 pvalues = MALLOC_36(data->info->nchan,
float);
1321 for (
k = 0, this_buf = data->bufs; k < data->nbuf;
k++, this_buf++) {
1322 if (this_buf->lasts >= firsts) {
1323 start = firsts - this_buf->firsts;
1326 if (this_buf->is_skip) {
1327 for (p = start; p < this_buf->ns && ns > 0; p++, ns--, s++) {
1329 for (c = 0; c < sel->nchan; c++)
1330 if (sel->pick[c] >= 0)
1334 for (c = 0; c < data->info->nchan; c++)
1343 if (load_one_buffer(data,this_buf) != OK)
1348 if (compensate_buffer(data,this_buf) != OK)
1353 values = this_buf->vals;
1354 if (sel && sel->nderiv > 0 && data->deriv_matched)
1355 deriv_pvalues = MALLOC_36(data->deriv_matched->deriv_data->nrow,
float);
1356 for (p = start; p < this_buf->ns && ns > 0; p++, ns--, s++) {
1357 for (c = 0; c < data->info->nchan; c++)
1358 pvalues[c] = values[c][p];
1359 if (MneProjOp::mne_proj_op_proj_vector(data->proj,pvalues,data->info->nchan,TRUE) != OK)
1360 qWarning()<<
"Error";
1362 if (sel->nderiv > 0 && data->deriv_matched) {
1363 if (mne_sparse_vec_mult2(data->deriv_matched->deriv_data->data,pvalues,deriv_pvalues) == FAIL)
1366 for (c = 0; c < sel->nchan; c++) {
1370 if (sel->pick[c] >= 0)
1371 picked[c][s] = pvalues[sel->pick[c]];
1375 else if (sel->pick_deriv[c] >= 0 && data->deriv_matched)
1376 picked[c][s] = deriv_pvalues[sel->pick_deriv[c]];
1380 for (c = 0; c < data->info->nchan; c++) {
1381 picked[c][s] = pvalues[c];
1390 FREE_36(deriv_pvalues);
1397 for (; ns > 0; ns--, s++) {
1399 for (c = 0; c < sel->nchan; c++)
1400 picked[c][s] = picked[c][fills];
1402 for (c = 0; c < data->info->nchan; c++)
1403 picked[c][s] = picked[c][fills];
1407 for (; ns > 0; ns--, s++) {
1409 for (c = 0; c < sel->nchan; c++)
1412 for (c = 0; c < data->info->nchan; c++)
1433 mne_allocate_from_ring(data->filt_ring, buf->nchan, buf->ns,&buf->vals);
1438 vals = MALLOC_36(buf->nchan,
float *);
1439 for (
k = 0;
k < buf->nchan;
k++) {
1440 buf->ch_filtered[
k] = FALSE;
1441 vals[
k] = buf->vals[
k] + data->filter->taper_size;
1444 res = mne_raw_pick_data_proj(data,NULL,buf->firsts + data->filter->taper_size,buf->ns - 2*data->filter->taper_size,vals);
1450 printf(
"Loaded filtered buffer %d...%d %d %d last = %d\n",
1451 buf->firsts,buf->lasts,buf->lasts-buf->firsts+1,buf->ns,data->first_samp + data->nsamp);
1453 buf->valid = res == OK;
1465 int bs1,bs2,s1,s2,lasts;
1468 float **deriv_vals = NULL;
1475 if (!data->filter || !data->filter->filter_on)
1476 return mne_raw_pick_data_proj(data,sel,firsts,ns,picked);
1479 for (s = 0; s < ns; s++)
1480 for (c = 0; c < sel->nchan; c++)
1484 for (s = 0; s < ns; s++)
1485 for (c = 0; c < data->info->nchan; c++)
1488 lasts = firsts + ns - 1;
1492 if (data->first_sample_val) {
1493 dc = MALLOC_36(data->info->nchan,
float);
1495 for (
k = 0;
k < data->info->nchan;
k++)
1496 dc[
k] = data->first_sample_val[
k];
1500 if (data->comp && data->comp->current)
1501 if (MneCTFCompDataSet::mne_apply_ctf_comp(data->comp,TRUE,dc,data->info->nchan,NULL,0) != OK)
1504 if (MneProjOp::mne_proj_op_proj_vector(data->proj,dc,data->info->nchan,TRUE) != OK)
1507 filter_was = data->filter->filter_on;
1511 for (
k = 0, this_buf = data->filt_bufs; k < data->nfilt_buf;
k++, this_buf++) {
1512 if (this_buf->lasts >= firsts)
1515 for (;
k < data->nfilt_buf && this_buf->firsts <= lasts;
k++, this_buf++) {
1517 printf(
"this_buf (%d): %d..%d\n",
k,this_buf->firsts,this_buf->lasts);
1522 if (load_one_filt_buf(data,this_buf) != OK)
1528 for (c = 0; c < sel->nchan; c++) {
1529 if (sel->pick[c] >= 0) {
1530 if (!this_buf->ch_filtered[sel->pick[c]]) {
1535 if (data->info->chInfo[sel->pick[c]].kind == FIFFV_STIM_CH)
1536 data->filter->filter_on = FALSE;
1538 dc_offset = dc[sel->pick[c]];
1539 if (mne_apply_filter(data->filter,data->filter_data,this_buf->vals[sel->pick[c]],this_buf->ns,TRUE,
1540 dc_offset,data->info->chInfo[sel->pick[c]].kind) != OK) {
1541 data->filter->filter_on = filter_was;
1544 this_buf->ch_filtered[sel->pick[c]] = TRUE;
1545 data->filter->filter_on = filter_was;
1552 if (sel->nderiv > 0 && data->deriv_matched) {
1553 MneDeriv* der = data->deriv_matched;
1554 for (c = 0; c < der->deriv_data->ncol; c++) {
1555 if (der->in_use[c] > 0 &&
1556 !this_buf->ch_filtered[c]) {
1561 if (data->info->chInfo[c].kind == FIFFV_STIM_CH)
1562 data->filter->filter_on = FALSE;
1565 if (mne_apply_filter(data->filter,data->filter_data,this_buf->vals[c],this_buf->ns,TRUE,
1566 dc_offset,data->info->chInfo[c].kind) != OK) {
1567 data->filter->filter_on = filter_was;
1570 this_buf->ch_filtered[c] = TRUE;
1571 data->filter->filter_on = filter_was;
1580 for (c = 0; c < data->info->nchan; c++) {
1581 if (!this_buf->ch_filtered[c]) {
1586 if (data->info->chInfo[c].kind == FIFFV_STIM_CH)
1587 data->filter->filter_on = FALSE;
1590 if (mne_apply_filter(data->filter,data->filter_data,this_buf->vals[c],this_buf->ns,TRUE,
1591 dc_offset,data->info->chInfo[c].kind) != OK) {
1592 data->filter->filter_on = filter_was;
1595 this_buf->ch_filtered[c] = TRUE;
1596 data->filter->filter_on = filter_was;
1603 if (firsts >= this_buf->firsts) {
1604 bs1 = firsts - this_buf->firsts;
1609 s1 = this_buf->firsts - firsts;
1611 if (lasts >= this_buf->lasts) {
1613 s2 = this_buf->lasts - lasts + ns;
1616 bs2 = lasts - this_buf->lasts + this_buf->ns;
1620 printf(
"buf : %d..%d %d\n",bs1,bs2,bs2-bs1);
1621 printf(
"dest : %d..%d %d\n",s1,s2,s2-s1);
1627 if (sel->nderiv > 0 && data->deriv_matched) {
1631 if (deriv_ns < this_buf->ns || nderiv != data->deriv_matched->deriv_data->nrow) {
1632 FREE_CMATRIX_36(deriv_vals);
1633 deriv_vals = ALLOC_CMATRIX_36(data->deriv_matched->deriv_data->nrow,this_buf->ns);
1634 nderiv = data->deriv_matched->deriv_data->nrow;
1635 deriv_ns = this_buf->ns;
1637 if (mne_sparse_mat_mult2(data->deriv_matched->deriv_data->data,this_buf->vals,this_buf->ns,deriv_vals) == FAIL)
1640 for (c = 0; c < sel->nchan; c++) {
1644 if (sel->pick[c] >= 0) {
1645 values = this_buf->vals[sel->pick[c]];
1646 for (s = s1, bs = bs1; s < s2; s++, bs++)
1647 picked[c][s] += values[bs];
1649 else if (sel->pick_deriv[c] >= 0 && data->deriv_matched) {
1650 values = deriv_vals[sel->pick_deriv[c]];
1651 for (s = s1, bs = bs1; s < s2; s++, bs++)
1652 picked[c][s] += values[bs];
1657 for (c = 0; c < data->info->nchan; c++) {
1658 values = this_buf->vals[c];
1659 for (s = s1, bs = bs1; s < s2; s++, bs++)
1660 picked[c][s] += values[bs];
1665 FREE_CMATRIX_36(deriv_vals);
1670 FREE_CMATRIX_36(deriv_vals);
1678 MneRawData *MneRawData::mne_raw_open_file_comp(
const QString& name,
1680 int allow_maxshield,
1695 QList<FiffDirEntry::SPtr> dir0;
1700 int k, b, nbuf, ndir, nnames;
1701 int current_dir0 = 0;
1705 if (MneRawInfo::mne_load_raw_info(name,allow_maxshield,&info) == FAIL)
1708 for (
k = 0;
k < info->
nchan;
k++) {
1710 if (QString::compare(ch.
ch_name,MNE_DEFAULT_TRIGGER_CH) == 0) {
1711 if (std::fabs(1.0 - ch.
range) > 1e-5) {
1713 printf(
"%s range set to %f\n",MNE_DEFAULT_TRIGGER_CH,ch.
range);
1721 printf(
"Ch %s unit multiplier %d -> 0\n",ch.
ch_name.toLatin1().data(),ch.
unit_mul);
1731 data->filename = name;
1732 data->stream = stream;
1737 mne_channel_names_to_name_list(info->
chInfo,
1741 if (nnames != info->
nchan) {
1742 printf(
"Channel names were not translated correctly into a name list");
1748 data->comp = MneCTFCompDataSet::mne_read_ctf_comp_data(data->filename);
1750 if (data->comp->ncomp > 0)
1751 printf(
"Read %d compensation data sets from %s\n",data->comp->ncomp,data->filename.toUtf8().constData());
1753 printf(
"No compensation data in %s\n",data->filename.toUtf8().constData());
1756 qWarning() <<
"err_print_error()";
1757 if ((data->comp_file = MneCTFCompDataSet::mne_get_ctf_comp(data->info->chInfo,data->info->nchan)) == FAIL)
1759 printf(
"Compensation in file : %s\n",MneCTFCompDataSet::mne_explain_ctf_comp(MneCTFCompDataSet::mne_map_ctf_comp_kind(data->comp_file)));
1761 data->comp_now = data->comp_file;
1763 data->comp_now = comp_set;
1769 QList<FIFFLIB::FiffChInfo>(),
1777 printf(
"SSS data read from %s :\n",data->filename.toUtf8().constData());
1778 data->sss->print(stderr);
1781 printf(
"No SSS data in %s\n",data->filename.toUtf8().constData());
1789 dir0 = data->info->rawDir;
1790 ndir = data->info->ndir;
1797 if (!stream->read_tag(t_pTag,dir0[current_dir0]->pos))
1799 data->first_samp = *t_pTag->toInt();
1807 if (!stream->read_tag(t_pTag,dir0[current_dir0]->pos))
1809 nsamp_skip = data->info->buf_size*(*t_pTag->toInt());
1810 printf(
"Data skip of %d samples in the beginning\n",nsamp_skip);
1816 if (!stream->read_tag(t_pTag,dir0[current_dir0]->pos))
1818 data->first_samp += *t_pTag->toInt();
1823 data->omit_samp = data->first_samp + nsamp_skip;
1824 data->omit_samp_old = nsamp_skip;
1825 data->first_samp = 0;
1828 data->first_samp = data->first_samp + nsamp_skip;
1831 else if (omit_skip) {
1832 data->omit_samp = data->first_samp;
1833 data->first_samp = 0;
1836 printf(
"data->first_samp = %d\n",data->first_samp);
1842 for (
k = 0, nbuf = 0;
k < ndir;
k++)
1849 for (
k = 0, nbuf = 0;
k < ndir;
k++)
1853 bufs[nbuf].ent = dir0[
k];
1854 bufs[nbuf].nchan = data->info->nchan;
1856 bufs[nbuf].vals = NULL;
1857 bufs[nbuf].valid = FALSE;
1858 bufs[nbuf].ch_filtered = NULL;
1859 bufs[nbuf].comp_status = data->comp_file;
1865 for (
k = 0;
k < nbuf;
k++) {
1868 if (dir->type == FIFFT_DAU_PACK16 || dir->type == FIFFT_SHORT)
1869 bufs[
k].ns = dir->size/(data->info->nchan*
sizeof(fiff_dau_pack16_t));
1870 else if (dir->type == FIFFT_FLOAT)
1871 bufs[
k].ns = dir->size/(data->info->nchan*
sizeof(fiff_float_t));
1872 else if (dir->type == FIFFT_INT)
1873 bufs[
k].ns = dir->size/(data->info->nchan*
sizeof(fiff_int_t));
1875 printf(
"We are not prepared to handle raw data type: %d",dir->type);
1882 if (!stream->read_tag(t_pTag,dir->pos))
1884 bufs[
k].ns = data->info->buf_size*(*t_pTag->toInt());
1886 bufs[
k].firsts =
k == 0 ? data->first_samp : bufs[
k-1].lasts + 1;
1887 bufs[
k].lasts = bufs[
k].firsts + bufs[
k].ns - 1;
1888 data->nsamp += bufs[
k].ns;
1894 data->bad = MALLOC_36(data->info->nchan,
int);
1895 data->offsets = MALLOC_36(data->info->nchan,
float);
1896 for (
k = 0;
k < data->info->nchan;
k++) {
1897 data->bad[
k] = FALSE;
1898 data->offsets[
k] = 0.0;
1904 if (mne_read_bad_channel_list(name,data->badlist,data->nbad) == OK) {
1905 for (b = 0; b < data->nbad; b++) {
1906 for (
k = 0;
k < data->info->nchan;
k++) {
1907 if (QString::compare(data->info->chInfo[
k].ch_name,data->badlist[b],Qt::CaseInsensitive) == 0) {
1908 data->bad[
k] = TRUE;
1913 printf(
"%d bad channels read from %s%s",data->nbad,name.toUtf8().constData(),data->nbad > 0 ?
":\n" :
"\n");
1914 if (data->nbad > 0) {
1916 for (
k = 0;
k < data->nbad;
k++)
1917 printf(
"%s%c",data->badlist[
k].toUtf8().constData(),k < data->nbad-1 ?
' ' :
'\n');
1924 nbuf = approx_ring_buf_size/(data->info->buf_size*data->info->nchan*
sizeof(float));
1925 data->ring = mne_initialize_ring(nbuf);
1930 *data->filter = *filter;
1931 setup_filter_bufs(data);
1934 float **vals = ALLOC_CMATRIX_36(data->info->nchan,1);
1936 if (mne_raw_pick_data(data,NULL,data->first_samp,1,vals) == FAIL)
1938 data->first_sample_val = MALLOC_36(data->info->nchan,
float);
1939 for (
k = 0;
k < data->info->nchan;
k++)
1940 data->first_sample_val[
k] = vals[
k][0];
1941 FREE_CMATRIX_36(vals);
1942 printf(
"Initial dc offsets determined\n");
1944 printf(
"Raw data file %s:\n",name.toUtf8().constData());
1945 printf(
"\tnchan = %d\n",data->info->nchan);
1946 printf(
"\tnsamp = %d\n",data->nsamp);
1947 printf(
"\tsfreq = %-8.3f Hz\n",data->info->sfreq);
1948 printf(
"\tlength = %-8.3f sec\n",data->nsamp/data->info->sfreq);
1965 MneRawData *MneRawData::mne_raw_open_file(
const QString& name,
int omit_skip,
int allow_maxshield,
mneFilterDef filter)
1970 return mne_raw_open_file_comp(name,omit_skip,allow_maxshield,filter,-1);