0001 function [data,times] = fiff_read_raw_segment(raw,from,to,sel)
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 me='MNE:fiff_read_raw_segment';
0050
0051 if nargin == 3
0052 sel = [];
0053 elseif nargin == 2
0054 to = raw.last_samp;
0055 sel = [];
0056 elseif nargin == 1
0057 from = raw.first_samp;
0058 to = raw.last_samp;
0059 sel = [];
0060 elseif nargin ~= 4
0061 error(me,'Incorrect number of arguments');
0062 end
0063
0064
0065
0066 from = double(from);
0067 to = double(to);
0068 if from < raw.first_samp
0069 from = raw.first_samp;
0070 end
0071 if to > raw.last_samp
0072 to = raw.last_samp;
0073 end
0074
0075 if from > to
0076 error(me,'No data in this range');
0077 end
0078 fprintf(1,'Reading %d ... %d = %9.3f ... %9.3f secs...', ...
0079 from,to,from/raw.info.sfreq,to/raw.info.sfreq);
0080
0081
0082
0083 nchan = raw.info.nchan;
0084 dest = 1;
0085 cal = diag(raw.cals);
0086
0087 if isempty(sel)
0088 data = zeros(nchan,to-from+1);
0089 if isempty(raw.proj) && isempty(raw.comp)
0090 mult = [];
0091 else
0092 if isempty(raw.proj)
0093 mult = raw.comp*cal;
0094 elseif isempty(raw.comp)
0095 mult = raw.proj*cal;
0096 else
0097 mult = raw.proj*raw.comp*cal;
0098 end
0099 end
0100 else
0101 data = zeros(length(sel),to-from+1);
0102 if isempty(raw.proj) && isempty(raw.comp)
0103 mult = [];
0104 cal = diag(raw.cals(sel));
0105 else
0106 if isempty(raw.proj)
0107 mult = raw.comp(sel,:)*cal;
0108 elseif isempty(raw.comp)
0109 mult = raw.proj(sel,:)*cal;
0110 else
0111 mult = raw.proj(sel,:)*raw.comp*cal;
0112 end
0113 end
0114 end
0115 do_debug=false;
0116 if ~isempty(cal)
0117 cal = sparse(cal);
0118 end
0119 if ~isempty(mult)
0120 mult = sparse(mult);
0121 end
0122
0123 if isempty(fopen(raw.fid))
0124 fid = fopen(raw.info.filename,'rb','ieee-be');
0125 if (fid < 0)
0126 error(me,'Cannot open file %s',raw.info.filename);
0127 end
0128 else
0129 fid = raw.fid;
0130 end
0131
0132 for k = 1:length(raw.rawdir)
0133 this = raw.rawdir(k);
0134
0135
0136
0137 if this.last > from
0138 if isempty(this.ent)
0139
0140
0141
0142 if do_debug
0143 fprintf(1,'S');
0144 end
0145 doing_whole = false;
0146 if isempty(sel)
0147 one = zeros(nchan,this.nsamp);
0148 else
0149 one = zeros(length(sel),this.nsamp);
0150 end
0151 else
0152 tag = fiff_read_tag(fid,this.ent.pos);
0153
0154
0155
0156
0157 if isempty(mult)
0158 if isempty(sel)
0159 one = cal*double(reshape(tag.data,nchan,this.nsamp));
0160 else
0161 one = double(reshape(tag.data,nchan,this.nsamp));
0162 one = cal*one(sel,:);
0163 end
0164 else
0165 one = mult*double(reshape(tag.data,nchan,this.nsamp));
0166 end
0167 end
0168
0169
0170
0171 if to >= this.last && from <= this.first
0172
0173
0174
0175 first_pick = 1;
0176 last_pick = this.nsamp;
0177 if do_debug
0178 fprintf(1,'W');
0179 end
0180 elseif from > this.first
0181 first_pick = from - this.first + 1;
0182 if to < this.last
0183
0184
0185
0186 last_pick = this.nsamp + to - this.last;
0187 if do_debug
0188 fprintf(1,'M');
0189 end
0190 else
0191
0192
0193
0194 last_pick = this.nsamp;
0195 if do_debug
0196 fprintf(1,'E');
0197 end
0198 end
0199 else
0200
0201
0202
0203 first_pick = 1;
0204 last_pick = to - this.first + 1;
0205 if do_debug
0206 fprintf(1,'B');
0207 end
0208 end
0209
0210
0211
0212 picksamp = last_pick - first_pick + 1;
0213 if picksamp > 0
0214 data(:,dest:dest+picksamp-1) = one(:,first_pick:last_pick);
0215 dest = dest + picksamp;
0216 end
0217 end
0218
0219
0220
0221 if this.last >= to
0222 fprintf(1,' [done]\n');
0223 break;
0224 end
0225 end
0226
0227 fclose(fid);
0228
0229 if nargout == 2
0230 times = [ from:to ];
0231 times = double(times)/raw.info.sfreq;
0232 end
0233
0234 return;
0235
0236 end