load_neuroscan.m

来自「绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX」· M 代码 · 共 1,009 行 · 第 1/4 页

M
1,009
字号
         %  multiplexed format.
         %
         ns.data(:, [ns.SETUP.pnts*(i-1)+1 : ns.SETUP.pnts*i]) = ...
		fread(fid, [ns.SETUP.nchannels, ns.SETUP.pnts], 'int16');
      end

      %  scale data
      %
      for j = 1:ns.SETUP.nchannels

         %  To scale a data point to microvolts for channel j, first subtract
         %  off the amplifier d.c.offset (if any) found in the variable
         %  (ELECTLOC[j]->baseline). Then multiply by the sensitivity
         %  (ELECTLOC[j]->sensitivity) times the channel specific scale factor
         %  (ELECTLOC[j]->calib) devided by 204.8.

         dc = ns.ELECTLOC(j).baseline;
         sf = ns.ELECTLOC(j).sensitivity * ns.ELECTLOC(j).calib / 204.8;
         ns.data(j,:) = sf*(ns.data(j,:) - dc);
      end
   else

      ns.data = read_avg(fid, ns);
   end

   ns.dataunits = 'microvolts';

   fclose(fid);

   return;					% load_neuroscan


%---------------------------------------------------------------------
function [data, start_time, end_time] = read_cnt(fid, ns)
  
   %  each data point is 2-byte for 16-bit CNT data file
   %  and is 4-byte for 32-bit CNT32 data file
   %
   if strcmpi(ns.precision, 'int32')
      byte_per_pnts = 4;
   else
      byte_per_pnts = 2;
   end;

   %  number of data scans (in bytes) for all channels
   %
   ndata = (ns.SETUP.EventTablePos - (900 + 75 * ns.SETUP.nchannels));

   %  number of data scans (in bytes) for each channel
   %
   ndata = ndata / ns.SETUP.nchannels;

   %  number of data scans (in data points) for each channel
   %
   ndata = ndata / byte_per_pnts;

   %  round to start timepoints &  end_timepoints first,
   %  then, replace start_time & end_time with actual value
   %
   if isempty(ns.start_time)
      start_pnts = 0;
   else
      start_pnts = round(ns.start_time * ns.SETUP.rate);
   end;

   start_time = start_pnts / ns.SETUP.rate;

   if isempty(ns.end_time)
      end_pnts = ndata - 1;
   else
      end_pnts = round(ns.end_time * ns.SETUP.rate);
   end

   end_time = end_pnts / ns.SETUP.rate;

   %  read data
   %

if 0	% no difference between ContinuousType to read data

   if ns.SETUP.ContinuousType == 3		% SynAmp

      %  Type 3 means using SynAmps for continuous files. Data is stored
      %  as 2-byte integer after SETUP hdr and ELECTLOC hdr, and is sent
      %  in a blocked rather than multiplexed format. The size of block 
      %  in byte is SETUP.ChannelOffset and the size of block in point
      %  is SETUP.ChannelOffset/2. Since data is recorded continuously, 
      %  block has to be concatenated each other.

      %  block size in data points should be SETUP.ChannelOffset / 2
      %  since each data point is 2-byte (16-bit short integer)
      %
      blk_pnts = ns.SETUP.ChannelOffset / byte_per_pnts;

      %  find the block for start_time and the block for end_time,
      %
      start_blk = floor(start_pnts / blk_pnts) + 1;
      end_blk = floor(end_pnts / blk_pnts) + 1;

      %  replace start_time with the beginning of start block, and
      %  end_time with the end of end block
      %
      start_pnts = (start_blk - 1) * blk_pnts;
      end_pnts = (end_blk - 1) * blk_pnts;

      start_time = start_pnts / ns.SETUP.rate;
      end_time = end_pnts / ns.SETUP.rate;

      data = zeros(ns.SETUP.nchannels, (end_pnts-start_pnts+1));

      fseek(fid, start_pnts*ns.SETUP.nchannels*byte_per_pnts, 'cof');

      for i = 1 :(end_blk-start_blk)
         data(:, ((i-1)*blk_pnts+1):(i*blk_pnts) ) = ...
		[fread(fid, [ns.SETUP.nchannels blk_pnts], ns.precision)];
      end		% for num_block

   elseif ns.SETUP.ContinuousType == 1 | ns.SETUP.ContinuousType == 0

      %  Type 0 or 1 means using 100/330KHz module for continuous files.
      %  Data is stored as 2-byte integer after SETUP hdr and ELECTLOC 
      %  hdr in multiplexed format. Each data scan consists of 
      %  SETUP.nchannels points.

end
end

      fseek(fid, start_pnts*ns.SETUP.nchannels*byte_per_pnts, 'cof');

      data = fread(fid, [ns.SETUP.nchannels, (end_pnts-start_pnts+1)], ...
			ns.precision);
   %else
    %  error('Invalid continuous NeuroScan data');
   %end		% switch ContinuousType

   %  scale data
   %
   for j = 1:ns.SETUP.nchannels

      %  To scale a data point to microvolts for channel j, first subtract
      %  off the amplifier d.c.offset (if any) found in the variable
      %  (ELECTLOC[j]->baseline). Then multiply by the sensitivity
      %  (ELECTLOC[j]->sensitivity) times the channel specific scale factor
      %  (ELECTLOC[j]->calib) devided by 204.8.

      dc = ns.ELECTLOC(j).baseline;
      sf = ns.ELECTLOC(j).sensitivity * ns.ELECTLOC(j).calib / 204.8;
      data(j,:) = sf*(data(j,:) - dc);
   end

   return;					% read_cnt


