1 function [HDR]=
mwfopen(HDR,PERMISSION,arg3,arg4,arg5,arg6)
2 % MWFOPEN reads MFER files
4 % HDR =
mwfopen(Filename,PERMISSION);
6 % HDR contains the Headerinformation and
internal data
8 % see also: SOPEN, SREAD, SSEEK, STELL, SCLOSE, SWRITE, SEOF
11 % This program is free software; you can redistribute it and/or
12 % modify it under the terms of the GNU General Public License
13 % as published by the Free Software Foundation; either version 3
14 % of the License, or (at your option) any later version.
16 % $Id:
mwfopen.m 2205 2009-10-27 12:18:15Z schloegl $
17 % (C) 2004,2007,2008 by Alois Schloegl <a.schloegl@ieee.orgA
18 % This is part of the BIOSIG-toolbox http:
23 if nargin<1, PERMISSION=
'rb'; end;
32 HDR.FILE.FID = fopen(HDR.FileName,PERMISSION,
'ieee-be');
33 HDR.Endianity=
'ieee-be';
35 %%% Default values %%%
36 HDR.SampleRate = 1000;
45 fprintf(2,
'Warning MWFOPEN: support of MFER format not complete, but in testing state\n');
47 % standard 12 leads ECG codes
48 ECG12LeadsCodes(1:9)={
'I';
'II';
'V1';
'V2';
'V3';
'V4';
'V5';
'V6';
'V1'};
49 ECG12LeadsCodes(11:15)={
'V3R';
'V4R';
'V5R';
'V6R';
'V7R'};
50 ECG12LeadsCodes(61:64)={
'III';
'aVR';
'aVL';
'aVF'};
51 ECG12LeadsCodes(66:69)={
'V8';
'V9';
'V8R';
'V9R'};
53 if ~isempty(findstr(PERMISSION,
'r')), %%%%% READ
57 %
while count>0, %~feof(HDR.FILE.FID)
58 tag = fread(HDR.FILE.FID,1,'uchar');
59 while ~feof(HDR.FILE.FID)
60 len = fread(HDR.FILE.FID,1,'uint8');
61 %fprintf(1,'[%i] Tag %i: (%i)\n',ftell(HDR.FILE.FID),tag,len);
63 if (len < 0) | (len > 127),
65 len = fread(HDR.FILE.FID,[l,1],'uchar');
66 %len = 256.^[0:l-1]*len;
67 len = 256.^[l-1:-1:0]*len;
69 % tmp = fread(HDR.FILE.FID,[1,len],'uint8');
70 % fprintf(1,'[%i] Tag %i: (%i)\n',ftell(HDR.FILE.FID),tag,len);
73 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uint8');
76 [tmp,count] = fread(HDR.FILE.FID,[1,1],'uint8');
79 [tmp,count] = fread(HDR.FILE.FID,[1,1],'uint8');
81 elseif (tmp==0) & strcmp(HDR.Endianity,'ieee-le'),
82 tmp = ftell(HDR.FILE.FID);
83 HDR.Endianity='ieee-be';
85 HDR.FILE.FID = fopen(HDR.FileName,PERMISSION,HDR.Endianity);
86 fseek(HDR.FILE.FID,tmp,'bof');
87 elseif (tmp==1) & strcmp(HDR.Endianity,'ieee-be'),
88 tmp = ftell(HDR.FILE.FID);
89 HDR.Endianity='ieee-le';
91 HDR.FILE.FID = fopen(HDR.FileName,PERMISSION,HDR.Endianity);
92 fseek(HDR.FILE.FID,tmp,'bof');
97 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uint8');
101 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uint8');
104 elseif tag==4; % channel number
108 [tmp,count] = fread(HDR.FILE.FID,1,'uint8');
110 [tmp,count] = fread(HDR.FILE.FID,1,'int16');
112 [tmp,count] = fread(HDR.FILE.FID,1,'bit24');
113 %[tmp,count] = fread(HDR.FILE.FID,3,'uint8');
114 %tmp = (2.^[16,8,0])*tmp;
116 [tmp,count] = fread(HDR.FILE.FID,1,'int32');
118 fprintf(2,'Error MWFOPEN: len=%i exceeds max length (4) in tag 04h\n',len);
120 %[HDR.SPR,count] = fread(HDR.FILE.FID,[1,len],'uchar');
124 % [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
128 [tmp,count] = fread(HDR.FILE.FID,1,'int8');
130 [tmp,count] = fread(HDR.FILE.FID,1,'int16');
132 [tmp,count] = fread(HDR.FILE.FID,1,'bit24');
133 %tmp = (2.^[16,8,0])*tmp;
134 %tmp = (2.^[0:8:16])*tmp;
136 [tmp,count] = fread(HDR.FILE.FID,1,'int32');
138 fprintf(2,'Error MWFOPEN: max length exceeded in tag 05h\n');
140 HDR.NS = tmp; %*256.^[len-1:-1:0]';
143 if len==0; tmp = NaN;
144 elseif len==1; [tmp,count] = fread(HDR.FILE.FID,1,'int8');
145 elseif len==2; [tmp,count] = fread(HDR.FILE.FID,1,'int16');
146 elseif len==3; [tmp,count] = fread(HDR.FILE.FID,1,'bit24')
147 % tmp = (2.^[16,8,0])*tmp;
148 elseif len==4; [tmp,count] = fread(HDR.FILE.FID,1,'int32');
149 else fprintf(2,'Error MWFOPEN: max length exceeded in tag 06h\n');
154 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
155 HDR.Pointer = tmp*256.^[0:len-1]';
158 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
159 if tmp==0, HDR.MFER.WaveFormType = 'undefined';
160 elseif tmp==1, HDR.MFER.WaveFormType = 'ECG_STD12';
161 elseif tmp==2, HDR.MFER.WaveFormType = 'ECG_LTERM';
162 elseif tmp==3, HDR.MFER.WaveFormType = 'Vectorcardiogram';
163 elseif tmp==4, HDR.MFER.WaveFormType = 'Stress_ECG';
164 elseif tmp==5, HDR.MFER.WaveFormType = 'ECG_LTERM';
165 else HDR.MFER.WaveFormType = tmp;
169 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
170 HDR.Label =
char(tmp);
173 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
174 if tmp==0; % int16, default
177 elseif tmp==1; % uint16
180 elseif tmp==2; % int32
183 elseif tmp==3; % uint8
186 elseif tmp==4; % 16bit status
189 elseif tmp==5; % int8
192 elseif tmp==6; % uint32
195 elseif tmp==7; % float32
198 elseif tmp==8; % float64
201 elseif tmp==9; % 8 bit AHA compression
204 fprintf(2,'Error MWFOPEN: compression not supported, yet.\n');
208 [tmp1,count] = fread(HDR.FILE.FID,2,'int8');
210 if len == 1; [tmp,count] = fread(HDR.FILE.FID,1,'int8');
211 elseif len == 2; [tmp,count] = fread(HDR.FILE.FID,1,'int16');
212 elseif len == 3; [tmp,count] = fread(HDR.FILE.FID,1,'bit24');
213 elseif len == 4; [tmp,count] = fread(HDR.FILE.FID,1,'int32');
218 HDR.SampleRate=tmp*e;
221 HDR.SampleRate= 1/(tmp*e);
224 HDR.SampleRate=tmp*e;
227 elseif tag==12; % sensitivity, resolution, gain, calibration,
228 [tmp,count] = fread(HDR.FILE.FID,2,'int8');
229 if tmp(1)== 0, HDR.PhysDimCode = 4256; %% V
230 elseif tmp(1)== 1, HDR.PhysDimCode = 3872; %% mmHg
231 elseif tmp(1)== 2, HDR.PhysDimCode = 3840; %% Pa
232 elseif tmp(1)== 3, HDR.PhysDimCode = 3904; %% cmH2O
233 elseif tmp(1)== 4, HDR.PhysDim = 'mmHg/S';
234 elseif tmp(1)== 5, HDR.PhysDimCode = 3808; %% dyne
235 elseif tmp(1)== 6, HDR.PhysDimCode = 3776; %% N
236 elseif tmp(1)== 7, HDR.PhysDimCode = 544; %% '%'
237 elseif tmp(1)== 8, HDR.PhysDimCode = 6048; %% °C
238 elseif tmp(1)== 9, HDR.PhysDimCode = 2528; %% 1/min
239 elseif tmp(1)==10, HDR.PhysDimCode = 4264; %% 1/s
240 elseif tmp(1)==11, HDR.PhysDimCode = 4288; %% Ohm
241 elseif tmp(1)==12, HDR.PhysDimCode = 4160; %% A
242 elseif tmp(1)==13, HDR.PhysDimCode = 65376; %% r.p.m.
243 elseif tmp(1)==14, HDR.PhysDimCode = 4032; %% W
244 elseif tmp(1)==15, HDR.PhysDimCode = 6448; %% dB
245 elseif tmp(1)==16, HDR.PhysDimCode = 1731; %% kg';
246 elseif tmp(1)==17, HDR.PhysDimCode = 3968; %% J';
247 elseif tmp(1)==18, HDR.PhysDimCode = 6016; %% dyne s m-2 cm-5';
248 elseif tmp(1)==19, HDR.PhysDim = 'k';
249 elseif tmp(1)==20, HDR.PhysDimCode = 3040; %% L/s';
250 elseif tmp(1)==21, HDR.PhysDimCode = 3072; %% L/m';
251 elseif tmp(1)==22, HDR.PhysDimCode = 4480; %% cd';
252 elseif tmp(1)==23, HDR.PhysDim = '';
253 elseif tmp(1)==24, HDR.PhysDim = '';
254 elseif tmp(1)==25, HDR.PhysDim = '';
258 if len == 1; [tmp,count] = fread(HDR.FILE.FID,1,'int8');
259 elseif len == 2; [tmp,count] = fread(HDR.FILE.FID,1,'int16');
260 elseif len == 3; [tmp,count] = fread(HDR.FILE.FID,1,'bit24');
261 elseif len == 4; [tmp,count] = fread(HDR.FILE.FID,1,'int32');
265 elseif tag==13; % offset
266 [tmp,count] = fread(HDR.FILE.FID,1,
gdfdatatype(HDR.GDFTYP));
269 elseif tag==14; % compression
270 %[tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar')
271 [HDR.MFER.CompressionCode,count] = fread(HDR.FILE.FID,1,'uint16');
272 [HDR.MFER.DataLength1,count] = fread(HDR.FILE.FID,1,'uint32');
273 [HDR.MFER.DataLength2,count] = fread(HDR.FILE.FID,1,'uint32');
274 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
276 HDR.FLAG.Compresssion=tmp;
279 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
282 elseif tag==18; % null value
283 [tmp,count] = fread(HDR.FILE.FID,1,
gdfdatatype(HDR.GDFTYP));
284 HDR.MFER.NullValue = tmp;
287 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
291 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
292 HDR.comment=
char(tmp);
295 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uchar');
296 HDR.tag23 =
char(tmp);
299 if isfield(HDR,'MFER');
300 if isfield(HDR.MFER,'SPR');
301 HDR.AS.spb = sum(HDR.MFER.SPR);
303 HDR.AS.spb = HDR.SPR*HDR.NS;
304 HDR.MFER.SPR = HDR.SPR(ones(1,HDR.NS));
308 HDR.SPR = HDR.MFER.SPR(1);
310 HDR.SPR = lcm(HDR.SPR,HDR.MFER.SPR(k));
313 HDR.FRAME.N = HDR.FRAME.N + 1;
314 HDR.FRAME.POS(HDR.FRAME.N) = ftell(HDR.FILE.FID);
315 HDR.FRAME.TYP(HDR.FRAME.N) = HDR.GDFTYP;
316 nos = len/(HDR.AS.bps*HDR.AS.spb);
317 HDR.FRAME.sz(HDR.FRAME.N,1:5) = [HDR.AS.spb,nos,HDR.NRec,HDR.NS,len];
318 HDR.FRAME.Fs(HDR.FRAME.N) = HDR.SampleRate;
320 fseek(HDR.FILE.FID,len,'cof');
324 chansel = mod(len,128)+1;
327 [len, count] = fread(HDR.FILE.FID,1,'uchar');
329 chansel(k) = mod(len,128)+1;
332 [len, count] = fread(HDR.FILE.FID,1,'uchar');
336 tag2 = fread(HDR.FILE.FID,1,'uchar');
337 len2 = fread(HDR.FILE.FID,1,'uint8');
338 len = len - 2 - len2;
344 [tmp,count] = fread(HDR.FILE.FID,1,'int8');
346 [tmp,count] = fread(HDR.FILE.FID,1,'int16');
348 [tmp,count] = fread(HDR.FILE.FID,1,'bit24');
350 [tmp,count] = fread(HDR.FILE.FID,1,'int32');
352 fprintf(2,'Error MWFOPEN: len=%i exceeds max length (4) in tag 04h\n',len);
354 %[HDR.SPR,count] = fread(HDR.FILE.FID,[1,len],'uchar');
355 HDR.MFER.SPR(chansel) = tmp;
356 HDR.SPR = lcm(tmp,HDR.SPR);
359 [tmp, count] = fread(HDR.FILE.FID,1,
gdfdatatype(HDR.GDFTYP));
360 HDR.Cal(chansel) = tmp;
362 [tmp, count] = fread(HDR.FILE.FID,1,
gdfdatatype(HDR.GDFTYP));
363 HDR.Off(chansel) = tmp;
365 [tmp, count] = fread(HDR.FILE.FID,[1,len2],'uchar');
366 if ~isfield(HDR,'Label')
367 HDR.Label(chansel,:) = ECG12LeadsCodes{tmp};
369 HDR.Label(chansel,1:length(ECG12LeadsCodes{tmp})) = ECG12LeadsCodes{tmp};
371 else %
if (tag2 == 10),
372 fprintf(2,
'Error MWFOPEN: Tag %i in channel-specific section not supported \n',tag2);
373 fclose(HDR.FILE.FID);
377 %fprintf(1,
'\t%i',chansel);
378 %fprintf(1,
'> [%i] Tag %i = %i: (%i) \n',ftell(HDR.FILE.FID),tag2,tmp,len2);
383 tag2 = fread(HDR.FILE.FID,1,
'uchar');
384 len2 = fread(HDR.FILE.FID,1,
'uint8');
385 %%
this part is not tested yet
386 fprintf(1,
'\t%i',chansel);
387 fprintf(1,
'> [%i] Tag %i: (%i)\n',ftell(HDR.FILE.FID),tag2,len2);
389 if (len2 < 0) | (len2 > 127),
391 len2 = fread(HDR.FILE.FID,[l,1],
'uchar');
392 %len = 256.^[0:l-1]*len;
393 len2 = 256.^[l-1:-1:0]*len2;
395 [tmp, count] = fread(HDR.FILE.FID,[1,len],
'uchar');
398 if isfield(HDR,
'Label')
399 HDR.Label =
char(HDR.Label);
402 elseif tag==64; % Preamble
403 [tmp,count] = fread(HDR.FILE.FID,[1,len],'uint8');
406 elseif tag==65; % Events
410 [HDR.EVENT.TYP(k),count] = fread(HDR.FILE.FID,1,'uint16');
412 [HDR.EVENT.POS(k),count] = fread(HDR.FILE.FID,1,'uint32');
415 [HDR.EVENT.DUR(k),count] = fread(HDR.FILE.FID,1,'uint32');
418 [HDR.EVENT.Desc{k},count] = fread(HDR.FILE.FID,len-10,
'uint8');
422 elseif tag==67; % Sample Skew
423 [tmp,count] = fread(HDR.FILE.FID,1,
'int16');
424 HDR.SampleSkew = tmp;
426 elseif tag==129; % Patient Name
427 [tmp,count] = fread(HDR.FILE.FID,[1,len],
'uint8');
428 HDR.Patient.Name = char(tmp);
430 elseif tag==130; % Patient Id
431 [tmp,count] = fread(HDR.FILE.FID,[1,len],
'uint8');
434 elseif tag==131; % Patient Age
435 [tmp,count] = fread(HDR.FILE.FID,[1,len],
'uint8');
436 HDR.Patient.Age = char(tmp);
438 elseif tag==132; % Patient Age
439 [tmp,count] = fread(HDR.FILE.FID,[1,len],
'uint8');
440 HDR.Patient.Sex = char(tmp);
442 elseif tag==133; % recording time
443 [HDR.T0(1),count] = fread(HDR.FILE.FID,1,
'int16');
444 [HDR.T0(2:6),count] = fread(HDR.FILE.FID,[1,5],
'uint8');
445 [tmp,count] = fread(HDR.FILE.FID,[1,2],
'int16');
446 HDR.T0(6) = HDR.T0(6) + tmp(1)*1e-3 + tmp(2)+1e-6;
449 [tmp,count] = fread(HDR.FILE.FID,[1,len],
'uint8');
450 %fprintf(1,
'[%i %i] Tag %i: (%i) %s\n',ftell(HDR.FILE.FID),count,tag,len,
char(tmp));
452 % fprintf(1,
'[%i %i] Tag %i: (%i)\n',ftell(HDR.FILE.FID),count,tag,len);
455 tag = fread(HDR.FILE.FID,1,
'uchar');
458 HDR.HeadLen = ftell(HDR.FILE.FID);
460 HDR.Calib = sparse([HDR.Off(ones(1,HDR.NS/length(HDR.Off)));eye(HDR.NS)]);
461 HDR.Calib = HDR.Calib * sparse(1:HDR.NS,1:HDR.NS,HDR.Cal);