datafile_utility

PURPOSE ^

DATAFILE_UTILITY(INPUTFILE, OUTPUTFILE, OPT)

SYNOPSIS ^

function datafile_utility(inputFile, outputFile, opt)

DESCRIPTION ^

 DATAFILE_UTILITY(INPUTFILE, OUTPUTFILE, OPT)
 utility to operate on EIT data files

 INPUTFILE: Name of input file
 OUTPUTFILE: Name of output file
 opt.action:
     LQ4_splitEITfile
     opt.range = [startFrame, endFrame]
     opt.transform = fcn_to_call on each frame
          if opt.transform is numeric, multiply by it.
     opt.addition  = add opt.addition(:,i) to frame i

 To replace EIT files with other content, do
opt.range  = [1,2];
opt.transform  = sparse(2048,2048);
opt.addition = zeros(2048,2);
opt.addition(1:2:2048,:) = [vh.meas,vi.meas]*1e6;

 In order to "shrink" EIT files, do
 PAT = [0,5];
 skip5 = {32,1,PAT,PAT,{'meas_current'},1};
 [tst.stimulation,~] = mk_stim_patterns(skip5{:});
 idx = reciprocity_idx( tst );
 mvr =  idx == max([idx,idx(idx)],[],2);
 mv2 = idx; mv2(~mvr) = [];
 mvr = find(mvr);
 mult = sparse(mvr,mvr,1,1024,1024) +  ...
        sparse(mvr,mv2,1,1024,1024);
 skip5{4} = 'no_meas_current_next2';
 [~,msel] = mk_stim_patterns(skip5{:});
 mult(~msel,:) = 0;
 
 opt.transform  = sparse(2048,2048);
 opt.transform(1:2:2048,1:2:2048)  = real(mult);
 %opt.transform(2:2:2048,2:2:2048)  = imaginary
 datafile_utility(infile,outfile,opt);

 (C) 2018 Andy Adler and Beat Mueller
 License: GPL version 2 or version 3
 $Id: datafile_utility.m 6048 2019-12-31 20:21:18Z aadler $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function datafile_utility(inputFile, outputFile, opt)