%---------------------------------------------------------------------
function data = read_avg(fid, ns)

   %  Average Neuroscan data is stored as 4-byte floates in vectored
   %  format for each channel. Each channel has a 5-byte header that
   %  is no longer used. Thus, after SETUP & ELECTLOC, there is an
   %  unused 5-byte header followed by SETUP.pnts of 4-byte floating
   %  point numbers for the first channel; then a 5-byte header for
   %  channel two followed by SETUP.pnts * size(float) bytes, etc.
   %  Therefore, the total number of bytes after SETUP & ELECTLOC is:
   %  SETUP.nchannels * ( 5 + SETUP.pnts * sizeof(float) ). To scale
   %  a data point to microvolts, multiply by the channel-specific
   %  calibration factor (i.e., for electrode j: channel[j]->calib)
   %  and divided by the number of sweeps in the average (i.e., 
   %  channel[j]->n);

   data = zeros(ns.SETUP.nchannels, ns.SETUP.pnts);

   for j = 1:ns.SETUP.nchannels
      fseek(fid, 5, 'cof');
      data(j,:) = [fread(fid, ns.SETUP.pnts, 'float32')]';
      data(j,:) = data(j,:) * ns.ELECTLOC(j).calib / ns.ELECTLOC(j).n;
   end

   return;					% read_avg


%---------------------------------------------------------------------
function SETUP = read_SETUP(fid)

   fseek(fid,0,'bof');

   %  Original structures	
   %  typedef struct{ 
   %     char   rev[12];         /* Revision string                         */
   %     long   NextFile;        /* offset to next file                     */
   %     long   PrevFile;        /* offset to prev file                     */
   %     char   type;            /* File type AVG=0, EEG=1, etc.            */
   %     char   id[20];          /* Patient ID                              */
   %     char   oper[20];        /* Operator ID                             */
   %     char   doctor[20];      /* Doctor ID                               */
   %     char   referral[20];    /* Referral ID                             */
   %     char   hospital[20];    /* Hospital ID                             */
   %     char   patient[20];     /* Patient name                            */
   %     short  int age;         /* Patient Age                             */
   %     char   sex;             /* Patient Sex Male='M', Female='F'        */
   %     char   hand;            /* Handedness Mixed='M',Rt='R', lft='L'    */
   %     char   med[20];         /* Medications                             */
   %     char   category[20];    /* Classification                          */
   %     char   state[20];       /* Patient wakefulness                     */
   %     char   label[20];       /* Session label                           */
   %     char   date[10];        /* Session date string                     */
   %     char   time[12];        /* Session time strin                      */
   %     float  mean_age;        /* Mean age (Group files only)             */
   %     float  stdev;           /* Std dev of age (Group files only)       */
   %     short int n;            /* Number in group file                    */
   %     char   compfile[38];    /* Path and name of comparison file        */
   %     float  SpectWinComp;    // Spectral window compensation factor
   %     float  MeanAccuracy;    // Average respose accuracy
   %     float  MeanLatency;     // Average response latency
   %     char   sortfile[46];    /* Path and name of sort file              */
   %     int    NumEvents;       // Number of events in eventable
   %     char   compoper;        /* Operation used in comparison            */
   %     char   avgmode;         /* Set during online averaging             */
   %     char   review;          /* Set during review of EEG data           */
   %     short unsigned nsweeps;      /* Number of expected sweeps          */
   %     short unsigned compsweeps;   /* Number of actual sweeps            */ 
   %     short unsigned acceptcnt;    /* Number of accepted sweeps          */
   %     short unsigned rejectcnt;    /* Number of rejected sweeps          */
   %     short unsigned pnts;         /* Number of points per waveform      */
   %     short unsigned nchannels;    /* Number of active channels          */
   %     short unsigned avgupdate;    /* Frequency of average update        */
   %     char  domain;           /* Acquisition domain TIME=0, FREQ=1       */
   %     char  variance;         /* Variance data included flag             */
   %     unsigned short rate;    /* D-to-A rate                             */
   %     double scale;           /* scale factor for calibration            */
   %     char  veogcorrect;      /* VEOG corrected flag                     */
   %     char  heogcorrect;      /* HEOG corrected flag                     */
   %     char  aux1correct;      /* AUX1 corrected flag                     */
   %     char  aux2correct;      /* AUX2 corrected flag                     */
   %     float veogtrig;         /* VEOG trigger percentage                 */
   %     float heogtrig;         /* HEOG trigger percentage                 */
   %     float aux1trig;         /* AUX1 trigger percentage                 */
   %     float aux2trig;         /* AUX2 trigger percentage                 */
   %     short int heogchnl;     /* HEOG channel number                     */
   %     short int veogchnl;     /* VEOG channel number                     */
   %     short int aux1chnl;     /* AUX1 channel number                     */
   %     short int aux2chnl;     /* AUX2 channel number                     */
   %     char  veogdir;          /* VEOG trigger direction flag             */
   %     char  heogdir;          /* HEOG trigger direction flag             */
   %     char  aux1dir;          /* AUX1 trigger direction flag             */ 
   %     char  aux2dir;          /* AUX2 trigger direction flag             */
   %     short int veog_n;       /* Number of points per VEOG waveform      */
   %     short int heog_n;       /* Number of points per HEOG waveform      */
   %     short int aux1_n;       /* Number of points per AUX1 waveform      */
   %     short int aux2_n;       /* Number of points per AUX2 waveform      */
   %     short int veogmaxcnt;   /* Number of observations per point - VEOG */
   %     short int heogmaxcnt;   /* Number of observations per point - HEOG */
   %     short int aux1maxcnt;   /* Number of observations per point - AUX1 */
   %     short int aux2maxcnt;   /* Number of observations per point - AUX2 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?