0001 function [status]=wt_ave_hdr_v(fid,infhdr,inchdr,ename,cnames,fcom,ftext);
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 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
0037
0038
0039
0040
0041
0042 fhdr(BytOrd) = 16909060;
0043
0044
0045 fhdr(HdrVer) = -1;
0046
0047
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
0055 if (fhdr(NChan) == 64 | fhdr(NChan) == 128)
0056 fhdr(NChan) = fhdr(NChan) + 1;
0057 end
0058
0059
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
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
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
0092 fhdr(LComment) = length(fcom);
0093 fhdr(LText) = length(ftext);
0094
0095
0096
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');
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');
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)']);