0002 % DATAFILE_UTILITY(INPUTFILE, OUTPUTFILE, OPT)
0003 % utility to operate on EIT data files
0004 %
0005 % INPUTFILE: Name of input file
0006 % OUTPUTFILE: Name of output file
0007 % opt.action:
0008 %     LQ4_splitEITfile
0009 %     opt.range = [startFrame, endFrame]
0010 %     opt.transform = fcn_to_call on each frame
0011 %          if opt.transform is numeric, multiply by it.
0012 %     opt.addition  = add opt.addition(:,i) to frame i
0013 %
0014 % To replace EIT files with other content, do
0015 %opt.range  = [1,2];
0016 %opt.transform  = sparse(2048,2048);
0017 %opt.addition = zeros(2048,2);
0018 %opt.addition(1:2:2048,:) = [vh.meas,vi.meas]*1e6;
0019 %
0020 % In order to "shrink" EIT files, do
0021 % PAT = [0,5];
0022 % skip5 = {32,1,PAT,PAT,{'meas_current'},1};
0023 % [tst.stimulation,~] = mk_stim_patterns(skip5{:});
0024 % idx = reciprocity_idx( tst );
0025 % mvr =  idx == max([idx,idx(idx)],[],2);
0026 % mv2 = idx; mv2(~mvr) = [];
0027 % mvr = find(mvr);
0028 % mult = sparse(mvr,mvr,1,1024,1024) +  ...
0029 %        sparse(mvr,mv2,1,1024,1024);
0030 % skip5{4} = 'no_meas_current_next2';
0031 % [~,msel] = mk_stim_patterns(skip5{:});
0032 % mult(~msel,:) = 0;
0033 %
0034 % opt.transform  = sparse(2048,2048);
0035 % opt.transform(1:2:2048,1:2:2048)  = real(mult);
0036 % %opt.transform(2:2:2048,2:2:2048)  = imaginary
0037 % datafile_utility(infile,outfile,opt);
0038 %
0039 % (C) 2018 Andy Adler and Beat Mueller
0040 % License: GPL version 2 or version 3
0041 % $Id: datafile_utility.m 6048 2019-12-31 20:21:18Z aadler $
0042 
0043 
0044 switch opt.action
0045    case 'LQ4_splitEITfile';
0046       if ~isfield(opt,'transform'); 
0047          opt.transform = 1; %
0048       end
0049       if ~isfield(opt,'addition'); 
0050          opt.addition = 0; %
0051       end
0052       splitEITfile(inputFile, outputFile, opt.range, ...
0053                    opt.transform, opt.addition);
0054    otherwise;
0055       error('opt.action = "%s" not recognized', opt.action);
0056 end
0057 
0058 function splitEITfile(filename, newFilename, range, fcall, addvec)
0059 % split EIT file
0060 %
0061 % filename:     name of file
0062 % newFilename:  store file here
0063 % range:        [startFrame endFrame]
0064 %
0065 % header is same as filename
0066 % if endFrame is bigger than number of frames, only available frames are
0067 % copied
0068 % only consider fileformat 4
0069 
0070     fid = fopen(filename, 'r', 'ieee-le', 'UTF-8');
0071     
0072     format_version = fread(fid, 1, 'int32', 'ieee-le');
0073     
0074     if format_version ~= 4
0075         error('wrong file version')
0076     end
0077     
0078     header_size = fread(fid, 1, 'uint32', 'ieee-le');
0079     
0080     tsStart = fread(fid, 1, 'uint64', 'ieee-le');
0081     tsEnd = fread(fid, 1, 'uint64', 'ieee-le');
0082     nFrames = fread(fid, 1, 'uint32', 'ieee-le');
0083     
0084     % check range:
0085     if length(range) ~= 2
0086         error('range has wrong size');
0087     end
0088     
0089     if range(1) > nFrames || range(1) > range(2)
0090         error('range not well defined');
0091     end
0092     
0093     % check header_size
0094     if header_size < 2664
0095         disp('assume header size of 2664');
0096         header_size = 2664;
0097     end
0098     
0099     % create new file and copy input
0100     fNewId = fopen(newFilename, 'w', 'ieee-le', 'UTF-8');
0101     fseek(fid, 0, 'bof');
0102     
0103     tmp = fread(fid, header_size, 'int8', 'ieee-le');
0104     fwrite(fNewId, tmp, 'int8', 'ieee-le');
0105     
0106      i = 0;
0107      storeFrames = 0;
0108      copiedFrames = 0;
0109      while fseek(fid, 1,'cof') ~= -1
0110         fseek(fid, -1,'cof');
0111         % need to have payload
0112         tStampsAbs = fread(fid,1,'int64','ieee-le'); 
0113         ft = fread(fid,1,'int32','ieee-le'); 
0114         pl = fread(fid,1,'int32','ieee-le');
0115         
0116         if ft == 0
0117             i = i + 1;
0118             
0119             if i >= range(1) && i <= range(2)
0120                 storeFrames = 1;
0121                 copiedFrames = copiedFrames + 1;
0122             else
0123                 storeFrames = 0;
0124             end
0125         end
0126         
0127         if storeFrames == 1
0128             % put it into new Files
0129             fwrite(fNewId, tStampsAbs, 'int64','ieee-le'); 
0130             fwrite(fNewId, ft, 'int32', 'ieee-le');
0131             fwrite(fNewId, pl, 'int32', 'ieee-le');
0132             if pl > 0
0133                 vi_payload = 64;
0134                 iq_payload = 2048;
0135                 p_startframe = ftell(fid);
0136                 payLoad = fread(fid, pl, 'int8', 'ieee-le');
0137                 p_endframe   = ftell(fid);
0138 
0139                 fseek(fid,p_startframe + 15*4 + 12 + 4*vi_payload,'bof');
0140                 iqPayload = fread(fid,iq_payload,'int32','ieee-le');
0141                 fseek(fid,p_endframe,'bof');
0142 
0143                 p_startframe = ftell(fNewId);
0144                 fwrite(fNewId, payLoad, 'int8', 'ieee-le');
0145                 p_endframe   = ftell(fNewId);
0146 
0147                 fseek(fNewId,p_startframe + 15*4 + 12 + 4*vi_payload,'bof');
0148                 if isnumeric(fcall)
0149                    iqPayload = full(fcall*iqPayload); % transform it
0150                 else
0151                    iqPayload = fcall(iqPayload); % transform it
0152                 end
0153                 if size(addvec,2) == 1
0154                    iqPayload = iqPayload + addvec;
0155                 else
0156                    iqPayload = iqPayload + addvec(:,i);
0157                 end
0158                 fwrite(fNewId, iqPayload, 'int32', 'ieee-le');
0159                 fseek(fNewId,p_endframe,'bof');
0160             end
0161         elseif pl > 0
0162             % go to the next frame
0163             fseek(fid,pl,'cof'); 
0164         elseif pl < 0
0165             disp('payloadsize < 0. try to continue');
0166         end
0167     end      
0168     % readjust number of Frame
0169     fseek(fNewId, 4 + 4 + 8 + 8, 'bof');
0170     fwrite(fNewId, copiedFrames, 'uint32', 'ieee-le');
0171 
0172 
0173     fclose(fid);
0174     fclose(fNewId);
0175

Generated on Sun 29-Dec-2024 11:41:59 by m2html © 2005