TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
leadidcodexyz.m
Go to the documentation of this file.
1 function [HDR] = leadidcodexyz(arg1)
2 % LeadIdCodeXYZ uses the Label information for computing the
3 % LeadIdCode and the XYZ position of the EEG Electrodes
4 % according to Annex A of FEF Vital Signs Format [1]
5 %
6 % HDR = leadidcodexyz(HDR);
7 % adds HDR.LeadIdCode and HDR.ELEC.XYZ, if needed.
8 %
9 % see also: SLOAD, SOPEN, PHYSICALUNITS, doc/leadidtable_scpecg.txt, doc/elecpos.txt
10 %
11 % Reference(s):
12 % [1] CEN/TC251/PT40 (2001)
13 % File Exchange Format for Vital Signs - Annex A
14 %
15 % Birbaumer, N. (2006). Brain-computer-interface research: Coming of age. Clinical Neurophysiology, 117:479–83.
16 % http://www.acns.org/pdfs/ACFDD46.pdf.
17 % ACNS (2006). Guidelines for standard electrode position nomenclature. American Clinical
18 % Neurophysiology Society. http://www.acns.org/pdfs/ACFDD46.pdf.
19 
20 %
21 % This program is free software; you can redistribute it and/or
22 % modify it under the terms of the GNU General Public License
23 % as published by the Free Software Foundation; either version 3
24 % of the License, or (at your option) any later version.
25 
26 % $Id: leadidcodexyz.m 2602 2011-02-03 13:05:03Z schloegl $
27 % Copyright (C) 2006,2007,2008,2009 by Alois Schloegl <a.schloegl@ieee.org>
28 % This is part of the BIOSIG-toolbox http://biosig.sf.net/
29 
30 
31 global BIOSIG_GLOBAL;
32 % BIOSIG_GLOBAL=[]; %%% used for debugging, only.
33 
34 if ~isfield(BIOSIG_GLOBAL,'Phi');
35  BIOSIG_GLOBAL.ISLOADED_XYZ = 0 ;
36 end;
37 if ~isfield(BIOSIG_GLOBAL,'ISLOADED_XYZ')
38  BIOSIG_GLOBAL.ISLOADED_XYZ = 0 ;
39 end;
40 if ~BIOSIG_GLOBAL.ISLOADED_XYZ;
41  f = which('getfiletype.m'); % identify path to biosig
42  [p,f,e] = fileparts(f);
43  [p,f,e] = fileparts(p);
44 
45  BIOSIG_GLOBAL.ISLOADED_XYZ = 0 ;
46 
47  N = 0;
48  fid = fopen(fullfile(p,'doc','leadidtable_scpecg.txt'),'r');
49  s = char(fread(fid,[1,inf],'uint8'));
50  fclose(fid);
51 
52  Code = repmat(NaN, 200, 1); Phi = Code; Theta = Code;
53  while ~isempty(s),
54  [t,s] = strtok(s,[10,13]);
55  if ~length(t)
56  elseif ~strncmp(t,'#',1)
57  ix3 = strfind(t,'MDC_ECG_LEAD_');
58  if isempty(ix3)
59  ix3 = length(t)+1;
60  end
61  [t1,t2] = strtok(t(1:ix3-1),[9,32]);
62  [t2,t3] = strtok(t2,[9,32]);
63  id = str2double(t2);
64  N = N + 1;
65  Labels{N,1} = t1;
66  Code(N,1) = id;
67  Description{N,1} = deblank(t3);
68  %MDC_ECG_LEAD{N,1} = t(ix3+13:end)
69  MDC_ECG_LEAD{N,1} = t(ix3:end);
70  end;
71  end;
72  N1 = N;
73 
74  % load table
75  fid = fopen(fullfile(p,'doc','elecpos.txt'),'r');
76  t = char(fread(fid,[1,inf],'uint8'));
77  fclose(fid);
78 
79  % extract table information
80  while ~isempty(t)
81  [x,t] = strtok(t,[10,13]);
82  if isempty(x)
83  elseif strncmp(x,'#',1)
84  else
85  N = N + 1;
86  [num,status,strarray] = str2double(x);
87  Code(N,1) = num(1);
88  Labels{N,1} = upper(strarray{2});
89  Phi(N,1) = num(3);
90  Theta(N,1) = num(4);
91  end;
92  end;
93  Phi = Phi(:) *pi/180;
94  Theta = Theta(:)*pi/180;
95 
96 
97  % loading is done only once.
98  BIOSIG_GLOBAL.XYZ = [sin(Theta).*cos(Phi), sin(Theta).*sin(Phi), cos(Theta)];
99  BIOSIG_GLOBAL.Phi = Phi*180/pi;
100  BIOSIG_GLOBAL.Theta = Theta*180/pi;
101  BIOSIG_GLOBAL.LeadIdCode = Code;
102  BIOSIG_GLOBAL.Label = Labels;
103  BIOSIG_GLOBAL.Description = Description;
104  BIOSIG_GLOBAL.MDC_ECG_LEAD = MDC_ECG_LEAD;
105 
106  BIOSIG_GLOBAL.ISLOADED_XYZ = 1;
107 end;
108 
109 
110 if nargin<1,
111  HDR.LeadIdCode = BIOSIG_GLOBAL.LeadIdCode;
112  HDR.Label = BIOSIG_GLOBAL.Label;
113  HDR.ELEC.XYZ = BIOSIG_GLOBAL.XYZ;
114  HDR.TYPE = 'ELPOS';
115 
116 else % electrode code and position
117 
118  if isstruct(arg1)
119  HDR = arg1;
120  elseif isnumeric(arg1),
121  HDR.LeadIdCode = arg1;
122  else
123  HDR.Label = arg1;
124  end;
125 
126  tmp.flag1 = isfield(HDR,'ELEC');
127  if tmp.flag1,
128  tmp.flag1 = isfield(HDR.ELEC,'XYZ');
129  end;
130  if tmp.flag1,
131  tmp.flag1 = any(HDR.ELEC.XYZ(:));
132  end;
133  tmp.flag2 = isfield(HDR,'LeadIdCode');
134  tmp.flag3 = isfield(HDR,'Label');
135 
136  if (~tmp.flag1 || ~tmp.flag2 || ~tmp.flag3),
137  if 0,
138  elseif tmp.flag3,
139  if ischar(HDR.Label)
140  HDR.Label = cellstr(HDR.Label);
141  end;
142  NS = length(HDR.Label);
143  elseif tmp.flag2,
144  NS = length(HDR.LeadIdCode);
145  elseif isfield(HDR,'NS')
146  NS = HDR.NS;
147  HDR.LeadIdCode = zeros(1,HDR.NS);
148  end;
149 
150  if tmp.flag3,
151  if ~tmp.flag1,
152  HDR.ELEC.XYZ = repmat(NaN,NS,3);
153  HDR.ELEC.Phi = repmat(NaN,NS,1);
154  HDR.ELEC.Theta = repmat(NaN,NS,1);
155  end;
156  if ~tmp.flag2,
157  HDR.LeadIdCode = repmat(NaN,NS,1);
158  end;
159  for k = 1:NS;
160  Label = upper(deblank(HDR.Label{k}));
161  pos = find(Label==':');
162  if ~isempty(pos)
163  Label = Label(pos+1:end);
164  end;
165  ix = strmatch(Label,BIOSIG_GLOBAL.Label,'exact');
166 
167  if length(ix)==2,
168  %%%%% THIS IS A HACK %%%%%
169  %% solve ambiguity for 'A1','A2'; could be EEG or ECG
170  if sum(HDR.LeadIdCode(1:k)>=996)>sum(HDR.LeadIdCode(1:k)<996)
171  %% majority are EEG electrodes,
172  ix = ix(find(BIOSIG_GLOBAL.LeadIdCode(ix)>996));
173  else
174  %% majority are ECG electrodes,
175  ix = ix(find(BIOSIG_GLOBAL.LeadIdCode(ix)<996));
176  end;
177  elseif isempty(ix)
178  ix = strmatch(deblank(HDR.Label{k}),BIOSIG_GLOBAL.MDC_ECG_LEAD,'exact');
179  end;
180 
181  if (length(ix)==1),
182  if ~tmp.flag1,
183  HDR.ELEC.XYZ(k,1:3) = BIOSIG_GLOBAL.XYZ(ix,:);
184  HDR.ELEC.Phi(k) = BIOSIG_GLOBAL.Phi(ix);
185  HDR.ELEC.Theta(k) = BIOSIG_GLOBAL.Theta(ix);
186  end;
187  if ~tmp.flag2,
188  HDR.LeadIdCode(k,1) = BIOSIG_GLOBAL.LeadIdCode(ix);
189  end;
190  end;
191  end;
192  else
193  HDR.Label = cell(NS,1);
194  for k = 1:NS;
195  ix = find(BIOSIG_GLOBAL.LeadIdCode==HDR.LeadIdCode(k));
196  if (length(ix)>=1),
197  ix=ix(1); %
198  HDR.Label{k} = BIOSIG_GLOBAL.Label{ix};
199  if ~tmp.flag1,
200  HDR.ELEC.XYZ(k,1:3) = BIOSIG_GLOBAL.XYZ(ix,1:3);
201  HDR.ELEC.Phi(k,1) = BIOSIG_GLOBAL.Phi(ix);
202  HDR.ELEC.Theta(k,1) = BIOSIG_GLOBAL.Theta(ix);
203  end;
204  else
205  HDR.Label{k} = ['#',int2str(k)];
206  end;
207  end;
208  end;
209  end;
210  if tmp.flag3 && ~any(HDR.LeadIdCode),
211  for k = 1:HDR.NS;
212  ix = strmatch(upper(HDR.Label{k}),BIOSIG_GLOBAL.Label,'exact');
213  if length(ix)==1,
214  HDR.LeadIdCode(k) = BIOSIG_GLOBAL.LeadIdCode(ix);
215  end;
216  end;
217  end;
218 end;
219 
str2double
function str2double(in s, in cdelim, in rdelim, in ddelim)
getfiletype
function getfiletype(in arg1)
leadidcodexyz
function leadidcodexyz(in arg1)