Home > eeg > egis > egis_file_utils > wt_ave_hdr_v.m

wt_ave_hdr_v

PURPOSE ^

[status]=wt_ave_hdr_v(fid,fhdr,chdr,ename,cnames,fcom,ftext)

SYNOPSIS ^

function [status]=wt_ave_hdr_v(fid,infhdr,inchdr,ename,cnames,fcom,ftext);

DESCRIPTION ^

[status]=wt_ave_hdr_v(fid,fhdr,chdr,ename,cnames,fcom,ftext)

This function creates a new EGIS average file header in the file with id fid.
The structures which are passed to this routine are the same as
are returned from function rd_ses_hdr_v (c.f.) and which are
passed to wt_ses_hdr_v.  
This routine reformats the file header and recalculates its size.
The total number of elements written is returned.
Note that this number is NOT the header size in bytes.

NOTE:     The NObs (NTrials) and NAvg (number of trials in average) fields in 
        the Average Cell Specifics must be set prior to calling this routine
        The same if true for the LastDone field of fhdr (Average File Description)

See also:rd_ses_hdr_v.m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [status]=wt_ave_hdr_v(fid,infhdr,inchdr,ename,cnames,fcom,ftext);
0002 %[status]=wt_ave_hdr_v(fid,fhdr,chdr,ename,cnames,fcom,ftext)
0003 %
0004 %This function creates a new EGIS average file header in the file with id fid.
0005 %The structures which are passed to this routine are the same as
0006 %are returned from function rd_ses_hdr_v (c.f.) and which are
0007 %passed to wt_ses_hdr_v.
0008 %This routine reformats the file header and recalculates its size.
0009 %The total number of elements written is returned.
0010 %Note that this number is NOT the header size in bytes.
0011 %
0012 %NOTE:     The NObs (NTrials) and NAvg (number of trials in average) fields in
0013 %        the Average Cell Specifics must be set prior to calling this routine
0014 %        The same if true for the LastDone field of fhdr (Average File Description)
0015 %
0016 %See also:rd_ses_hdr_v.m
0017 
0018 %  Modification history:
0019 %    1/12/96 PJ    BytOrd value is set to 16909060 (integer*4) so that
0020 %            header modified by wt_csdm_hdr_v is reset to default
0021 %            value
0022 %
0023 %    6/6/95    PJ    Created the file based on wt_ses_hdr_v
0024 %
0025 % 04/30/97 PJ  Fixed machine architecture compatability
0026 
0027 if nargin < 7
0028   error('Too few arguments to wt_ave_hdr_v')
0029 end
0030 
0031 ave_hdr_offsets_v;
0032 fhdr = infhdr;
0033 chdr = inchdr;
0034 
0035 %
0036 % Perform necessary transformations on the header
0037 %
0038 
0039 %% Put in the proper BytOrd value -- this is changed by wt_csdm_hdr_v,
0040 %% so it has to be changed back when writing files derived from csdm files
0041 
0042     fhdr(BytOrd) = 16909060;
0043 
0044 %% Modify Average File Description
0045     fhdr(HdrVer) = -1;
0046     
0047 %% Modify Average Information
0048     fhdr(5:10) = zeros(1,6);
0049     fhdr(ScaleBins) = 500;
0050     fhdr(ScaleCal) = 50;
0051     fhdr(BaseDur) = 0;
0052     fhdr(15:17) = zeros(1,3);
0053 
0054 %% Modify Average File Data Description
0055     if (fhdr(NChan) == 64 | fhdr(NChan) == 128)
0056         fhdr(NChan) = fhdr(NChan) + 1;    % add a channel now that the average reference has been computed
0057     end
0058     
0059 %% Modify Average Cell Headers
0060 if any(chdr(:, LSpec) < 28)
0061   error(['Length of specs in cell(s) [' find(chdr(:,LSpec) < 28) '] is less than required minimum']);
0062 end
0063 
0064 if infhdr(HdrVer) == 1    % session file
0065     ses_hdr_offsets;
0066     chdr(:,RunDateMo_Ave) = ones(fhdr(NCells),1).*infhdr(RunDateMo);
0067     chdr(:,RunDateDay_Ave) = ones(fhdr(NCells),1).*infhdr(RunDateDay);
0068     chdr(:,RunDateYr_Ave) = ones(fhdr(NCells),1).*infhdr(RunDateYr);
0069     chdr(:,RunTimeHr_Ave) = ones(fhdr(NCells),1).*infhdr(RunTimeHr);
0070     chdr(:,RunTimeMin_Ave) = ones(fhdr(NCells),1).*infhdr(RunTimeMin);
0071     chdr(:,RunTimeSec_Ave) = ones(fhdr(NCells),1).*infhdr(RunTimeSec);
0072     chdr(:,SubjID_Ave) = ones(fhdr(NCells),1).*infhdr(SubjID);
0073     chdr(:,Handed_Ave) = ones(fhdr(NCells),1).*infhdr(Handed);
0074     chdr(:,Sex_Ave) = ones(fhdr(NCells),1).*infhdr(Sex);
0075     chdr(:,Age_Ave) = ones(fhdr(NCells),1).*infhdr(Age);
0076     chdr(:,ExperID_Ave) = ones(fhdr(NCells),1).*infhdr(ExperID);
0077     chdr(:,EdVer_Ave) = ones(fhdr(NCells),1).*infhdr(EdVer);
0078     chdr(:,CalFlag_Ave) = ones(fhdr(NCells),1);
0079 end
0080 
0081 % Check to make sure that cell name matrix has 80 columns
0082 if size(cnames,2) ~= 80
0083   disp('Resizing cell name array')
0084   if size(cnames,2) < 80
0085     cnames = [cnames zeros(size(cnames,1),80-size(cnames,2))];
0086   else
0087     cnames = cnames(:,1:80);
0088   end
0089 end
0090 
0091 %% Recompute header length
0092 fhdr(LComment) = length(fcom);
0093 fhdr(LText) = length(ftext);
0094 
0095 
0096 %disp('Computing length variables in header of average EGIS file...')
0097 L = ((chdr(:,LSpec).*chdr(:,NObs))'+90);
0098 fhdr(LCellHdr:(LCellHdr+(fhdr(NCells)-1))) = L';
0099 nlcellhdr = fhdr(NCells)*2;
0100 lhdr = 130+nlcellhdr+ fhdr(NChan) * 4 + sum(L)+fhdr(LComment)+fhdr(LText);
0101 fhdr(LPad) = (512-rem(lhdr,512));
0102 lhdr = lhdr+fhdr(LPad);
0103 fhdr(LHeader) = lhdr;
0104 fhdr(LData) = sum((chdr(:,NPoints).*chdr(:,NObs))*fhdr(NChan))*2;
0105 
0106 status=fseek(fid,0,'bof');
0107 status=fwrite(fid,fhdr(BytOrd),'integer*4');
0108 message=ferror(fid);
0109 if ~isempty(message),error(message),end
0110 
0111 status=fwrite(fid,fhdr(HdrVer),'integer*2');
0112 message=ferror(fid);
0113 if ~isempty(message), error(message),end
0114 
0115 status=fwrite(fid,fhdr(LHeader),'integer*2');
0116 message=ferror(fid);
0117 if ~isempty(message),error(message),end
0118 
0119 status=fwrite(fid,fhdr(LData),'integer*4');
0120 message=ferror(fid);
0121 if ~isempty(message),error(message),end
0122 
0123 status=fwrite(fid,abs(ename),'char');
0124 message=ferror(fid);
0125 if ~isempty(message),error(message),end
0126 
0127 status=fwrite(fid,zeros(6,1),'integer*2');  % empty fields -- run date and time in ses files
0128 message=ferror(fid);
0129 if ~isempty(message),error(message),end
0130 
0131 status=fwrite(fid,fhdr(LastDone:BaseDur),'integer*2');
0132 message=ferror(fid);
0133 if ~isempty(message),error(message),end
0134 
0135 status=fwrite(fid,zeros(3,1),'integer*2');
0136 message=ferror(fid);
0137 if ~isempty(message),error(message),end
0138 
0139 status=fwrite(fid,fhdr(NCells:BrdGain),'integer*2');
0140 message=ferror(fid);
0141 if ~isempty(message),error(message),end
0142 
0143 status=fwrite(fid,fhdr(LCellHdr:(LCellHdr+(fhdr(NCells)-1))),'integer*2');
0144 message=ferror(fid);
0145 if ~isempty(message),error(message),end
0146 
0147 status=fwrite(fid,zeros(fhdr(NChan)*2, 1),'integer*2');  % fill with zeros
0148 message=ferror(fid);
0149 if ~isempty(message),error(message),end
0150 
0151 for loop=1:fhdr(NCells)
0152     status=fwrite(fid,chdr(loop,CellID),'integer*2');
0153     message=ferror(fid);
0154     if ~isempty(message),error(message),end
0155     status=fwrite(fid,abs(cnames(loop,:)),'char');
0156     message=ferror(fid);
0157     if ~isempty(message),error(message),end
0158     lastrow=5+((chdr(loop,LSpec)/2)*chdr(loop,NObs));
0159     status=fwrite(fid,chdr(loop,NObs:lastrow),'integer*2');
0160     message=ferror(fid);
0161     if ~isempty(message),error(message),end
0162 end
0163 
0164 status=fwrite(fid,fcom,'char');
0165 message=ferror(fid);
0166 if ~isempty(message),error(message),end
0167 
0168 status=fwrite(fid,ftext,'char');
0169 message=ferror(fid);
0170 if ~isempty(message),error(message),end
0171 
0172 status=fwrite(fid,zeros(fhdr(LPad),1),'char');
0173 message=ferror(fid);
0174 if ~isempty(message),error(message),end
0175 
0176 status = 0;
0177 disp(['Successfully wrote the average file header. ' '(' num2str(fhdr(LHeader)) ' bytes long)']);

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003