1 function [argout,H1,h2] =
hdr2ascii(source,dest)
2 % HDR2ASCII converts the header information into ASCII text.
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
13 % see also: SLOAD, SOPEN
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:
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.
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.
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.
38 [s,HDR]=
sload(source);
44 'not implemented yet',
46 dest = [tempname,
'.dlm'];
47 elseif isstruct(source) && ischar(dest);
49 elseif ischar(source) && ischar(dest);
53 'not implemented yet',
55 if isnan(HDR.NS) && exist(
'mexSLOAD',
'file'),
56 [s,HDR]=mexSLOAD(HDR.FileName);
60 fid = fopen(dest,
'wt');
62 fprintf(2,
'ERROR HDR2ASCII: could not open file %s\n',dest);
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));
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);
80 fprintf(fid,
'\n[Fixed Header]\n');
81 if isfield(HDR,
'FileName')
82 fprintf(fid,'Filename\t= %s\n',HDR.FileName);
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;
89 fprintf(fid,'NumberOfChannels\t= %i\n',HDR.NS);
90 if isfield(HDR,'SampleRate')
91 fprintf(fid,'SamplingRate \t= %i\n',HDR.SampleRate);
93 if isfield(HDR,'NRec') && isfield(HDR,'SPR')
94 fprintf(fid,'Number_of_Samples\t= %i\n',HDR.NRec*HDR.SPR);
99 case 1, T0 = datevec(HDR.T0);
102 fprintf(fid,'RecordingDateTime\t= %04i-%02i-%02i %02i:%02i:%06.3f\n',T0);
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);
109 if isfield(HDR.Patient,'Id')
110 fprintf(fid,'\tId\t\t= %s\n',HDR.Patient.Id);
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');
118 fprintf(fid,'\tGender \t= unknown\n');
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;
128 fprintf(fid,'\tAge\t\t= %4.1f years\n',(datenum(T0)-datenum(T1))/(365.25));
130 fprintf(fid,'\tBirthday\t= %04i-%02i-%02i %02i:%02i:%06.3f\n',T1);
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);
139 if isfield(HDR.Manufacturer,'Model')
140 fprintf(fid,'\tModel\t\t= %s\n',HDR.Manufacturer.Model);
142 if isfield(HDR.Manufacturer,'Version')
143 fprintf(fid,'\tVersion \t= %s\n',HDR.Manufacturer.Version);
145 if isfield(HDR.Manufacturer,'SerialNumber')
146 fprintf(fid,'\tSerialNumber \t= %s\n',HDR.Manufacturer.SerialNumber);
151 %%%%%%%% CHANNEL DATA %%%%%%%%%%%%%%%
152 if ~isfield(HDR,'AS') && isfield(HDR,'SampleRate')
153 HDR.AS.SampleRate = repmat(HDR.SampleRate,HDR.NS,1);
155 if ~isfield(HDR.AS,'SPR'),
156 HDR.AS.SPR = repmat(HDR.SPR,1,HDR.NS);
158 if ~isfield(HDR.AS,'SampleRate'),
159 HDR.AS.SampleRate = HDR.AS.SPR/HDR.SPR*HDR.SampleRate;
161 if ~isfield(HDR,'THRESHOLD')
162 HDR.THRESHOLD = repmat(NaN,HDR.NS,2);
164 if ~isfield(HDR,'PhysDimCode')
165 if isfield(HDR,'PhysDim')
168 HDR.PhysDimCode = zeros(1,HDR.NS);
171 if ~isfield(HDR,'LeadIdCode')
174 if ~isfield(HDR,'REC')
175 HDR.REC.Impedance = repmat(NaN,HDR.NS,1);
177 if ~isfield(HDR.REC,'Impedance')
178 HDR.REC.Impedance = repmat(NaN,HDR.NS,1);
180 if ~isfield(HDR,'InChanSelect')
181 InChanSelect = 1:HDR.NS;
183 InChanSelect = HDR.InChanSelect;
185 if ~isfield(HDR,'Off')
186 HDR.Off = zeros(HDR.NS,1);
187 HDR.Cal(InChanSelect) = diag(HDR.Calib(2: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,:));
194 if length(HDR.Filter.HighPass)==1,
195 HDR.Filter.HighPass = repmat(HDR.Filter.HighPass,HDR.NS,1);
197 if length(HDR.Cal)==1,
198 HDR.Cal = repmat(HDR.Cal,HDR.NS,1);
200 if length(HDR.Filter.LowPass)==1,
201 HDR.Filter.LowPass = repmat(HDR.Filter.LowPass,HDR.NS,1);
203 if length(HDR.Filter.Notch)==1,
204 HDR.Filter.Notch = repmat(HDR.Filter.Notch,HDR.NS,1);
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');
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);
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,:));
228 if ~isfield(HDR.EVENT,
'SampleRate');
229 HDR.EVENT.SampleRate = HDR.SampleRate;
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');
237 fprintf(fid,'\tDescription\n');
239 % use global to improve speed
240 global BIOSIG_GLOBAL;
241 if ~isfield(BIOSIG_GLOBAL,'ISLOADED_EVENTCODES')
242 BIOSIG_GLOBAL.ISLOADED_EVENTCODES = 0;
244 if ~BIOSIG_GLOBAL.ISLOADED_EVENTCODES,
248 if ~isfield(HDR,'Calib'); HDR.Calib = sparse(2:HDR.NS+1,1:HDR.NS,HDR.Cal); end
250 for k = 1:length(HDR.EVENT.POS);
251 if length(HDR.T0)==1,
254 T0 = datenum(HDR.T0);
256 if isfield(HDR.EVENT,'TimeStamp');
257 t = datevec(HDR.EVENT.TimeStamp(k));
259 t = datevec(HDR.EVENT.POS(k)/(24*3600*HDR.SampleRate)+T0);
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));
268 fprintf(fid,'\t-\t-');
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});
274 elseif HDR.EVENT.TYP(k)==0,
276 elseif HDR.EVENT.TYP(k)==hex2dec(
'7ffe'),
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)});
281 ix = find(HDR.EVENT.TYP(k)==BIOSIG_GLOBAL.EVENT.CodeIndex);
283 fprintf(fid,
'\t%s',BIOSIG_GLOBAL.EVENT.CodeDesc{ix});