Home > vbmeg > functions > device > trigger_timing > vb_get_trial_time_index.m

vb_get_trial_time_index

PURPOSE ^

get trial time index from status channel

SYNOPSIS ^

function [ix_trial,trig,cond_id,status_val,status_out] =vb_get_trial_time_index(status,parm)

DESCRIPTION ^

 get trial time index from status channel

    [ix_trial,trig,cond_id,status_val,status_out] = ...
          vb_get_trial_time_index(status,parm)
 --- Inout
  parm.Pretrigger_ms : Pretrigger period   [msec]
  parm.Posttrigger_ms: Posttrigger period  [msec]
  parm.condition : string describing condition [string or cell array]
  parm.status_level : status level (ratio for max) for onset [Ncondition x 1]
  parm.fsamp     : Sample Frequency [Hz]
 --- Output
 trig(n)       : Onset time index for n-th trial 
 cond_id(n)    : Condition ID for n-th trial 
 ix_trial(:,n) : Time index for n-th trial   [Tperiod x Ntrial]
                 Tperiod : # of time sample in one trial
                 Ntrial  : # of trials
 status_val(m) = parm.status_level(m) * max(status)
 status_out : masked status signal if mask flag is specified

 2009-6-11 Masa-aki Sato
 2011-12-11 Masa-aki Sato 
    Added 'status_out'
 2012-1-8  Masa-aki Sato 
   Added 'multi' channel bit pattern

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function    [ix_trial,trig,cond_id,status_val,status_out] = ...
0002             vb_get_trial_time_index(status,parm)
0003 % get trial time index from status channel
0004 %
0005 %    [ix_trial,trig,cond_id,status_val,status_out] = ...
0006 %          vb_get_trial_time_index(status,parm)
0007 % --- Inout
0008 %  parm.Pretrigger_ms : Pretrigger period   [msec]
0009 %  parm.Posttrigger_ms: Posttrigger period  [msec]
0010 %  parm.condition : string describing condition [string or cell array]
0011 %  parm.status_level : status level (ratio for max) for onset [Ncondition x 1]
0012 %  parm.fsamp     : Sample Frequency [Hz]
0013 % --- Output
0014 % trig(n)       : Onset time index for n-th trial
0015 % cond_id(n)    : Condition ID for n-th trial
0016 % ix_trial(:,n) : Time index for n-th trial   [Tperiod x Ntrial]
0017 %                 Tperiod : # of time sample in one trial
0018 %                 Ntrial  : # of trials
0019 % status_val(m) = parm.status_level(m) * max(status)
0020 % status_out : masked status signal if mask flag is specified
0021 %
0022 % 2009-6-11 Masa-aki Sato
0023 % 2011-12-11 Masa-aki Sato
0024 %    Added 'status_out'
0025 % 2012-1-8  Masa-aki Sato
0026 %   Added 'multi' channel bit pattern
0027 %
0028 % Copyright (C) 2011, ATR All Rights Reserved.
0029 % License : New BSD License(see VBMEG_LICENSE.txt)
0030 
0031 % Pre/Post period in msec
0032 Pretrigger_ms  = parm.Pretrigger_ms ; % [msec]
0033 Posttrigger_ms = parm.Posttrigger_ms; % [msec]
0034 
0035 % Sample Frequency [Hz]
0036 fsamp  = parm.fsamp;
0037 %fprintf('Sampleing Frequency: %6.1f [Hz]\n',fsamp)
0038 
0039 % Sample number for Pre/Post period
0040 Pretrigger  = ceil(Pretrigger_ms *(fsamp/1000));
0041 Posttrigger = ceil(Posttrigger_ms*(fsamp/1000));
0042 
0043 status_level  = parm.status_level ;
0044 
0045 if isempty(status_level)
0046     Ncond = 1;
0047 else
0048     Ncond = length(status_level);
0049 end
0050 
0051 [Nch ,T] = size(status);
0052 status_out = zeros(Nch ,T);
0053 
0054 if (Ncond ~= Nch ),
0055     if (Ncond > 1) && (Nch > 1),
0056         error('Condition number and Status ch number mismatch!!')
0057     end
0058 end
0059 
0060 if    strcmp( lower(parm.trig_type), 'multi')==1,
0061     [ix_trg,status_val,status_out] = ...
0062         vb_get_trigger_multi(status,status_level,parm);
0063     
0064     Ntrg   = length(ix_trg);
0065     ix_trg = {ix_trg};
0066     Ncond  = 1;
0067     fprintf('# of trials = %d\n',Ntrg)
0068 else
0069     
0070     ix_trg = cell(1,Ncond);
0071     Ntrg = zeros(Ncond,1);
0072     status_val = zeros(Ncond,1);
0073     
0074     % extract event start from status signal
0075     for n = 1:Ncond
0076         if (n > 1) && (Nch == 1),
0077             y = status;
0078         else
0079             y = status(n,:);
0080         end
0081         status_offset = 0;
0082         status_mask   = [];
0083         if isfield(parm,'status_offset')
0084             status_offset = parm.status_offset;
0085         end
0086         if isfield(parm,'status_mask')
0087             % mask irrelevant bit
0088             status_mask = parm.status_mask;
0089         end
0090         y = vb_get_status_mask(y, status_mask, status_offset);
0091         
0092         if isempty(status_level)
0093             level = [];
0094         else
0095             level = status_level(n);
0096         end
0097     
0098         [ix_trg{n},status_val(n),ya] = vb_get_trigger_event(y,level,parm);
0099         
0100         if (n > 1) && (Nch == 1),
0101             status_out = ya;
0102         else
0103             status_out(n,:) = ya;
0104         end
0105     
0106         Ntrg(n) = length(ix_trg{n});
0107         fprintf('# of trials = %d (Cond: %d)\n',Ntrg(n),n)
0108     end
0109 end
0110 
0111 if sum(Ntrg)==0
0112     fprintf('No epoch is found\n\n')
0113     ix_trial = [];
0114     cond_id = [];
0115     trig = [];
0116     return
0117 end
0118 
0119 % trigger index and condition flag for all epochs
0120 Nall = sum(Ntrg);
0121 cond_id = zeros(1,Nall);
0122 trig = zeros(1,Nall);
0123 Nst  = 0;
0124 
0125 % Combine all trigger time index
0126 for n = 1:Ncond
0127     ix = (1:Ntrg(n)) + Nst;
0128     trig(ix) = round(ix_trg{n})';
0129     cond_id(ix) = n;
0130     Nst = Nst + Ntrg(n);
0131 end
0132 
0133 % trig(n) : n-th triger time
0134 % cond_id(n) : corresponding condition label
0135 
0136 %--- sort triger timing according to temporal order
0137 [trig, jx] = sort(trig);
0138 cond_id = cond_id(jx);
0139 
0140 % Time index for one trial
0141 ix = (-Pretrigger:Posttrigger)';
0142 Tp = length(ix);
0143 
0144 % Time index for all trials extracted from 'trig'
0145 ix_trial = repmat(ix,[1 Nall]) + repmat(trig,[Tp 1]);
0146 
0147 % Check index range
0148 ix_trial = max(ix_trial,1);
0149 trig     = max(trig    ,1);
0150 ix_trial = min(ix_trial,T);
0151 trig     = min(trig    ,T);

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