0001 function [epochs] = fiff_read_epochs(fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 global FIFF;
0012 if isempty(FIFF)
0013 FIFF = fiff_define_constants();
0014 end
0015
0016 me='MNE:fiff_read_epochs';
0017
0018
0019
0020
0021 fprintf(1,'Reading %s ...\n',fname);
0022 [ fid, tree ] = fiff_open(fname);
0023
0024
0025
0026 [ info, meas ] = fiff_read_meas_info(fid,tree);
0027 info.filename = fname;
0028
0029
0030
0031
0032 [ events, mappings ] = fiff_read_events(fid,tree);
0033
0034
0035
0036
0037 processed = fiff_dir_tree_find(meas, FIFF.FIFFB_PROCESSED_DATA);
0038 if length(processed) == 0
0039 fclose(fid);
0040 error(me,'Could not find epochs data');
0041 end
0042
0043 ep = fiff_dir_tree_find(meas, FIFF.FIFFB_EPOCHS);
0044 if length(ep) == 0
0045 fclose(fid);
0046 error(me,'Could not find epochs data');
0047 end
0048
0049 comment = '';
0050 selection = '';
0051 drop_log = '';
0052 for k = 1:ep.nent
0053 kind = ep.dir(k).kind;
0054 pos = ep.dir(k).pos;
0055 switch kind
0056 case FIFF.FIFF_FIRST_SAMPLE
0057 tag = fiff_read_tag(fid,pos);
0058 first = tag.data;
0059 case FIFF.FIFF_LAST_SAMPLE
0060 tag = fiff_read_tag(fid,pos);
0061 last = tag.data;
0062 case FIFF.FIFF_COMMENT
0063 tag = fiff_read_tag(fid,pos);
0064 comment = tag.data;
0065 case FIFF.FIFF_EPOCH
0066 tag = fiff_read_tag(fid,pos);
0067 epoch = tag.data;
0068 case FIFF.FIFF_MNE_BASELINE_MIN
0069 tag = fiff_read_tag(fid,pos);
0070 bmin = tag.data;
0071 case FIFF.FIFF_MNE_BASELINE_MAX
0072 tag = fiff_read_tag(fid,pos);
0073 bmax = tag.data;
0074 case FIFF.FIFFB_MNE_EPOCHS_SELECTION
0075 tag = fiff_read_tag(fid,pos);
0076 selection = tag.data;
0077 case FIFF.FIFFB_MNE_EPOCHS_DROP_LOG
0078 tag = fiff_read_tag(fid,pos);
0079 drop_log = tag.data;
0080 end
0081 end
0082
0083 if ~exist('epoch','var')
0084 fclose(fid);
0085 error(me,'Epochs data not found');
0086 end
0087
0088 if ~exist('bmin','var')
0089 bmin = double(first)/info.sfreq;
0090 end
0091
0092 if ~exist('bmax','var')
0093 bmax = double(last)/info.sfreq;
0094 end
0095
0096 baseline = [bmin, bmax];
0097
0098 nsamp = last-first+1;
0099 fprintf(1,'\tFound the data of interest:\n');
0100 fprintf(1,'\t\tt = %10.2f ... %10.2f ms (%s)\n',...
0101 1000*double(first)/info.sfreq,1000*double(last)/info.sfreq,comment);
0102 if ~isempty(info.comps)
0103 fprintf(1,'\t\t%d CTF compensation matrices available\n',length(info.comps));
0104 end
0105
0106 if size(epoch,1) ~= size(events,1)
0107 fclose(fid);
0108 error(me,'Incorrect number of trials (%d instead of %d)',...
0109 size(epoch,1),size(events,1));
0110 end
0111
0112 if size(epoch,2) ~= info.nchan
0113 fclose(fid);
0114 error(me,'Incorrect number of channels (%d instead of %d)',...
0115 size(epoch,2),info.nchan);
0116 end
0117
0118 if size(epoch,3) ~= nsamp
0119 fclose(fid);
0120 error(me,'Incorrect number of samples (%d instead of %d)',...
0121 size(epoch,3),nsamp);
0122 end
0123
0124
0125
0126
0127 for k = 1:info.nchan
0128 cals(k) = info.chs(k).cal;
0129 end
0130
0131 nepochs = size(epoch, 1);
0132 epoch = repmat(cals, [nepochs, 1, nsamp]) .* epoch;
0133
0134 times = double(first:last) / info.sfreq;
0135 tmin = times(1);
0136 tmax = times(end);
0137
0138
0139
0140
0141 epochs.info = info;
0142 epochs.events = events;
0143 epochs.name = comment;
0144 epochs.times = times;
0145 epochs.tmin = tmin;
0146 epochs.tmax = tmax;
0147 epochs.data = epoch;
0148 epochs.baseline = baseline;
0149 epochs.event_id = mappings;
0150 epochs.selection = selection;
0151 epochs.drop_log = drop_log;
0152
0153 fclose(fid);
0154
0155 return;