0001 function [vv, auxdata, stim]= eidors_readdata( fname, format, frame_range, extra )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 if nargin>=1 && strcmp(fname,'UNIT_TEST'); do_unit_test; return; end
0080
0081 if ~exist(fname,'file')
0082 error([fname,' does not exist']);
0083 end
0084
0085 if nargin < 2
0086
0087 dotpos = find(fname == '.');
0088 if isempty( dotpos )
0089 error('file format unspecified, can`t autodetect');
0090 else
0091 dotpos= dotpos(end);
0092 format= fname( dotpos+1:end );
0093 end
0094 end
0095
0096
0097 auxdata = [];
0098 fmt = pre_proc_spec_fmt( format, fname );
0099 switch fmt
0100 case 'mceit';
0101 [vv,curr,volt,auxdata_out,auxtime,rawdata] = mceit_readdata( fname );
0102 auxdata.auxdata = auxdata_out;
0103 auxdata.auxtime = auxtime;
0104 auxdata.curr = curr;
0105 auxdata.volt = volt;
0106
0107
0108 if strcmp(lower(format),'get-raw')
0109 vv= rawdata(1:208,:);
0110 stim = mk_stim_patterns(16,1,[0,1],[0,1], {'no_meas_current','rotate_meas'}, 1);
0111 else
0112 stim = basic_stim(16);
0113 end
0114 case 'draeger-get'
0115 vv = draeger_get_readdata( fname );
0116
0117 stim = basic_stim(16);
0118
0119 case 'draeger-eit'
0120 [fr] = read_draeger_header( fname );
0121
0122 stim = mk_stim_patterns(16,1,[0,1],[0,1],{'rotate_meas','no_meas_current'},.005);
0123 [stim(:).framerate] = deal(fr);
0124 [vvOrig, tstamp, event, signals, counter] = read_draeger_file( fname );
0125
0126 ft = [.00098242, .00019607];
0127 vv = ft(1)*vvOrig(1:208,:) - ft(2)*vvOrig(322+(1:208),:);
0128 auxdata.vv = vvOrig;
0129 auxdata.tstamp = tstamp;
0130 auxdata.event = event;
0131 auxdata.signals = signals;
0132
0133
0134 fc = 194326.3536;
0135 auxdata.curr = vvOrig(224+(1:16), :) / fc;
0136 fv = 0.11771;
0137 auxdata.volt = 1/fv * (vvOrig(256+(1:16), :) - vvOrig(578+(1:16), :));
0138 auxdata.counter = counter;
0139
0140 case {'raw', 'sheffield'}
0141 vv = sheffield_readdata( fname );
0142
0143 stim = basic_stim(16);
0144 case {'p2k', 'its'}
0145 vv = its_readdata( fname );
0146
0147 stim = 'UNKNOWN';
0148 case {'txt','iirc'}
0149 vv = iirc_readdata( fname );
0150
0151 stim = basic_stim(16);
0152 case 'uct_seq'
0153 [vv,auxdata] = UCT_sequence_file( fname );
0154
0155 stim = 'UNKNOWN';
0156 case 'uct_cal'
0157 [vv,auxdata] = UCT_calibration_file( fname );
0158
0159 stim = 'UNKNOWN';
0160 case 'uct_data'
0161 [vv] = UCT_LoadDataFrame( fname );
0162
0163 stim = 'UNKNOWN';
0164 case {'carefusion','goeiimf-eit'}
0165 [vv, auxdata_out, auxtime] = carefusion_eit_readdata( fname );
0166 auxdata.auxdata = auxdata_out;
0167 auxdata.auxtime = auxtime;
0168
0169 stim = mk_stim_patterns(16,1,[0,1],[0,1], {'no_meas_current','rotate_meas'}, .005);
0170
0171 case 'lq1'
0172 [vv] = landquart1_readdata( fname );
0173
0174 stim = mk_stim_patterns(32,1,[0,5],[0,5],{'no_rotate_meas','no_meas_current'},.005);
0175
0176 case {'lq2','lq3'}
0177 [vv, elecImps, tStampsAbs, tStampsRel] = landquart2_readdata( fname );
0178 auxdata.elec_impedance = elecImps;
0179 auxdata.t_abs = tStampsAbs;
0180 auxdata.t_rel = tStampsRel;
0181
0182 stim = mk_stim_patterns(32,1,[0,5],[0,5],{'no_rotate_meas','no_meas_current'},.005);
0183
0184 case 'lq4pre'
0185 [vv] = landquart4pre_readdata( fname );
0186
0187 stim = mk_stim_patterns(32,1,[0,5],[0,5],{'no_rotate_meas','no_meas_current'},.005);
0188
0189 case 'lq4'
0190 [vv, evtlist, elecImps, tStampsAbs, tStampsRel, n_elec, amp, skip, extra, patPosition] = landquart4_readdata( fname );
0191 auxdata.event = evtlist;
0192 auxdata.elec_impedance = elecImps;
0193 auxdata.t_abs = tStampsAbs;
0194 auxdata.t_rel = tStampsRel;
0195 auxdata.pat_position = patPosition;
0196 for fn = fieldnames(extra)'
0197 auxdata.(fn{1}) = extra.(fn{1});
0198 end
0199
0200 [stim, auxdata.meas_sel] = mk_stim_patterns(n_elec,1,[0,skip+1],[0,skip+1],{'no_rotate_meas','no_meas_current'},amp);
0201
0202 case 'dixtal_encode'
0203 [vv] = dixtal_read_codepage( fname );
0204 stim = 'N/A';
0205
0206 case 'dixtal'
0207 [vv] = dixtal_read_data( fname, frame_range, extra );
0208 auxdata = vv(1025:end,:);
0209 vv = vv([1:1024],:);
0210 stim= mk_stim_patterns(32,1,[0,4],[0,4], ...
0211 {'meas_current','no_rotate_meas'}, 1);
0212
0213 otherwise
0214 error('eidors_readdata: file "%s" format unknown', format);
0215 end
0216
0217 function stim = basic_stim(N_el);
0218 stim= mk_stim_patterns(16,1,[0,1],[0,1], ...
0219 {'no_meas_current','no_rotate_meas'}, 1);
0220
0221 function fmt = pre_proc_spec_fmt( format, fname );
0222 fmt= lower(format);
0223 if strcmp(fmt,'get')
0224 if is_get_file_a_draeger_file( fname)
0225 fmt= 'draeger-get';
0226 else
0227 fmt= 'mceit';
0228 end
0229 end
0230
0231 if strcmp(fmt,'get-raw')
0232 fmt= 'mceit';
0233 end
0234
0235
0236 if strcmp(fmt,'eit')
0237 draeger = is_eit_file_a_draeger_file( fname );
0238 swisstom= is_eit_file_a_swisstom_file( fname );
0239 carefusion= is_eit_file_a_carefusion_file( fname );
0240 if carefusion
0241 eidors_msg('"%s" appears to be in GOEIIMF/Carefusion format',fname,3);
0242 fmt= 'carefusion';
0243 elseif draeger
0244 eidors_msg('"%s" appears to be in Draeger format',fname,3);
0245 fmt= 'draeger-eit';
0246 elseif swisstom
0247 fmt= sprintf('lq%d',swisstom);
0248 if swisstom == 3.5
0249 fmt= 'lq4pre';
0250 end
0251 eidors_msg('"%s" appears to be in %s format',fname,upper(fmt),3);
0252 else
0253 error('EIT file specified, but it doesn''t seem to be a Carefusion file')
0254 end
0255 end
0256
0257 function df= is_get_file_a_draeger_file( fname)
0258 fid= fopen(fname,'rb');
0259 d= fread(fid,[1 26],'uchar');
0260 fclose(fid);
0261 df = all(d == '---Draeger EIT-Software---');
0262
0263 function df= is_eit_file_a_draeger_file( fname );
0264 fid= fopen(fname,'rb');
0265 d= fread(fid,[1 80],'uchar');
0266 fclose(fid);
0267 ff = strfind(char(d), '---Draeger EIT-Software');
0268 if ff;
0269 df = 1;
0270 eidors_msg('Draeger format: %s', d(ff(1)+(0:30)),4);
0271 else
0272 df = 0;
0273 end
0274
0275 function df= is_eit_file_a_swisstom_file( fname );
0276 fid= fopen(fname,'rb');
0277 d= fread(fid,[1 4],'uchar');
0278 fclose(fid);
0279
0280
0281
0282
0283 if any(d(4)==[2,3,4]) && all(d([1,2,3]) == 0);
0284 df = d(4);
0285 if d(4)==4; df = 3.5; end
0286
0287 elseif any(d(1:4) == [4,0,0,0])
0288 df = 4;
0289 else
0290 df = 0;
0291 end
0292
0293 function df = is_eit_file_a_carefusion_file( fname )
0294 fid= fopen(fname,'rb');
0295 d= fread(fid,[1 80],'uchar');
0296 fclose(fid);
0297 df = 0;
0298 if d(1:2) ~= [255,254]; return; end
0299 if d(4:2:end) ~= 0; return; end
0300 str= char(d(3:2:end));
0301 tests1= '<?xml version="1.0" ?>';
0302 tests2= '<EIT_File>';
0303 if ~any(strfind(str,tests1)); return; end
0304 if ~any(strfind(str,tests2)); return; end
0305
0306 df= 1;
0307 return
0308
0309 function [vv, auxdata, auxtime] = carefusion_eit_readdata( fname );
0310 fid= fopen(fname,'rb');
0311 d= fread(fid,[1 180],'uchar');
0312 str= char(d(3:2:end));
0313 outv = regexp(str,'<Header>(\d+) bytes</Header>','tokens');
0314 if length(outv) ~=1;
0315 error('format problem reading carefusion eit files');
0316 end
0317 header = str2num(outv{1}{1});
0318
0319
0320
0321
0322 fseek(fid, header, -1);
0323 d_EIT= fread(fid,[inf],'float32');
0324
0325 fseek(fid, header, -1);
0326 d_struct= fread(fid,[inf],'int32');
0327 fclose(fid);
0328 pt_EIT=1;
0329 vv=[];
0330 auxdata=[];
0331
0332 while (pt_EIT<=length(d_struct))
0333 switch d_struct(pt_EIT)
0334 case 3,
0335
0336 vv(1:256,1+d_struct(pt_EIT+2))= d_EIT(pt_EIT+6:pt_EIT+6+255);
0337 pt_EIT=pt_EIT+6+256;
0338 case 7,
0339 pt_EIT=pt_EIT+912+6;
0340 case 8,
0341 aux_seg_len = d_struct(pt_EIT+3)/4*d_struct(pt_EIT+4);
0342 if (d_struct(pt_EIT+1) == 0)
0343 aux_segment = d_EIT(pt_EIT+6 : pt_EIT+6+aux_seg_len-1);
0344 auxdata = [auxdata; aux_segment];
0345 else
0346
0347 end
0348 pt_EIT=pt_EIT+6+aux_seg_len;
0349 case 10,
0350
0351 pt_EIT=pt_EIT+6+d_struct(pt_EIT+3)/4*d_struct(pt_EIT+4);
0352 otherwise,
0353 eidors_msg('WARNING: unknown type in carefusion file type');
0354 pt_EIT=pt_EIT+6+d_struct(pt_EIT+3)/4*d_struct(pt_EIT+4);
0355 end
0356 end
0357 vv=vv(47+[1:208],:);
0358 auxtime = (cumsum([1 13*ones(1,15)])-1)/208;
0359 auxtime = reshape(repmat(1:size(vv,2), length(auxtime), 1),[],1) + repmat(auxtime, 1, size(vv,2))';
0360
0361
0362
0363 function [vv,curr,volt,auxdata] = draeger_get_readdata( fname );
0364 fid= fopen(fname,'rb');
0365 emptyctr=0;
0366 while emptyctr<2
0367 line= fgetl(fid);
0368 if isempty(line)
0369 emptyctr= emptyctr+1;
0370 else
0371 eidors_msg('data not understood',0);
0372 emptyctr=0;
0373 end
0374 end
0375 d= fread(fid,inf,'float',0,'ieee-le');
0376 fclose(fid);
0377
0378 if rem( length(d), 256) ~=0
0379 eidors_msg('File length strange - cropping file',0);
0380 d=d(1:floor(length(d)/256)*256);
0381 end
0382
0383 dd= reshape( d, 256, length(d)/256);
0384 vv= untwist(dd);
0385
0386 curr=0.00512*dd(209:224,:);
0387 volt=12*dd(225:240,:);
0388 auxdata= dd(241:255,:);
0389 auxdata= auxdata(:);
0390
0391 function jnk
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425 function vv = sheffield_readdata( fname );
0426 fid=fopen(fname,'rb','ieee-le');
0427 draw=fread(fid,[104,inf],'float32');
0428 fclose(fid);
0429 ldat = size(draw,2);
0430
0431 [x,y]= meshgrid( 1:16, 1:16);
0432 idxm= y-x;
0433
0434 gtbl = [0,849,213,87,45,28,21,19,21,28,45,87,213,849,0];
0435 idxm(idxm<=0)= 1;
0436 idxm= gtbl(idxm);
0437
0438
0439 draw = draw .* (idxm(idxm>0) * ones(1,ldat));
0440
0441 vv= zeros(16*16, size(draw,2));
0442 vv(find(idxm),:) = draw;
0443
0444
0445 vv= reshape(vv,[16,16,ldat]);
0446 vv= vv + permute( vv, [2,1,3]);
0447 vv= reshape(vv,[16*16,ldat]);
0448
0449
0450
0451 function [vv,curr,volt,auxdata,auxtime,rawdata] = mceit_readdata( fname );
0452
0453 fid= fopen(fname,'rb');
0454 d= fread(fid,inf,'float');
0455 fclose(fid);
0456
0457 if rem( length(d), 256) ~=0
0458 eidors_msg('File length strange - cropping file',0);
0459 d=d(1:floor(length(d)/256)*256);
0460 end
0461
0462 dd= reshape( d, 256, length(d)/256);
0463 rawdata = dd;
0464 no_reciprocity = (dd(39,1)==0);
0465 if no_reciprocity
0466 dd=transfer104_208(dd);
0467 end
0468 vv= untwist(dd);
0469
0470 curr=0.00512*dd(209:224,:);
0471 volt=12*dd(225:240,:);
0472 auxdata= dd(241:256,:);
0473 auxdata= auxdata(:);
0474
0475
0476 if no_reciprocity
0477
0478 auxdata(14:16:end) = []; auxdata(14:15:end) = []; auxdata(14:14:end) = [];
0479
0480
0481
0482
0483
0484 auxtime = [0.0000,0.1390,0.2535,0.3617,0.4642,0.5486,0.6367,0.7110,0.7780,0.8354,0.8865,0.9304,0.9711];
0485
0486 else
0487
0488 auxtime = (cumsum([1 13*ones(1,15)])-1)/208;
0489 end
0490
0491 auxtime = reshape(repmat(1:size(dd,2), length(auxtime), 1),[],1) + repmat(auxtime, 1, size(dd,2))';
0492
0493
0494 function array208=transfer104_208(array104),
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512 ind=[39,51,63,75,87,99,111,123,135,147,159,171,183,52,64,76, ...
0513 88,100,112,124,136,148,160,172,184,196,65,77,89,101,113, ...
0514 125,137,149,161,173,185,197,78,90,102,114,126,138,150,162, ...
0515 174,186,198,91,103,115,127,139,151,163,175,187,199,104,116, ...
0516 128,140,152,164,176,188,200,117,129,141,153,165,177,189, ...
0517 201,130,142,154,166,178,190,202,143,155,167,179,191,203, ...
0518 156,168,180,192,204,169,181,193,205,182,194,206,195,207,208];
0519 ro=[1:13, 14:26, 27:38, 40:50, 53:62, 66:74, 79:86, 92:98, ...
0520 105:110,118:122,131:134,144:146,157:158,170:170];
0521
0522 [x,y]=size(array104);
0523 if x~=256 && y~=256,
0524 eidors_msg(['eidors_readdata: expectingin an input array ', ...
0525 'of size 208*n']);
0526 return;
0527 elseif y==256,
0528 array104=array104';
0529 y=x;
0530 end
0531 array208=array104;
0532 for i=1:y,
0533 array208(ind,i)=array104(ro,i);
0534 end
0535
0536
0537
0538 function vv= untwist(dd);
0539 elec=16;
0540 pos_i= [0,1];
0541 ELS= rem(rem(0:elec^2-1,elec) - ...
0542 floor((0:elec^2-1)/elec)+elec,elec)';
0543 ELS=~any(rem( elec+[-1 0 [-1 0]+pos_i*[-1;1] ] ,elec)' ...
0544 *ones(1,elec^2)==ones(4,1)*ELS')';
0545 twist= [ 0+(1:13), ...
0546 13+(1:13), ...
0547 39-(0:-1:0), 26+(1:12), ...
0548 52-(1:-1:0), 39+(1:11), ...
0549 65-(2:-1:0), 52+(1:10), ...
0550 78-(3:-1:0), 65+(1: 9), ...
0551 91-(4:-1:0), 78+(1: 8), ...
0552 104-(5:-1:0), 91+(1: 7), ...
0553 117-(6:-1:0), 104+(1: 6), ...
0554 130-(7:-1:0), 117+(1: 5), ...
0555 143-(8:-1:0), 130+(1: 4), ...
0556 156-(9:-1:0), 143+(1: 3), ...
0557 169-(10:-1:0),156+(1: 2), ...
0558 182-(11:-1:0),169+(1: 1), ...
0559 195-(12:-1:0), ...
0560 208-(12:-1:0) ];
0561 vv= zeros(256,size(dd,2));
0562 vv(ELS,:)= dd(twist,:);
0563
0564
0565
0566
0567
0568 function vv = its_readdata( fname )
0569 fid= fopen( fname, 'rb', 'ieee-le');
0570 vv=[];
0571
0572
0573 header= fread(fid, 880, 'uchar');
0574 frameno= 0;
0575 rings= 8;
0576 while( ~feof(fid) )
0577 frameno= frameno+1;
0578
0579 framehdr= fread(fid, 40);
0580 data= fread(fid, 104*rings, 'double');
0581 vv= [vv, data];
0582 end
0583
0584 if 0
0585 ringno= 1;
0586 ld= size(vv,2);
0587 vx= [zeros(1,ld);vv( ringno*104 + (-103:0) ,: )];
0588 idx= ptr104_208;
0589 vv= vx(idx+1,:);
0590 end
0591
0592
0593 function idx= ptr104_208;
0594 idx= zeros(16);
0595 idx(1,:)= [0,0,1:13,0];
0596 ofs= 13;
0597 for i= 2:14
0598 mm= 15-i;
0599 idx(i,:) = [zeros(1,i+1), (1:mm)+ofs ];
0600 ofs= ofs+ mm;
0601 end
0602
0603 idx= idx + idx';
0604
0605 function vv = iirc_readdata( fname );
0606 fid= fopen( fname, 'r');
0607 while ~feof(fid)
0608 line = fgetl(fid);
0609 if isempty(line)
0610 continue;
0611 end
0612
0613 num= regexp(line,'Channel : (\d+)');
0614 if ~isempty(num)
0615 channels= str2num( line(num(1):end ) );
0616 continue;
0617 end
0618
0619 num= regexp(line,'Frequency : (\d+)kHz');
0620 if ~isempty(num)
0621 freqency= str2num( line(num(1):end ) );
0622 continue;
0623 end
0624
0625 num= regexp(line,'Scan Method : (\w+)');
0626 if ~isempty(num)
0627 scan_method= line(num(1):end );
0628 continue;
0629 end
0630
0631 num= regexp(line,'Study : (\w+)');
0632 if ~isempty(num)
0633 study= line(num(1):end);
0634 continue;
0635 end
0636
0637 if strcmp(line,'Data');
0638 data= fscanf(fid,'%f',[4,inf])';
0639 continue;
0640 end
0641 end
0642 vv= data(:,1) + 1i*data(:,2);
0643 if length(vv) ~= channels^2
0644 error('eidors_readdata: data length wrong')
0645 end
0646
0647
0648
0649 function [stimulations,meas_select] = UCT_sequence_file( fname );
0650
0651
0652
0653
0654
0655
0656 fid = fopen(fname, 'rt');
0657
0658
0659 if fid == -1
0660 errordlg('File not found','ERROR')
0661 return;
0662 end
0663
0664
0665 tline = fgetl(fid);
0666
0667
0668
0669
0670
0671
0672
0673
0674 tokenlist = [];
0675
0676
0677 tline = fgetl(fid);
0678
0679 while length(tline) ~= 0
0680
0681
0682 rem = tline(11:end);
0683
0684
0685 while length(rem) ~=0
0686 [token, rem] = strtok(rem, ',');
0687 tokenlist = [tokenlist; token];
0688 end
0689
0690
0691 tline = fgetl(fid);
0692 end
0693
0694 fclose(fid);
0695
0696
0697
0698 drive_lay = [];
0699 drive_elec = [];
0700 sense_lay = [];
0701
0702 injection_no = 1;
0703
0704 for i=1:3:length(tokenlist)
0705
0706
0707 tsource_layer = tokenlist(i,3);
0708 tsink_layer = tokenlist(i,4);
0709 source_layer = sscanf(tsource_layer, '%x');
0710 sink_layer = sscanf(tsink_layer, '%x');
0711
0712 drive_lay = [drive_lay; [source_layer sink_layer]];
0713
0714
0715
0716 tsource_elec = tokenlist(i+1,3);
0717 tsink_elec = tokenlist(i+1,4);
0718 source_elec = sscanf(tsource_elec, '%x');
0719 sink_elec = sscanf(tsink_elec, '%x');
0720
0721 drive_elec = [drive_elec; [source_elec sink_elec]];
0722
0723
0724
0725 tpos_layer = tokenlist(i+2,3);
0726 tneg_layer = tokenlist(i+2,4);
0727 pos_sense_layer = sscanf(tpos_layer, '%x');
0728 neg_sense_layer = sscanf(tneg_layer, '%x');
0729
0730 sense_lay = [sense_lay; [pos_sense_layer neg_sense_layer]];
0731 end
0732
0733 n_elec = size(sense_lay,1);
0734 n_inj = size(drive_lay,1);
0735 elecs_per_plane = 16;
0736 raw_index = 0;
0737 meas_select = [];
0738
0739 for i=1:n_inj
0740 stimulations(i).stimulation= 'mA';
0741
0742
0743 e_inj_p = drive_lay(i, 1) * elecs_per_plane + drive_elec(i,1) + 1;
0744 e_inj_n = drive_lay(i, 2) * elecs_per_plane + drive_elec(i,2) + 1;
0745
0746
0747 inj = zeros(n_elec, 1);
0748 inj(e_inj_p) = 1;
0749 inj(e_inj_n) = -1;
0750 stimulations(i).stim_pattern = inj;
0751
0752
0753
0754
0755 meas_pat = [];
0756 for e = 0:15
0757 raw_index = raw_index + 1;
0758 meas = zeros(1, n_elec);
0759
0760
0761
0762 e_meas_p = sense_lay(i,1) * elecs_per_plane + mod(e+1,elecs_per_plane) + 1;
0763 e_meas_n = sense_lay(i,2) * elecs_per_plane + e + 1;
0764
0765
0766
0767 if any( e_meas_p == [e_inj_p, e_inj_n] ) | ...
0768 any( e_meas_n == [e_inj_p, e_inj_n] )
0769 continue;
0770 end
0771
0772 meas(e_meas_p) = -1;
0773 meas(e_meas_n) = 1;
0774 meas_select = [meas_select;raw_index];
0775
0776
0777 meas_pat = [meas_pat; meas];
0778
0779 end
0780 stimulations(i).meas_pattern = sparse(meas_pat);
0781 end
0782
0783 function [cur_data,no_cur_data] = UCT_calibration_file( fname );
0784 fid = fopen(fname, 'rb');
0785 mag_num = fread(fid, 1, 'int32');
0786 version = fread(fid, 1, 'int32');
0787
0788 comments = fread(fid, 2048, 'uint8');
0789 comments = char(comments');
0790 no_of_layers = fread(fid, 1, 'int32');
0791 uppa_dac = fread(fid, 1, 'float64');
0792 lowa_dac = fread(fid, 1, 'float64');
0793 raw_frame_size = fread(fid, 1, 'int32');
0794
0795 no_cur_data = [];
0796 cur_data = [];
0797
0798 for i=1:no_of_layers
0799 no_cur_data = [no_cur_data;fread(fid, raw_frame_size, 'float64')];
0800 cur_data = [cur_data;fread(fid, raw_frame_size, 'float64')];
0801 end
0802 fclose(fid);
0803
0804
0805
0806
0807 function [v_raw] = UCT_LoadDataFrame(infilename)
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818 fid = fopen(infilename, 'rb');
0819
0820 if fid == -1
0821 errordlg('File not found','ERROR')
0822 return;
0823 end
0824
0825
0826 magic_number = fread(fid, 1, 'uint32');
0827
0828 if magic_number ~= 2290649224
0829 disp('UCT File: wrong file type');
0830 end
0831 version = fread(fid, 1, 'uint32');
0832 foffset = fread(fid, 1, 'uint32');
0833 no_of_layers = fread(fid, 1, 'uint32');
0834 frame_size = fread(fid, 1, 'uint32');
0835 fseek(fid, foffset-8, 'cof');
0836
0837
0838
0839
0840
0841
0842 frame_no = fread(fid, 1, 'uint32');
0843
0844 v_raw = [];
0845 while feof(fid)==0
0846 v_raw = [v_raw, fread(fid, frame_size*no_of_layers, 'float64')];
0847 frame_no = fread(fid, 1, 'uint32');
0848 end
0849
0850 fclose(fid);
0851
0852
0853
0854
0855 function [vv] = landquart1_readdata( fname );
0856 FrameSize = 1024;
0857
0858 enableCounter=1;
0859 counter=0;
0860
0861 fid=fopen(fname);
0862
0863 codec=fread(fid,1,'int');
0864 if codec~=1; error('Codec unexpected value'); end
0865
0866 nbFileInHeader=fread(fid,1,'int');
0867
0868 for i=1:nbFileInHeader
0869 lenghtFile = fread(fid,1,'int64');
0870 jnk= fread(fid,lenghtFile,'int8');
0871 end
0872
0873
0874 vv= [];
0875 while 1;
0876 type=fread(fid,1,'int');
0877 if type == -1; break ; end
0878
0879 nel= fread(fid,1,'int');
0880 sz= fread(fid,1,'int');
0881 iq=fread(fid,[2,sz/8],'int')';
0882
0883 vv = [vv, iq*[1;1j]];
0884
0885 for j=1:nel-1
0886 sz= fread(fid,1,'int');
0887 jnk = fread(fid,sz/4,'int');
0888 end
0889
0890 end
0891
0892
0893 function [vv, elecImps, tStampsAbs, tStampsRel] = landquart2_readdata( fname )
0894 [fid msg]= fopen(fname,'r','ieee-be','UTF-8');
0895 try
0896 format_version = fread(fid,1,'int32','ieee-be');
0897 if format_version ~= 3
0898 error('unsupported file format version');
0899 else
0900
0901 fseek(fid,16,'cof');
0902 nFrames = fread(fid, 1, 'int32', 'ieee-be');
0903 tStampsAbs = nan(1, nFrames);
0904 tStampsRel = nan(1, nFrames);
0905 viPayload = nan(64, nFrames);
0906 iqPayload = nan(2048, nFrames);
0907
0908 progress_msg('Reading EIT frame', 0, nFrames);
0909
0910 header_size = 2264;
0911 fseek(fid,header_size + 8,'bof');
0912
0913
0914 frame_length = fread(fid, 1, 'int32', 'ieee-be') + 12;
0915
0916 fseek(fid,header_size,'bof');
0917
0918
0919 i = 1;
0920 while fseek(fid, 1,'cof') ~= -1
0921 if mod(i, 100) == 0
0922 progress_msg(i, nFrames);
0923 end
0924
0925 fseek(fid, -1,'cof');
0926
0927 tStampsAbs(i) = fread(fid,1,'int64','ieee-be');
0928 pl = fread(fid,1,'int32','ieee-le');
0929 frame_header = fread(fid,15,'int32','ieee-le');
0930
0931 tStampsRel(i) = frame_header(5);
0932
0933
0934 fseek(fid,12, 'cof');
0935
0936
0937 viPayload(:,i) = fread(fid,64,'int32','ieee-le');
0938
0939 iqPayload(:,i) = fread(fid,2048,'int32','ieee-le');
0940 fseek(fid,header_size + i*frame_length,'bof');
0941 i = i +1;
0942 end
0943
0944 end
0945 catch err
0946 fclose(fid);
0947 rethrow(err);
0948 end
0949 fclose(fid);
0950
0951 progress_msg(inf);
0952
0953 i = i-1;
0954 if i ~= nFrames
0955
0956 if i < nFrames
0957 tStampsAbs(i+1:end) = [];
0958 tStampsRel(i+1:end) = [];
0959 viPayload(:,i+1:end) = [];
0960 iqPayload(:,i+1:end) = [];
0961 end
0962 eidors_msg('"%s": expected %.0f frames but read %.0f',fname, nFrames, i ,3);
0963 end
0964
0965
0966 tStampsAbs = tStampsAbs/(24*3600*1E3) + datenum(1970, 1, 1, 1, 0, 0);
0967
0968
0969
0970
0971
0972 amplitudeFactor = 2.048 / (2^20 * 360 * 1000);
0973 vv = amplitudeFactor * (iqPayload(1:2:end,:) + 1i*iqPayload(2:2:end,:));
0974
0975 elecImps = viPayload(1:2:end,:) + 1i*viPayload(2:2:end,:);
0976
0977
0978 function [vv, evtlist, elecImps, tStampsAbs, tStampsRel, n_elec, amp, skip, extra, patPosition] = landquart4_readdata( fname )
0979 evtlist = [];
0980 [fid msg]= fopen(fname,'r','ieee-le','UTF-8');
0981 try
0982 format_version = fread(fid,1,'int32','ieee-le');
0983 if format_version ~= 4
0984 error('unsupported file format version');
0985 else
0986 header_size = fread(fid,1,'int32', 'ieee-le');
0987 eit_frame_offset = 328;
0988 iq_payload = 2048;
0989 vi_payload = 64;
0990 pat_position = 3;
0991
0992
0993 fseek(fid,16,'cof');
0994 nFrames = fread(fid, 1, 'int32', 'ieee-le');
0995 tStampsAbs = nan(1, nFrames);
0996 tStampsRel = nan(1, nFrames);
0997 viPayload = nan(vi_payload, nFrames);
0998 iqPayload = nan(iq_payload, nFrames);
0999 patPosition = nan(pat_position, nFrames);
1000
1001 progress_msg('Reading EIT frame', 0, nFrames);
1002 fseek(fid,424,'bof');
1003 x = fread(fid, 2, 'int16', 'ieee-le');
1004 extra.filename = fread(fid,[1,100], 'int16=>char', 'ieee-le');
1005 extra.conditions = fread(fid,[1,300], 'int16=>char', 'ieee-le');
1006 extra.comments = fread(fid,[1,600], 'int16=>char', 'ieee-le');
1007
1008
1009 extra.frame_rate = fread(fid, 1, 'float', 'ieee-le');
1010 amp = fread(fid, 1, 'float', 'ieee-le');
1011 extra.freq = fread(fid, 1, 'float', 'ieee-le');
1012 extra.settle = fread(fid, 1, 'float', 'ieee-le');
1013
1014
1015 skip = fread(fid, 1, 'int16', 'ieee-le');
1016 x = fread(fid, 1, 'int16', 'ieee-le');
1017 n_elec = fread(fid, 1, 'int16', 'ieee-le');
1018
1019 fseek(fid,header_size,'bof');
1020
1021
1022 i = 1;
1023 evti = 1;
1024 while fseek(fid, 1,'cof') ~= -1
1025 if mod(i, 100) == 0
1026 progress_msg(i, nFrames);
1027 end
1028
1029 fseek(fid, -1,'cof');
1030
1031
1032 tStampsAbs(i) = fread(fid,1,'int64','ieee-le');
1033 ft = fread(fid,1,'int32','ieee-le');
1034 pl = fread(fid,1,'int32','ieee-le');
1035 if ft == 1
1036
1037 evtlist(evti).timestamp = tStampsAbs(i) ;
1038 evtlist(evti).frame = i ;
1039 evti = evti + 1;
1040 if pl > 0
1041 evtlist(evti).eventId = fread(fid, 1, 'int32', 'ieee-le');
1042 end
1043 elseif ft == 0
1044 frame_header = fread(fid,15,'int32','ieee-le');
1045
1046 tStampsRel(i) = frame_header(5);
1047
1048
1049 patPosition(:, i) = fread(fid,pat_position,'int32','ieee-le');
1050
1051 viPayload(:,i) = fread(fid,vi_payload,'int32','ieee-le');
1052
1053 iqPayload(:,i) = fread(fid,iq_payload,'int32','ieee-le');
1054 fseek(fid,pl-4*iq_payload-eit_frame_offset,'cof');
1055 i = i+1;
1056 elseif pl > 0
1057 fseek(fid,pl,'cof');
1058 else
1059
1060 end
1061 end
1062 end
1063 catch err
1064 fclose(fid);
1065 rethrow(err);
1066 end
1067 fclose(fid);
1068
1069 progress_msg(inf);
1070
1071 i = i-1;
1072 if i ~= nFrames
1073
1074 if i < nFrames
1075 tStampsAbs(i+1:end) = [];
1076 tStampsRel(i+1:end) = [];
1077 viPayload(:,i+1:end) = [];
1078 iqPayload(:,i+1:end) = [];
1079 patPosition(:,i+1:end) = [];
1080 end
1081 eidors_msg('"%s": expected %.0f frames but read %.0f',fname, nFrames, i ,3);
1082 end
1083
1084
1085 tStampsAbs = tStampsAbs/(24*3600*1E3) + datenum(1, 1, 1, 0, 0, 0);
1086
1087
1088
1089
1090
1091 amplitudeFactor = 2.048 / (2^20 * 360 * 1000);
1092 vv = amplitudeFactor * (iqPayload(1:2:end,:) + 1i*iqPayload(2:2:end,:));
1093
1094
1095
1096 impedanceFactor = 2.048 / (2^12 * 0.173 * 0.003) / 2^15;
1097 elecImps = impedanceFactor * (viPayload(1:2:end,:) + 1i*viPayload(2:2:end,:));
1098
1099 function [vv] = landquart4pre_readdata( fname )
1100 [fid msg]= fopen(fname,'r','ieee-le','UTF-8');
1101 try
1102 format_version = fread(fid,1,'int32','ieee-le');
1103 if format_version ~= 4
1104 error('unsupported file format version');
1105 else
1106 header_size = fread(fid,1,'int32', 'ieee-le');
1107
1108 fseek(fid,header_size + 12,'bof');
1109
1110 frame_length = fread(fid, 1, 'int32', 'ieee-le') + 16;
1111
1112 fseek(fid,header_size,'bof');
1113
1114
1115 i = 1;
1116 while fseek(fid, 1,'cof') ~= -1
1117 fseek(fid, -1,'cof');
1118
1119
1120 fseek(fid,344, 'cof');
1121 iqPayload(:,i) = fread(fid,2048,'int32','ieee-le');
1122 fseek(fid,header_size + i*frame_length,'bof');
1123 i = i +1;
1124 end
1125
1126 end
1127 catch err
1128 fclose(fid);
1129 rethrow(err);
1130 end
1131 fclose(fid);
1132
1133
1134 amplitudeFactor = 2.048 / (2^20 * 360 * 1000);
1135 vv = amplitudeFactor * (iqPayload(1:2:end,:) + 1i*iqPayload(2:2:end,:));
1136
1137
1138
1139 function [vv] = dixtal_read_codepage( fname );
1140
1141 fid = fopen(fname,'rb');
1142 b1234= fread(fid, [1,4], 'uint8');
1143 if b1234~= [77, 90, 144, 0];
1144 error('This does not appear to be the correct Dll');
1145 end
1146 fseek(fid, hex2dec('e00'), 'bof');
1147 encodepage1 = fread(fid, hex2dec('1000'), 'uint8');
1148 fclose(fid);
1149 encodepage1 = flipud(reshape(encodepage1,4,[]));
1150 vv = encodepage1(:);
1151
1152
1153
1154 function [vv] = dixtal_read_data( file_name, frame_range, encodepage1 );
1155
1156
1157 fid=fopen(file_name,'rb');
1158 n1 = fgetl(fid);
1159 n2 = fgetl(fid);
1160 n3 = fgetl(fid);
1161 nframes = str2num( n3 );
1162
1163 fseek(fid, hex2dec('800'), 'bof');
1164 encodepage2 = fread(fid, hex2dec('1000'), 'uint8');
1165 fclose(fid);
1166
1167 encodepage = bitxor(encodepage1, encodepage2);
1168
1169
1170 dplen = hex2dec('1090');
1171 start = hex2dec('2800');
1172 fid=fopen(file_name,'rb');
1173 if isempty(frame_range)
1174 frame_range = 0:nframes;
1175 else
1176 frame_range = frame_range - 1;
1177 frame_range(frame_range>nframes) = [];
1178 end
1179 vv= zeros(dplen/4, length(frame_range));
1180 k= 1;
1181 for i = frame_range
1182 status= fseek(fid, start + i*dplen, 'bof');
1183 if status~=0; break; end
1184 datapage = fread(fid, dplen, 'uint8');
1185 if length(datapage)== 0;
1186 vv= vv(:,1:k-1); break;
1187 end
1188 vv(:,k) = proc_dixtal_data( datapage, encodepage );
1189 k=k+1;
1190 end
1191 fclose(fid);
1192
1193
1194 function data = proc_dixtal_data( b, encodepage );
1195
1196 cryptolen = 1024*4;
1197 b(1:cryptolen) = bitxor(b(1:cryptolen), encodepage);
1198
1199 b1 = b(1:4:end,:);
1200 b2 = b(2:4:end,:);
1201 b3 = b(3:4:end,:);
1202 b4 = b(4:4:end,:);
1203 bb = [b1,b2,b3,b4]*(256.^[3;2;1;0]);
1204
1205 sgnbit = bitand( bb, 2^31);
1206 sgnbit = +1*(sgnbit == 0) - 1*(sgnbit == 2^31);
1207
1208 expbit = bitand( bb, 255*2^23 ) /2^23;
1209 expbit = 2.^(expbit - 127);
1210
1211 fracbt = bitand( bb, 2^23-1)/2^23 + 1;
1212 data = sgnbit .* expbit .* fracbt;
1213
1214 function [fr] = read_draeger_header( filename );
1215
1216
1217
1218
1219
1220
1221
1222 K0 = 2048;
1223 K1='Framerate [Hz]:';
1224 K2=15;
1225 K3=13;
1226
1227
1228 fid = fopen(filename,'r','l');
1229 if fid == -1
1230 error('Error read_draeger_header: can''t open file');
1231 end
1232 header = fread(fid,K0,'*char')';
1233 index = strfind(header,K1);
1234 if ~isempty(index)
1235 [tok,rem]= strtok(header(index+K2:end),K3);
1236 fr = str2num(tok);
1237 else
1238 error('Error read_draeger_header: frame rate unspecified');
1239 end
1240
1241 if isempty(fr)
1242 error('Error read_draeger_header: Frame rate could not be read');
1243 end
1244
1245 fclose(fid);
1246
1247
1248 function [vd, tstamp, event, signals, counter] = read_draeger_file(filename)
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260 ff = dir(filename);
1261 filelen = ff.bytes;
1262
1263
1264 fid = fopen(filename,'r','l');
1265 if fid == -1
1266 error('Error read_draeger_file: file could not be opened');
1267 end
1268
1269
1270 K0 = 128;
1271
1272 header = fread(fid,K0,'*char')';
1273 if 0
1274 if ~isempty(strfind(header,'V3.2'))
1275 version = '3.2';
1276 elseif ~isempty(strfind(header,'V4.01'))
1277 version = '4.01';
1278 elseif ~isempty(strfind(header,'V1.10'))
1279 version = '1.10';
1280 else
1281 error('Error read_draeger_file: unknown file version');
1282 end
1283 end
1284
1285
1286 fseek(fid,0,-1);
1287 hdr = fread(fid, 8, 'uint8');
1288 offset1 = (256.^(0:3))*hdr(5:8);
1289 switch sprintf('%02X-',header(1:4));
1290 case '1F-00-00-00-';
1291 type='Draeger v31'; SPC = 4112;
1292 case '20-00-00-00-';
1293 type='Draeger v32'; SPC = 5200;
1294 case '33-00-00-00-';
1295 type='Draeger v51'; SPC = 5495;
1296 otherwise;
1297 error('File "%s" is format version %d, which we don''t know how to read', ...
1298 hdr(1))
1299 end
1300
1301 len = floor( (filelen-offset1)/SPC );
1302 vd= zeros(600,len);
1303 tstamp = zeros(1, len, 'double');
1304 counter = zeros(1, len, 'uint16');
1305 event = repmat(char(0), 30, len);
1306 rawsigs = zeros(67, len, 'single');
1307 ss= offset1 + (0:len)*SPC;
1308
1309 progress_msg('Reading EIT frame', 0, length(ss)-1);
1310 for k= 1:length(ss)-1;
1311 if mod(k, 100) == 0
1312 progress_msg(k, length(ss)-1);
1313 end
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323 if fseek(fid,ss(k)+8,-1)<0; break; end
1324
1325 tstamp(k) = fread(fid,1,'float64', 0, 'ieee-le');
1326
1327 if fseek(fid,ss(k)+16,-1)<0; break; end
1328 vd(:,k)=fread(fid,600,'double', 0, 'ieee-le');
1329
1330 start = 16+600*8;
1331 if fseek(fid,ss(k)+start,-1)<0; break; end
1332 gugus(:,k) = fread(fid, floor((5169-start)/8), 'double', 'ieee-le');
1333
1334 if fseek(fid,ss(k)+5169,-1)<0; break; end
1335
1336 rawsigs(:,k) = fread(fid, 67, 'single', 'ieee-le');
1337
1338 if fseek(fid,ss(k)+5437,-1)<0; break; end
1339 event(:, k) = fread(fid, 30, 'char', 'ieee-le');
1340
1341
1342
1343
1344 if fseek(fid,ss(k)+5491,-1)<0; break; end
1345 counter(k) = fread(fid, 1, 'uint16', 'ieee-le');
1346 end
1347
1348
1349
1350
1351 SignalNames = {'RTD_Paw_mbar_01', 'RTD_Paw_mbar_02', 'RTD_Flow_L_min_03', 'RTD_Flow_L_min_04', 'RTD_Vol__mL_05', 'RTD_Vol__mL_06', 'unknown_07', 'unknown_08', 'unknown_09', 'unknown_10', 'unknown_11', 'Tispon_sec_12', 'Pmin_mbar_13', 'P0_1_mbar_14', 'Pmean_mbar_15', 'unknown_16', 'PEEP_mbar_17', 'unknown_18', 'unknown_19', 'unknown_20', 'unknown_21', 'PIP_mbar_22', 'unknown_23', 'unknown_24', 'unknown_25', 'unknown_26', 'VT_mL_27', 'unknown_28', 'unknown_29', 'unknown_30', 'unknown_31', 'VT_mL_32', 'VTispon_mL_33', 'NIF_mbar_34', 'MVleak_L_min_35', 'unknown_36', 'RRspon_1_min_37', 'unknown_38', 'MVspon_L_min_39', 'unknown_40', 'MVspon_L_min_41', 'unknown_42', 'RSB_1_LXMin_43', 'RRspon_1_min_44', 'unknown_45', 'unknown_46', 'unknown_47', 'unknown_48', 'unknown_49', 'etCO2___50', 'etCO2___51', 'unknown_52', 'unknown_53', 'unknown_54', 'unknown_55', 'unknown_56', 'unknown_57', 'unknown_58', 'unknown_59', 'unknown_60', 'unknown_61', 'unknown_62', 'unknown_63', 'unknown_64', 'unknown_65', 'unknown_66', 'unknown_67'};
1352
1353 for iS = 1 : size(rawsigs,1)
1354
1355 SigTmp = rawsigs(iS,:);
1356
1357 SigTmp((SigTmp == -1000) | (SigTmp < -1E38)) = nan;
1358 signals.(SignalNames{iS}) = SigTmp;
1359 end
1360
1361
1362 progress_msg(inf);
1363 fclose(fid);
1364
1365 function do_unit_test
1366
1367 pth = get_contrib_data('human-ventilation-2014','human-ventilation-2014.eit');
1368 [dd,auxdata,stim]= eidors_readdata(pth);
1369 unit_test_cmp('LQ2',dd(100,100), -8.820502983940973e-04 - 8.022670735677084e-04i,1e-16);
1370 unit_test_cmp('LQ2:elecZ', auxdata.elec_impedance(3), -6.883231000000000e+06 - 1.805360000000000e+05i,1e-10);
1371 unit_test_cmp('LQ2:t_rel', auxdata.t_rel(2), 38442185);