0001 function [data] = fiff_read_evoked(fname,setno)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 global FIFF;
0053 if isempty(FIFF)
0054 FIFF = fiff_define_constants();
0055 end
0056
0057 me='MNE:fiff_read_evoked';
0058
0059 if nargin == 1
0060 setno = 1;
0061 elseif nargin ~= 2
0062 error(me,'Incorrect number of arguments');
0063 end
0064 if setno <= 0
0065 error(me,'Data set selector must be positive');
0066 end
0067
0068
0069
0070 fprintf(1,'Reading %s ...\n',fname);
0071 [ fid, tree ] = fiff_open(fname);
0072
0073
0074
0075 [ info, meas ] = fiff_read_meas_info(fid,tree);
0076 info.filename = fname;
0077
0078
0079
0080 processed = fiff_dir_tree_find(meas,FIFF.FIFFB_PROCESSED_DATA);
0081 if length(processed) == 0
0082 fclose(fid);
0083 error(me,'Could not find processed data');
0084 end
0085
0086 evoked = fiff_dir_tree_find(meas,FIFF.FIFFB_EVOKED);
0087 if length(evoked) == 0
0088 fclose(fid);
0089 error(me,'Could not find evoked data');
0090 end
0091
0092
0093
0094 naspect = 0;
0095 is_smsh = [];
0096 for k = 1:length(evoked)
0097 sets(k).aspects = fiff_dir_tree_find(evoked(k),FIFF.FIFFB_ASPECT);
0098 sets(k).naspect = length(sets(k).aspects);
0099 if sets(k).naspect > 0
0100 is_smsh = [ is_smsh zeros(1,sets(k).naspect) ];
0101 naspect = naspect + sets(k).naspect;
0102 end
0103 saspects = fiff_dir_tree_find(evoked(k), FIFF.FIFFB_SMSH_ASPECT);
0104 nsaspects = length(saspects);
0105 if nsaspects > 0
0106 sets(k).naspect = sets(k).naspect + nsaspects;
0107 sets(k).aspects = [ sets(k).aspects saspects ];
0108 is_smsh = [ is_smsh ones(1,sets(k).naspect) ];
0109 naspect = naspect + nsaspects;
0110 end
0111 end
0112 fprintf(1,'\t%d evoked data sets containing a total of %d data aspects in %s\n',length(evoked),naspect,fname);
0113 if setno > naspect || setno < 1
0114 fclose(fid);
0115 error(me,'Data set selector out of range');
0116 end
0117
0118
0119
0120 p = 0;
0121 goon = true;
0122 for k = 1:length(evoked)
0123 for a = 1:sets(k).naspect
0124 p = p + 1;
0125 if p == setno
0126 my_evoked = evoked(k);
0127 my_aspect = sets(k).aspects(a);
0128 goon = false;
0129 break;
0130 end
0131 end
0132 if ~goon
0133 break;
0134 end
0135 end
0136
0137
0138
0139 if ~exist('my_evoked','var') || ~exist('my_aspect','var')
0140 fclose(fid);
0141 error(me,'Desired data set not found');
0142 end
0143
0144
0145
0146 nchan = 0;
0147 sfreq = -1;
0148 q = 0;
0149 for k = 1:my_evoked.nent
0150 kind = my_evoked.dir(k).kind;
0151 pos = my_evoked.dir(k).pos;
0152 switch kind
0153 case FIFF.FIFF_COMMENT
0154 tag = fiff_read_tag(fid,pos);
0155 comment = tag.data;
0156 case FIFF.FIFF_FIRST_SAMPLE
0157 tag = fiff_read_tag(fid,pos);
0158 first = tag.data;
0159 case FIFF.FIFF_LAST_SAMPLE
0160 tag = fiff_read_tag(fid,pos);
0161 last = tag.data;
0162 case FIFF.FIFF_NCHAN
0163 tag = fiff_read_tag(fid,pos);
0164 nchan = tag.data;
0165 case FIFF.FIFF_SFREQ
0166 tag = fiff_read_tag(fid,pos);
0167 sfreq = tag.data;
0168 case FIFF.FIFF_CH_INFO
0169 q = q+1;
0170 tag = fiff_read_tag(fid,pos);
0171 chs(q) = tag.data;
0172 end
0173 end
0174 if ~exist('comment','var')
0175 comment = 'No comment';
0176 end
0177
0178
0179
0180 if nchan > 0
0181 if ~exist('chs','var')
0182 fclose(fid);
0183 error(me, ...
0184 'Local channel information was not found when it was expected.');
0185 end
0186 if length(chs) ~= nchan
0187 fclose(fid);
0188 error(me, ...
0189 'Number of channels and number of channel definitions are different');
0190 end
0191 info.chs = chs;
0192 info.nchan = nchan;
0193 fprintf(1, ...
0194 '\tFound channel information in evoked data. nchan = %d\n',nchan);
0195 if sfreq > 0
0196 info.sfreq = sfreq;
0197 end
0198 end
0199 nsamp = last-first+1;
0200 fprintf(1,'\tFound the data of interest:\n');
0201 fprintf(1,'\t\tt = %10.2f ... %10.2f ms (%s)\n',...
0202 1000*double(first)/info.sfreq,1000*double(last)/info.sfreq,comment);
0203 if ~isempty(info.comps)
0204 fprintf(1,'\t\t%d CTF compensation matrices available\n',length(info.comps));
0205 end
0206
0207
0208
0209 nepoch = 0;
0210 for k = 1:my_aspect.nent
0211 kind = my_aspect.dir(k).kind;
0212 pos = my_aspect.dir(k).pos;
0213 switch kind
0214 case FIFF.FIFF_COMMENT
0215 tag = fiff_read_tag(fid,pos);
0216 comment = tag.data;
0217 case FIFF.FIFF_ASPECT_KIND
0218 tag = fiff_read_tag(fid,pos);
0219 aspect_kind = tag.data;
0220 case FIFF.FIFF_NAVE
0221 tag = fiff_read_tag(fid,pos);
0222 nave = tag.data;
0223 case FIFF.FIFF_EPOCH
0224 nepoch = nepoch + 1;
0225 tag = fiff_read_tag(fid,pos);
0226 epoch(nepoch) = tag;
0227 end
0228 end
0229 if ~exist('nave','var')
0230 nave = 1;
0231 end
0232 fprintf(1,'\t\tnave = %d aspect type = %d\n',...
0233 nave,aspect_kind);
0234 if nepoch ~= 1 && nepoch ~= info.nchan
0235 fclose(fid);
0236 error(me,'Number of epoch tags is unreasonable (nepoch = %d nchan = %d)',nepoch,info.nchan);
0237 end
0238
0239 if nepoch == 1
0240
0241
0242
0243 all = epoch(1).data;
0244
0245
0246
0247 if size(all,2) == 1 && info.nchan == 1
0248 all = all';
0249 end
0250 else
0251
0252
0253
0254 all = epoch(1).data';
0255 for k = 2:nepoch
0256 all = [ all ; epoch(k).data' ];
0257 end
0258 end
0259 if size(all,2) ~= nsamp
0260 fclose(fid);
0261 error(me,'Incorrect number of samples (%d instead of %d)',...
0262 size(all,2),nsamp);
0263 end
0264
0265
0266
0267 for k = 1:info.nchan
0268 cals(k) = info.chs(k).cal;
0269 end
0270 all = diag(cals)*all;
0271
0272
0273
0274 data.info = info;
0275 data.evoked.aspect_kind = aspect_kind;
0276 data.evoked.is_smsh = is_smsh(setno);
0277 if exist('nave','var')
0278 data.evoked.nave = nave;
0279 else
0280 data.evoked.nave = 1;
0281 end
0282 data.evoked.first = first;
0283 data.evoked.last = last;
0284 if exist('comment','var')
0285 data.evoked.comment = comment;
0286 end
0287
0288
0289
0290 data.evoked.times = double(data.evoked.first:1:data.evoked.last)/data.info.sfreq;
0291 data.evoked.epochs = all;
0292
0293 fclose(fid);
0294
0295 return;
0296
0297 function [tag] = find_tag(node,findkind)
0298
0299 for p = 1:node.nent
0300 kind = node.dir(p).kind;
0301 pos = node.dir(p).pos;
0302 if kind == findkind
0303 tag = fiff_read_tag(fid,pos);
0304 return;
0305 end
0306 end
0307 tag = [];
0308 return
0309 end
0310
0311 end
0312
0313