TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
hdr2ascii.m
Go to the documentation of this file.
1 function [argout,H1,h2] = hdr2ascii(source,dest)
2 % HDR2ASCII converts the header information into ASCII text.
3 %
4 % HDR2ASCII(HDR [, ...]);
5 % converts file header HDR
6 % HDR2ASCII(file [, ...]);
7 % converts header of file
8 % HDR2ASCII(arg,dest_file);
9 % converts file header HDR and writes it into dest_file
10 % HDR=HDR2ASCII(...);
11 % returns header HDR
12 %
13 % see also: SLOAD, SOPEN
14 
15 % $Id: hdr2ascii.m 3169 2012-12-04 15:39:03Z schloegl $
16 % Copyright (C) 2007,2008 by Alois Schloegl <a.schloegl@ieee.org>
17 % This is part of the BIOSIG-toolbox http://biosig.sf.net/
18 %
19 % This program is free software; you can redistribute it and/or
20 % modify it under the terms of the GNU General Public License
21 % as published by the Free Software Foundation; either version 3
22 % of the License, or (at your option) any later version.
23 %
24 % This program is distributed in the hope that it will be useful,
25 % but WITHOUT ANY WARRANTY; without even the implied warranty of
26 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 % GNU General Public License for more details.
28 %
29 % You should have received a copy of the GNU General Public License
30 % along with this program; if not, write to the Free Software
31 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 
33 if nargin<2,
34  if isstruct(source)
35  HDR = source;
36  elseif ischar(source)
37  if any(source=='*')
38  [s,HDR]=sload(source);
39  else
40  HDR = sopen(source);
41  HDR = sclose(HDR);
42  end;
43  else
44  'not implemented yet',
45  end;
46  dest = [tempname,'.dlm'];
47 elseif isstruct(source) && ischar(dest);
48  HDR = source;
49 elseif ischar(source) && ischar(dest);
50  HDR = sopen(source);
51  HDR = sclose(HDR);
52 else
53  'not implemented yet',
54 end;
55 if isnan(HDR.NS) && exist('mexSLOAD','file'),
56  [s,HDR]=mexSLOAD(HDR.FileName);
57 end
58 
59 if nargin>1,
60  fid = fopen(dest,'wt');
61  if fid<0,
62  fprintf(2,'ERROR HDR2ASCII: could not open file %s\n',dest);
63  return;
64  end;
65 else
66  fid = 1;
67 end;
68 
69 
70 %%%%%%%%% FIXED HEADER %%%%%%%%%%%%%%
71 fprintf(fid,'[BioSig Header]\n\n');
72 fprintf(fid,'Version=0.10\n');
73 fprintf(fid,'generated=%04i-%02i-%02i %02i:%02i:%04.1f\n',datevec(now));
74 if fid>2;
75  fprintf(fid,'\n;This is a TAB-delimiter file. When you edit this file, make sure not to corrupt the TABs ASCII(9)!\n\n');
76  fprintf(fid,'ThisFile=%s\n',dest);
77 end;
78 
79 
80 fprintf(fid,'\n[Fixed Header]\n');
81 if isfield(HDR,'FileName')
82  fprintf(fid,'Filename\t= %s\n',HDR.FileName);
83 end;
84 fprintf(fid,'Format \t= %s\n',HDR.TYPE);
85 if isfield(HDR.FILE,'size'), fprintf(fid,'SizeOfFile\t= %i\n',HDR.FILE.size); end;
86 if ~isfield(HDR,'NS')
87  HDR.NS = 0;
88 end;
89 fprintf(fid,'NumberOfChannels\t= %i\n',HDR.NS);
90 if isfield(HDR,'SampleRate')
91  fprintf(fid,'SamplingRate \t= %i\n',HDR.SampleRate);
92 end;
93 if isfield(HDR,'NRec') && isfield(HDR,'SPR')
94  fprintf(fid,'Number_of_Samples\t= %i\n',HDR.NRec*HDR.SPR);
95 end;
96 T0 = zeros(1,6);
97 if isfield(HDR,'T0')
98  switch length(HDR.T0)
99  case 1, T0 = datevec(HDR.T0);
100  case 6, T0 = HDR.T0;
101  end;
102  fprintf(fid,'RecordingDateTime\t= %04i-%02i-%02i %02i:%02i:%06.3f\n',T0);
103 end;
104 if isfield(HDR,'Patient')
105  fprintf(fid,'Patient.\n');
106  if isfield(HDR.Patient,'Name')
107  fprintf(fid,'\tName \t= %s\n',HDR.Patient.Name);
108  end;
109  if isfield(HDR.Patient,'Id')
110  fprintf(fid,'\tId\t\t= %s\n',HDR.Patient.Id);
111  end;
112  if isfield(HDR.Patient,'Sex')
113  if (HDR.Patient.Sex==1)
114  fprintf(fid,'\tGender \t= male\n');
115  elseif (HDR.Patient.Sex==2)
116  fprintf(fid,'\tGender \t= female\n');
117  else
118  fprintf(fid,'\tGender \t= unknown\n');
119  end;
120  end;
121  T1 = zeros(1,6);
122  if isfield(HDR.Patient,'Birthday')
123  switch length(HDR.Patient.Birthday)
124  case 1, T1 = datevec(HDR.Patient.Birthday);
125  case 6, T1 = HDR.Patient.Birthday;
126  end;
127  if ~any(isnan(T0))
128  fprintf(fid,'\tAge\t\t= %4.1f years\n',(datenum(T0)-datenum(T1))/(365.25));
129  end;
130  fprintf(fid,'\tBirthday\t= %04i-%02i-%02i %02i:%02i:%06.3f\n',T1);
131  end;
132 end;
133 
134 if isfield(HDR,'Manufacturer')
135  fprintf(fid,'Manufacturer.\n');
136  if isfield(HDR.Manufacturer,'Name')
137  fprintf(fid,'\tName\t\t= %s\n',HDR.Manufacturer.Name);
138  end;
139  if isfield(HDR.Manufacturer,'Model')
140  fprintf(fid,'\tModel\t\t= %s\n',HDR.Manufacturer.Model);
141  end;
142  if isfield(HDR.Manufacturer,'Version')
143  fprintf(fid,'\tVersion \t= %s\n',HDR.Manufacturer.Version);
144  end;
145  if isfield(HDR.Manufacturer,'SerialNumber')
146  fprintf(fid,'\tSerialNumber \t= %s\n',HDR.Manufacturer.SerialNumber);
147  end;
148 end;
149 
150 
151 %%%%%%%% CHANNEL DATA %%%%%%%%%%%%%%%
152 if ~isfield(HDR,'AS') && isfield(HDR,'SampleRate')
153  HDR.AS.SampleRate = repmat(HDR.SampleRate,HDR.NS,1);
154 end;
155 if ~isfield(HDR.AS,'SPR'),
156  HDR.AS.SPR = repmat(HDR.SPR,1,HDR.NS);
157 end;
158 if ~isfield(HDR.AS,'SampleRate'),
159  HDR.AS.SampleRate = HDR.AS.SPR/HDR.SPR*HDR.SampleRate;
160 end;
161 if ~isfield(HDR,'THRESHOLD')
162  HDR.THRESHOLD = repmat(NaN,HDR.NS,2);
163 end;
164 if ~isfield(HDR,'PhysDimCode')
165  if isfield(HDR,'PhysDim')
166  HDR.PhysDimCode = physicalunits(HDR.PhysDim);
167  else
168  HDR.PhysDimCode = zeros(1,HDR.NS);
169  end
170 end;
171 if ~isfield(HDR,'LeadIdCode')
172  HDR = leadidcodexyz(HDR);
173 end;
174 if ~isfield(HDR,'REC')
175  HDR.REC.Impedance = repmat(NaN,HDR.NS,1);
176 end;
177 if ~isfield(HDR.REC,'Impedance')
178  HDR.REC.Impedance = repmat(NaN,HDR.NS,1);
179 end;
180 if ~isfield(HDR,'InChanSelect')
181  InChanSelect = 1:HDR.NS;
182 else
183  InChanSelect = HDR.InChanSelect;
184 end
185 if ~isfield(HDR,'Off')
186  HDR.Off = zeros(HDR.NS,1);
187  HDR.Cal(InChanSelect) = diag(HDR.Calib(2:end,:));
188 end;
189 if ~isfield(HDR,'Cal') && isfield(HDR,'Calib')
190  HDR.Cal = ones(HDR.NS,1);
191  HDR.Cal(InChanSelect) = diag(HDR.Calib(2:end,:));
192 end;
193 if HDR.NS,
194 if length(HDR.Filter.HighPass)==1,
195  HDR.Filter.HighPass = repmat(HDR.Filter.HighPass,HDR.NS,1);
196 end;
197 if length(HDR.Cal)==1,
198  HDR.Cal = repmat(HDR.Cal,HDR.NS,1);
199 end;
200 if length(HDR.Filter.LowPass)==1,
201  HDR.Filter.LowPass = repmat(HDR.Filter.LowPass,HDR.NS,1);
202 end;
203 if length(HDR.Filter.Notch)==1,
204  HDR.Filter.Notch = repmat(HDR.Filter.Notch,HDR.NS,1);
205 end;
206 end;
207 
208 
209 PhysDim = physicalunits(HDR.PhysDimCode);
210 fprintf(fid,'\n[Channel Header]\n#No LeadId Label\tfs [Hz]\tGDFTYP\tTH- TH+ Offset Calib PhysDim HP[Hz] LP[Hz] Notch R[kOhm] x y z\n');
211 for k = 1:HDR.NS,
212  Label = HDR.Label{k};
213  Z = HDR.REC.Impedance(k)/1000;
214  gdftyp = HDR.GDFTYP(min(length(HDR.GDFTYP),k));
215  LP = HDR.Filter.LowPass(min(length(HDR.Filter.LowPass),k));
216  HP = HDR.Filter.HighPass(min(length(HDR.Filter.HighPass),k));
217  Notch = HDR.Filter.Notch(min(length(HDR.Filter.Notch),k));
218  physdim = PhysDim(min(length(PhysDim),k));
219  Label(Label==9)=' '; % replace TAB's because TAB's are used as field delimiter
220  if isfield(HDR.AS,'SampleRate')
221  Fs = HDR.AS.SampleRate(k);
222  else
223  Fs = HDR.SampleRate;
224  end;
225  fprintf(fid,'%3i %i\t%-9s\t%6.1f %2i %i\t%i\t%6e\t%6e %5s %6.4f %5.1f %i %5.1f %f %f %f\n',k,HDR.LeadIdCode(k),Label,Fs,gdftyp,HDR.THRESHOLD(k,1:2),HDR.Off(k),HDR.Cal(k),physdim{1},HP,LP,Notch,Z,HDR.ELEC.XYZ(k,:));
226 end;
227 
228 if ~isfield(HDR.EVENT,'SampleRate');
229  HDR.EVENT.SampleRate = HDR.SampleRate;
230 end;
231 %%%%%%%%%% EVENTTABLE %%%%%%%%%%%%%%%
232 fprintf(fid,'\n[Event Table]\n');
233 fprintf(fid,'NumberOfEvents=%i SampleRate=%f\n TYP\t POS \t date time\t',length(HDR.EVENT.POS),HDR.EVENT.SampleRate);
234 if isfield(HDR.EVENT,'CHN')
235  fprintf(fid,'\tCHN\tDUR/VAL');
236 end;
237 fprintf(fid,'\tDescription\n');
238 
239 % use global to improve speed
240 global BIOSIG_GLOBAL;
241 if ~isfield(BIOSIG_GLOBAL,'ISLOADED_EVENTCODES')
242  BIOSIG_GLOBAL.ISLOADED_EVENTCODES = 0;
243 end;
244 if ~BIOSIG_GLOBAL.ISLOADED_EVENTCODES,
245  H=sopen('eventcodes.txt'); sclose(H);
246 end;
247 
248 if ~isfield(HDR,'Calib'); HDR.Calib = sparse(2:HDR.NS+1,1:HDR.NS,HDR.Cal); end
249 
250 for k = 1:length(HDR.EVENT.POS);
251  if length(HDR.T0)==1,
252  T0 = HDR.T0;
253  else
254  T0 = datenum(HDR.T0);
255  end;
256  if isfield(HDR.EVENT,'TimeStamp');
257  t = datevec(HDR.EVENT.TimeStamp(k));
258  else
259  t = datevec(HDR.EVENT.POS(k)/(24*3600*HDR.SampleRate)+T0);
260  end
261  fprintf(fid,'0x%04x\t%9i\t%04i-%02i-%02i %02i:%02i:%07.4f',[HDR.EVENT.TYP(k),HDR.EVENT.POS(k),t(1:6)]);
262 %% fprintf(fid,'0x%04x\t%9i',[HDR.EVENT.TYP(k),HDR.EVENT.POS(k)]');
263  if isfield(HDR.EVENT,'CHN')
264  if ~isempty(HDR.EVENT.CHN)
265  fprintf(fid,'\t%i\t%i',HDR.EVENT.CHN(k),HDR.EVENT.DUR(k));
266  end;
267  else
268  fprintf(fid,'\t-\t-');
269  end;
270  if HDR.EVENT.TYP(k)==hex2dec('7fff'),
271  ch = HDR.EVENT.CHN(k);
272  fprintf(fid,'\t%f %s',[1,HDR.EVENT.DUR(k)]*HDR.Calib([1,ch+1],ch),HDR.PhysDim{ch});
273  elseif 1,
274  elseif HDR.EVENT.TYP(k)==0,
275  ;
276  elseif HDR.EVENT.TYP(k)==hex2dec('7ffe'),
277  ;
278  elseif (isfield(HDR.EVENT,'CodeDesc') && (HDR.EVENT.TYP(k) <= length(HDR.EVENT.CodeDesc)))
279  fprintf(fid,'\t%s',HDR.EVENT.CodeDesc{HDR.EVENT.TYP(k)});
280  else
281  ix = find(HDR.EVENT.TYP(k)==BIOSIG_GLOBAL.EVENT.CodeIndex);
282  if length(ix)==1,
283  fprintf(fid,'\t%s',BIOSIG_GLOBAL.EVENT.CodeDesc{ix});
284  end;
285  end;
286  fprintf(fid,'\n');
287 end;
288 
289 if fid>2,
290  fclose(fid);
291 end;
292 if nargout>0,
293  argout=HDR;
294 end;
295 
sclose
function sclose(in HDR)
hdr2ascii
function hdr2ascii(in source, in dest)
physicalunits
function physicalunits(in arg1)
sload
function sload(in FILENAME, in varargin)
sopen
function sopen(in arg1, in PERMISSION, in CHAN, in MODE, in arg5, in arg6)
leadidcodexyz
function leadidcodexyz(in arg1)