0001 function [ix_trial,trig,cond_id,status_val,status_out] = ...
0002 vb_get_trial_time_index(status,parm)
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 Pretrigger_ms = parm.Pretrigger_ms ;
0033 Posttrigger_ms = parm.Posttrigger_ms;
0034
0035
0036 fsamp = parm.fsamp;
0037
0038
0039
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
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
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
0120 Nall = sum(Ntrg);
0121 cond_id = zeros(1,Nall);
0122 trig = zeros(1,Nall);
0123 Nst = 0;
0124
0125
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
0134
0135
0136
0137 [trig, jx] = sort(trig);
0138 cond_id = cond_id(jx);
0139
0140
0141 ix = (-Pretrigger:Posttrigger)';
0142 Tp = length(ix);
0143
0144
0145 ix_trial = repmat(ix,[1 Nall]) + repmat(trig,[Tp 1]);
0146
0147
0148 ix_trial = max(ix_trial,1);
0149 trig = max(trig ,1);
0150 ix_trial = min(ix_trial,T);
0151 trig = min(trig ,T);