TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
artifact_selection.m
Go to the documentation of this file.
1 function [HDR,A] = artifact_selection(fn,t1,t2)
2 % ARTIFACT_SELECTION returns the selected triggered trials with
3 % artifacts. The length of the trial is defined by t1 and t2 in
4 % seconds.
5 %
6 % [HDR] = artifact_selection(filename,[t1,t2])
7 % [HDR] = artifact_selection(HDR,[t1,t2])
8 % uses EVENT information in filename or HDR.EVENT.
9 %
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.
14 %
15 % In case the trigger information and the artifact scoring is
16 % stored in separate files, the information of several files can be
17 % merged.
18 %
19 % [HDR] = artifact_selection({sourcefile,eventfile1,eventfile2,...},[t1,t2])
20 %
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
23 % all other files.
24 %
25 % see also: TLOAD, SLOAD, SOPEN,
26 
27 % $Revision: 1.6 $
28 % $Id: artifact_selection.m 2202 2009-10-27 12:06:45Z schloegl $
29 % Copyright (c) 2004-2005,2007 by Alois Schloegl <a.schloegl@ieee.org>
30 % This is part of the BIOSIG-toolbox http://biosig.sf.net/
31 
32 
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.
37 %
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.
42 %
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.
46 
47 
48 % load and merge all artifact information
49 if ischar(fn)
50  HDR = sopen(fn,'r'); HDR = sclose(HDR);
51  EVENTMATRIX = [HDR.EVENT.POS,HDR.EVENT.TYP,HDR.EVENT.CHN,HDR.EVENT.DUR];
52 elseif iscell(fn)
53  for k = 1:length(fn)
54  if ischar(fn{k})
55  h = sopen(fn{k},'r'); h = sclose(h);
56  elseif isstruct(fn{k}) & isfield(fn{k},'EVENT')
57  h = fn{k};
58  end
59  eventmatrix = [h.EVENT.POS,h.EVENT.TYP,h.EVENT.CHN,h.EVENT.DUR];
60  if k==1,
61  HDR = h;
62  if length(HDR.FILE)>1,
63  fprintf(2,'Warning Artifact_Selection: does not work correctly for multiple files.\n');
64  end;
65  EVENTMATRIX = eventmatrix;
66  else
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;
72  end
73  end
74  else
75  HDR.SampleRate = h.SampleRate;
76  end;
77  end;
78  % merge
79  EVENTMATRIX = [EVENTMATRIX; eventmatrix];
80  end;
81  end;
82 end;
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);
88 
89 % prepare trigger information
90 if ~isfield(HDR,'TRIG')
91  HDR.TRIG = HDR.EVENT.POS(HDR.EVENT.TYP==hex2dec('0300'));
92 end;
93 HDR.TRIG = sort(HDR.TRIG);
94 SEL = logical(zeros(length(HDR.TRIG),1));
95 
96 % define interval
97 if nargin<2,
98  if HDR.FLAG.TRIGGERED,
99  ti = [1, HDR.SPR];
100  else
101  ti = [1, max(diff(HDR.TRIG))];
102  end;
103 else
104  if prod(size(t1))==1,
105  ti = [t1,t2];
106  elseif prod(size(t1))==2,
107  ti = t1;
108  else
109  error('invalid time interval');
110  return;
111  end;
112  ti = ti*HDR.SampleRate;
113 end;
114 
115 if min(diff(HDR.TRIG))<(ti(2)-ti(1))
116  fprintf(2,'Warning: trials do overlap.\n');
117 end;
118 
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);
125 
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))
133  k = k+1;
134  %[P1,P2,P3]
135  if P1<=P2,
136  SEL(ix1) = (P3>P2) | a;
137  %fprintf(1,'%6i\t',-1,k,a, ix1,ix2,P1,P2,P3,SEL(ix1));
138  ix1 = ix1+1;
139  P1 = TRIG(ix1)+ti(1);
140  P3 = TRIG(ix1)+ti(2);
141  elseif P2<P1,
142  a = a + A.EVENT.TYP(ix2);
143  %fprintf(1,'%6i\t',-2,k,a,ix1,ix2,P1,P2,P3,SEL(ix1));
144  ix2 = ix2+1;
145  P2 = A.EVENT.POS(ix2);
146  end;
147  %fprintf(1,'\n');
148 end;
149 
150 HDR.ArtifactSelection = SEL;
artifact_selection
function artifact_selection(in fn, in t1, in t2)
sclose
function sclose(in HDR)
sopen
function sopen(in arg1, in PERMISSION, in CHAN, in MODE, in arg5, in arg6)