2 % ARTIFACT_SELECTION returns the selected triggered trials with
3 % artifacts. The length of the trial is defined by t1 and t2 in
8 % uses EVENT information in filename or HDR.EVENT.
10 % The artifact selection is available in HDR.ArtifactSelection.
11 % HDR.ArtifactSelection is vector with the same length than the list
12 % of trigger points (event 0x0300). A value of 1 indicates an
13 % artifact, a value of 0 means the trial is free from any artifacts.
15 % In
case the trigger information and the artifact scoring is
16 % stored in separate files, the information of several files can be
21 % All files can be defined by their filename, or by the BIOSIG HDR-
struct.
22 % The header of the first file is merged with the Event information of
25 % see also: TLOAD, SLOAD, SOPEN,
29 % Copyright (c) 2004-2005,2007 by Alois Schloegl <a.schloegl@ieee.org>
30 % This is part of the BIOSIG-toolbox http:
33 % This program is free software; you can redistribute it and/or
34 % modify it under the terms of the GNU General Public License
35 % as published by the Free Software Foundation; either version 2
36 % of the License, or (at your option) any later version.
38 % This program is distributed in the hope that it will be useful,
39 % but WITHOUT ANY WARRANTY; without even the implied warranty of
40 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 % GNU General Public License
for more details.
43 % You should have received a copy of the GNU General Public License
44 % along with
this program;
if not, write to the Free Software
45 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
48 % load and merge all artifact information
51 EVENTMATRIX = [HDR.EVENT.POS,HDR.EVENT.TYP,HDR.EVENT.CHN,HDR.EVENT.DUR];
56 elseif isstruct(fn{k}) & isfield(fn{k},
'EVENT')
59 eventmatrix = [h.EVENT.POS,h.EVENT.TYP,h.EVENT.CHN,h.EVENT.DUR];
62 if length(HDR.FILE)>1,
63 fprintf(2,
'Warning Artifact_Selection: does not work correctly for multiple files.\n');
65 EVENTMATRIX = eventmatrix;
67 if isfield(h.EVENT,
'SampleRate')
68 if isfield(HDR.EVENT,'SampleRate')
69 if ~isnan(h.EVENT.SampleRate),
70 if HDR.EVENT.SampleRate ~= h.EVENT.SampleRate,
71 eventmatrix(:,[1,4]) = eventmatrix(:,[1,4])*HDR.EVENT.SampleRate/h.EVENT.SampleRate;
75 HDR.SampleRate = h.SampleRate;
79 EVENTMATRIX = [EVENTMATRIX; eventmatrix];
83 EVENTMATRIX = unique(EVENTMATRIX,'rows'); %$ remove
double entries
84 HDR.EVENT.POS = EVENTMATRIX(:,1);
85 HDR.EVENT.TYP = EVENTMATRIX(:,2);
86 HDR.EVENT.CHN = EVENTMATRIX(:,3);
87 HDR.EVENT.DUR = EVENTMATRIX(:,4);
89 % prepare trigger information
90 if ~isfield(HDR,'TRIG')
91 HDR.TRIG = HDR.EVENT.POS(HDR.EVENT.TYP==hex2dec('0300'));
93 HDR.TRIG = sort(HDR.TRIG);
94 SEL = logical(zeros(length(HDR.TRIG),1));
98 if HDR.FLAG.TRIGGERED,
101 ti = [1, max(diff(HDR.TRIG))];
104 if prod(size(t1))==1,
106 elseif prod(size(t1))==2,
109 error('invalid time interval');
112 ti = ti*HDR.SampleRate;
115 if min(diff(HDR.TRIG))<(ti(2)-ti(1))
116 fprintf(2,'Warning: trials do overlap.\n');
119 % prepare artifact information
120 ix = find(bitand(HDR.EVENT.TYP,hex2dec('FFF0'))==hex2dec('0100'));
121 A.EVENT.POS = [HDR.EVENT.POS(ix); HDR.EVENT.POS(ix) + HDR.EVENT.DUR(ix); inf]; % onset and offset
122 A.EVENT.TYP = [ones(length(ix),1); -ones(length(ix),1); 0]; % onset = +1, offset = -1;
123 [A.EVENT.POS, ix2] = sort(A.EVENT.POS); % sort the positions
124 A.EVENT.TYP = A.EVENT.TYP(ix2);
126 % check each trial for an artifact.
127 TRIG = [HDR.TRIG(:);inf];
128 ix1 = 1; ix2 = 1; a = 0; k=0;
129 P1 = HDR.TRIG(ix1)+ti(1);
130 P2 = A.EVENT.POS(ix2);
131 P3 = TRIG(ix1)+ti(2);
132 while (ix1<=length(HDR.TRIG)) & (ix2<length(A.EVENT.POS))
136 SEL(ix1) = (P3>P2) | a;
137 %fprintf(1,'%6i\t',-1,k,a, ix1,ix2,P1,P2,P3,
SEL(ix1));
139 P1 = TRIG(ix1)+ti(1);
140 P3 = TRIG(ix1)+ti(2);
142 a = a + A.EVENT.TYP(ix2);
143 %fprintf(1,'%6i\t',-2,k,a,ix1,ix2,P1,P2,P3,
SEL(ix1));
145 P2 = A.EVENT.POS(ix2);
150 HDR.ArtifactSelection =
SEL;