0001 function cal_erps=import_cal_erps(Results,cal_crwfile,cal_logfile,cal_blfORbdf_file,rm_chans,usedcal_itemnum,scale_factor)
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 global VERBLEVEL
0066
0067 VerbReport('Creating cal pulse ERPs with minimal memory demands.', 3, VERBLEVEL);
0068
0069
0070
0071
0072 fid_crw=erpio('openraw',cal_crwfile,cal_logfile,Results.presam,Results.cprecis,Results.decimat);
0073 if (erpio('get_errno') == 0),
0074 VerbReport(sprintf('Opening cal pulse crw file %s went OK', cal_crwfile), 2, VERBLEVEL);
0075 else
0076 error(erpio('get_errstr'));
0077 end;
0078
0079
0080 VerbReport('Getting cal pulse crw file header information.', 3, VERBLEVEL);
0081
0082
0083 expdesc = erpio('get_hdrvar', fid_crw, 'expdesc', 0);
0084 subdesc = erpio('get_hdrvar', fid_crw, 'subdesc', 0);
0085 VerbReport(sprintf('Cal crw File Exp Desc: %s',expdesc), 3, VERBLEVEL);
0086 VerbReport(sprintf('Cal crw File Sub Desc: %s',subdesc), 3, VERBLEVEL);
0087
0088
0089 nchans = erpio('get_hdrvar', fid_crw, 'chans',0);
0090 crw_chanlabels=cell(1,nchans);
0091 for a=1:nchans,
0092 crw_chanlabels{a}=erpio('get_hdrvar',fid_crw,'chndesc',a-1);
0093
0094 end
0095 tpts = 256*Results.cprecis;
0096 srate=100000/erpio('get_hdrvar',fid_crw,'clktick','junk');
0097
0098
0099 use_chans=1:nchans;
0100 use_crw_chanlabels=cell(1,1);
0101 use_chan_ct=0;
0102 if ~isempty(rm_chans),
0103 for a=1:nchans,
0104 for b=1:length(rm_chans),
0105 if strcmpi(crw_chanlabels{a},rm_chans{b}),
0106 use_chans(a)=-1;
0107 break;
0108 end
0109 end
0110 if use_chans(a)>0
0111 use_chan_ct=use_chan_ct+1;
0112 use_crw_chanlabels{use_chan_ct}=crw_chanlabels{a};
0113 end
0114 end
0115 use_chans=use_chans(find(use_chans>0));
0116 else
0117 use_crw_chanlabels=crw_chanlabels;
0118 end
0119 nchans=length(use_chans);
0120
0121
0122
0123
0124
0125 orig_level=VERBLEVEL;
0126 VERBLEVEL=1;
0127 [blf_info, bin_LongDesc, blffile, blf_evnum]=import_blf(cal_blfORbdf_file,cal_logfile, ...
0128 srate,[]);
0129 VERBLEVEL=orig_level;
0130
0131
0132 clear blf_info bin_LongDesc
0133
0134
0135
0136
0137
0138
0139
0140
0141 VerbReport(' ',1,VERBLEVEL);
0142
0143 VerbReport(sprintf('Creating temporary ascii version of logfile %s for calibration pulses',cal_logfile),1,VERBLEVEL);
0144
0145 asci_cmnd='rm -f temp_cal.asci';
0146 unix(asci_cmnd);
0147 asci_cmnd=sprintf('log2asci %s temp_cal.asci -d %d',cal_logfile,srate);
0148 [s, w]=unix(asci_cmnd);
0149 if s~=0,
0150 disp('Could not create ascii version of logfile using log2asci.');
0151 error('According to log2asci: %s',w);
0152 end
0153 asciilogfile='temp_cal.asci';
0154 VerbReport(sprintf('Getting log information from %s', asciilogfile), 1, VERBLEVEL);
0155
0156
0157
0158 enum = []; ecode = []; ccode = []; flag =[]; tick = []; sec =[]; deltaT = [];
0159
0160 fid_ascii=fopen(asciilogfile,'r');
0161 lyne=fgetl(fid_ascii);
0162 for dumb_loop=1:13,
0163 lyne=fgetl(fid_ascii);
0164 end
0165 if strcmpi(lyne(length(lyne)-4:end),'Delta')
0166 fclose(fid_ascii);
0167 [enum, ecode, ccode, flag, tick, sec, deltaT]=textread(asciilogfile,['%d' ...
0168 ' %d %d %d %d %f %f'],'headerlines',14);
0169 else
0170 err_str=sprintf('ERROR: ASCII version of logfile %s was not generated with sampling rate.\n', ...
0171 asciilogfile);
0172 error([err_str 'Please correct.']);
0173 end
0174 log_file_info = [enum, ecode, ccode, flag, sec];
0175
0176
0177
0178 log_info=zeros(size(blf_evnum,1),5);
0179 ct=0;
0180 for blf_ev=blf_evnum,
0181 ct=ct+1;
0182 log_info(ct,:) = log_file_info(find(enum==blf_ev),:);
0183 end
0184
0185
0186 if ~isempty(asci_cmnd)
0187 VerbReport(sprintf('Removing temporary ascii version of logfile.'),1,VERBLEVEL);
0188 asci_cmnd='rm -f temp_cal.asci';
0189 [s, w]=unix(asci_cmnd);
0190 if s~=0,
0191 disp('Could not remove ascii version of logfile: temp_cal.asci.');
0192 error('According to rm: %s',w);
0193 end
0194 end
0195
0196
0197
0198
0199
0200 VerbReport(sprintf('\nGetting epochs from %s', cal_crwfile), 1, VERBLEVEL);
0201
0202
0203 nep = size(log_info,1);
0204
0205
0206
0207 nom_cal_size = sum(log_info(:,3)==0);
0208
0209
0210 VerbReport('Assuming Condition Code 0=calibration pulses.',1,VERBLEVEL);
0211 VerbReport(sprintf('%d total calibration pulses from cal crw file',nom_cal_size), 1, VERBLEVEL);
0212
0213
0214
0215 runningsum=zeros(nchans,tpts);
0216
0217
0218
0219
0220
0221
0222
0223 tme=1000*((1:256*Results.cprecis)-1)/srate;
0224 tme=tme-Results.presam;
0225 [dummy, low_cntr_samp]=min(abs(Results.low_cursor-tme));
0226 [dummy, hi_cntr_samp]=min(abs(Results.hi_cursor-tme));
0227
0228
0229 lo_start_samp = low_cntr_samp-floor(Results.cal_npts/2);
0230 lo_stop_samp = low_cntr_samp+floor(Results.cal_npts/2);
0231 prewind=[tme(lo_start_samp) tme(lo_stop_samp)];
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 calcnt=zeros(1,length(use_chans));
0244 for i=1:nep,
0245
0246
0247 currdat = zeros(nchans,tpts);
0248 currdat = erpio('evread', fid_crw, log_info(i, 1));
0249
0250
0251 if (log_info(i,3)==0),
0252 if isempty(currdat)
0253
0254 msg=sprintf(['erpio: Can not read data at log item %d.\n' ...
0255 'Log item should be a calibration pulse.'],log_info(i,1));
0256 watchit(msg);
0257 else
0258
0259 currdat=rmbase(currdat,tpts,lo_start_samp:lo_stop_samp);
0260
0261 chn_ct=0;
0262 for chnloop=use_chans,
0263 chn_ct=chn_ct+1;
0264
0265 if ismember(log_info(i,1),usedcal_itemnum(chn_ct,:)),
0266 calcnt(chn_ct)=calcnt(chn_ct)+1;
0267
0268
0269
0270
0271 runningsum(chn_ct,:)=runningsum(chn_ct,:)+currdat(chnloop,:);
0272 VerbReport(sprintf('Loading cal %d/%d event (blf event %d/%d) for channel %s.', ...
0273 calcnt(chn_ct),nom_cal_size,i,nep,use_crw_chanlabels{chn_ct}),3,VERBLEVEL);
0274
0275 else
0276 VerbReport(sprintf('The cal pulse corresponding to blf event %d/%d has been trimmed for channel %s.', ...
0277 i,nep,use_crw_chanlabels{chn_ct}),3,VERBLEVEL);
0278 end
0279 end
0280 end
0281 elseif (log_info(i,3)<0)
0282 error('Unexplained negative condition code at log item %d.', i);
0283 end
0284 end
0285 erpio('close', fid_crw)
0286
0287
0288
0289
0290 n_orig_pulses=size(usedcal_itemnum,2);
0291 ids=find(calcnt~=n_orig_pulses);
0292 if ~isempty(ids),
0293 err_chan=[];
0294 for id_loop=ids,
0295 err_chan=[err_chan use_crw_chanlabels(id_loop)];
0296 end
0297 error('Channels %s do not have the same number of used calibration pulses as were used by trm_raw2nrm.m',err_chan);
0298 end
0299
0300
0301 cal_erps=runningsum./repmat(calcnt',1,tpts);
0302 cal_erps=cal_erps./repmat(scale_factor,1,tpts);
0303
0304 VerbReport('', 1, VERBLEVEL);
0305 VerbReport(sprintf('Done creating cal pulse ERP from %s\n',cal_crwfile), 3, VERBLEVEL);
0306
0307 if strcmpi(blffile,'temp.blf'),
0308 VerbReport('Removing temporary files: temp.rt, temp.blf', 2, VERBLEVEL);
0309 unix('rm -f temp.blf');
0310 unix('rm -f temp.rt');
0311 end
0312 VerbReport('', 1, VERBLEVEL);
0313