Home > vbmeg > external > mne > fiff_read_raw_segment.m

fiff_read_raw_segment

PURPOSE ^

SYNOPSIS ^

function [data,times] = fiff_read_raw_segment(raw,from,to,sel)

DESCRIPTION ^

 [data,times] = fiff_read_raw_segment(raw,from,to,sel)

 Read a specific raw data segment

 raw    - structure returned by fiff_setup_read_raw
 from   - first sample to include. If omitted, defaults to the
          first sample in data
 to     - last sample to include. If omitted, defaults to the last
          sample in data
 sel    - optional channel selection vector

 data   - returns the data matrix (channels x samples)
 times  - returns the time values corresponding to the samples (optional)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data,times] = fiff_read_raw_segment(raw,from,to,sel)
0002 %
0003 % [data,times] = fiff_read_raw_segment(raw,from,to,sel)
0004 %
0005 % Read a specific raw data segment
0006 %
0007 % raw    - structure returned by fiff_setup_read_raw
0008 % from   - first sample to include. If omitted, defaults to the
0009 %          first sample in data
0010 % to     - last sample to include. If omitted, defaults to the last
0011 %          sample in data
0012 % sel    - optional channel selection vector
0013 %
0014 % data   - returns the data matrix (channels x samples)
0015 % times  - returns the time values corresponding to the samples (optional)
0016 %
0017 
0018 %
0019 %   Author : Matti Hamalainen, MGH Martinos Center
0020 %   License : BSD 3-clause
0021 %
0022 %   Revision 1.9  2007/11/22 05:04:25  msh
0023 %   Fixed help text and some error checking
0024 %
0025 %   Revision 1.8  2006/04/23 15:29:40  msh
0026 %   Added MGH to the copyright
0027 %
0028 %   Revision 1.7  2006/04/21 22:11:34  msh
0029 %   Times calculation needed casting to double
0030 %
0031 %   Revision 1.6  2006/04/21 22:03:43  msh
0032 %   Fixed time limit output.
0033 %
0034 %   Revision 1.5  2006/04/21 16:17:48  msh
0035 %   Added handling of CTF compensation
0036 %
0037 %   Revision 1.4  2006/04/21 15:11:56  msh
0038 %   Made the debug output optional.
0039 %
0040 %   Revision 1.3  2006/04/21 14:43:59  msh
0041 %   Report projection operators.
0042 %   Some more checks in raw data reading.
0043 %
0044 %   Revision 1.2  2006/04/21 14:23:16  msh
0045 %   Further improvements in raw data reading
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 %  Initial checks
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 %  Initialize the data and calibration vector
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     %  Do we need this buffer
0136     %
0137     if this.last > from
0138         if isempty(this.ent)
0139             %
0140             %  Take the easy route: skip is translated to zeros
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             %   Depending on the state of the projection and selection
0155             %   we proceed a little bit differently
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         %  The picking logic is a bit complicated
0170         %
0171         if to >= this.last && from <= this.first
0172             %
0173             %    We need the whole buffer
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                 %   Something from the middle
0185                 %
0186                 last_pick = this.nsamp + to - this.last;
0187                 if do_debug
0188                     fprintf(1,'M');
0189                 end
0190             else
0191                 %
0192                 %   From the middle to the end
0193                 %
0194                 last_pick = this.nsamp;
0195                 if do_debug
0196                     fprintf(1,'E');
0197                 end
0198             end
0199         else
0200             %
0201             %    From the beginning to the middle
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         %   Now we are ready to pick
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     %   Done?
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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005