0001 function [coh_mat] = make_coherence(csdmfiles,outfname,win, cf_list, cf_flag, bad_chans);
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 status = -1;
0031 format compact
0032
0033
0034
0035
0036
0037 if (nargin<5)
0038 error('Too few arguments to make_coherence()')
0039 elseif nargin<6
0040 bad_chans = [];
0041 else
0042 if size(bad_chans,1) ~= size(csdmfiles,1)
0043 error('csdmfile list and bad_chans lists don''t match');
0044 end
0045 end;
0046
0047 if ~(strcmp(cf_flag, 'freq') | strcmp(cf_flag, 'chan'))
0048 error(['Invalid cf_flag: ' cf_flag]);
0049 end
0050
0051 ave_hdr_offsets_v;
0052
0053
0054
0055
0056
0057 [csdm_fhdr,csdm_chdr]=get_csdm_hdr(csdmfiles(1,:));
0058
0059 if win == []
0060 win = 1:csdm_chdr(1,NObs);
0061 end
0062
0063
0064
0065
0066
0067
0068 [ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext] = csdmhdr2avehdr(csdmfiles, win);
0069
0070
0071
0072
0073
0074 for c = 1:csdm_fhdr(NCells)
0075 if strcmp(cf_flag, 'chan')
0076 ave_chdr(c,NPoints) = csdm_chdr(c,NPoints)/2;
0077 else
0078 ave_chdr(c,NPoints) = ave_fhdr(NChan);
0079 end
0080 end
0081
0082
0083
0084
0085
0086 if size(csdmfiles,1) > 1 & length(win) > 1
0087 nwin_files = length(win);
0088 else
0089 nwin_files = 1;
0090 end
0091
0092 nitems = size(cf_list,2);
0093 nwin = length(win);
0094
0095
0096
0097
0098
0099
0100 new_root = outfname;
0101 if strcmp(cf_flag, 'freq')
0102 new_root = [new_root '_cf'];
0103 else
0104 new_root = [new_root '_cc'];
0105 end
0106
0107 fid_mat = zeros(nitems,nwin_files);
0108
0109 for iref = 1:nitems
0110 iref_str = int2str(cf_list(iref));
0111 outfname1 = [new_root iref_str];
0112 if nwin_files == 1
0113 fid_mat(iref, 1) = fopen(outfname1,'wb');
0114 wt_ave_hdr_v(fid_mat(iref,1),ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0115 fseek(fid_mat(iref, 1), 0, 'eof');
0116 else
0117 for fref = 1:nwin_files
0118 text_ref = int2str(fref);
0119 outfname1 = [new_root iref_str '_w' text_ref];
0120 fid_mat(iref, fref) = fopen(outfname1,'wb');
0121 wt_ave_hdr_v(fid_mat(iref,fref),ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0122
0123 fclose(fid_mat(iref,fref));
0124 end
0125 end
0126 end;
0127 ch_pair_indices;
0128
0129 ch_str = [];
0130 for c = 1:length(cf_list)
0131 ch_str = [ch_str int2str(cf_list(c)) ' '];
0132 end
0133 if strcmp(cf_flag, 'freq')
0134 disp(['Computing all channel coherences for frequency bins ' ch_str]);
0135 else
0136 disp(['Computing coherences for all frequency bins with respect to chans: ' ch_str]);
0137 end
0138
0139 [cell_data_offsets]=get_csdm_cell_off(csdm_fhdr, csdm_chdr);
0140
0141 for icell = 1:csdm_fhdr(NCells)
0142 disp(['Working on cell ' int2str(icell)]);
0143 npts = ave_chdr(icell,NPoints);
0144 nchan = ave_fhdr(NChan);
0145
0146 for ifile = 1:size(csdmfiles,1)
0147 disp([' Input file: ' csdmfiles(ifile,:)]);
0148 for iobs = 1:nwin
0149 disp([' Observation ' int2str(iobs) ' (' int2str(win(iobs)) ')']);
0150
0151
0152 [csdm_data, csdm_fhdr, csdm_chdr] = read_csdm(csdmfiles(ifile,:), icell, win(iobs));
0153
0154
0155 for iref = 1:nitems
0156 if strcmp(cf_flag, 'freq')
0157 disp([' Frequency band ' int2str(cf_list(iref))]);
0158 else
0159 disp([' Channel ' int2str(cf_list(iref))]);
0160 end
0161
0162
0163 if strcmp(cf_flag, 'freq')
0164 power_data = zeros(1,nchan);
0165 else
0166 power_data = zeros(npts, nchan);
0167 end
0168
0169 coh_data = zeros(npts,nchan);
0170 numerator = zeros(npts, nchan);
0171
0172
0173 if strcmp(cf_flag, 'freq')
0174 for ichan = 1:nchan
0175 power_data(ichan) = csdm_data(cf_list(iref),chpair(ichan,ichan));
0176 end;
0177
0178
0179 if bad_chans ~= []
0180 power_data(bad_chans(ifile,find(bad_chans(ifile,:)))) = ones(size(find(bad_chans(ifile,:)))) * NaN;
0181 end
0182 power_data = power_data' * power_data;
0183 else
0184 for ichan = 1:nchan
0185 power_data(:,ichan) = csdm_data(1:npts,chpair(ichan,ichan));
0186 end
0187 if bad_chans ~= []
0188 power_data(:,bad_chans(ifile,find(bad_chans(ifile,:)))) = ones(size(power_data(:,bad_chans(ifile,find(bad_chans(ifile,:)))))) * NaN;
0189 end;
0190 end
0191
0192
0193 testreal = isreal(sum(power_data));
0194 if testreal ~= 1
0195 power_data(1,[6 54 96])
0196 error('complex numbers in power data matrix')
0197 end;
0198
0199
0200
0201
0202
0203
0204 if strcmp(cf_flag, 'freq')
0205
0206 if strcmp(computer, 'MAC2')
0207 for rchan = 1:nchan
0208 numerator(rchan,:) = ppc_cmplx_mult(csdm_data(cf_list(iref),chpair(rchan, 1:nchan)),conj(csdm_data(cf_list(iref),chpair(rchan, 1:nchan))));
0209 end
0210 else
0211 for rchan = 1:nchan
0212 numerator(rchan,:) = csdm_data(cf_list(iref),chpair(rchan, 1:nchan)).*conj(csdm_data(cf_list(iref),chpair(rchan, 1:nchan)));
0213 end
0214 end
0215
0216
0217 coh_data = numerator./power_data;
0218
0219 else
0220
0221 if strcmp(computer, 'MAC2')
0222 for ichan = 1:nchan
0223 numerator(:,ichan) = ppc_cmplx_mult(csdm_data(1:npts,chpair(cf_list(iref), ichan)),conj(csdm_data(1:npts,chpair(cf_list(iref), ichan))));
0224 end
0225 else
0226 for ichan = 1:nchan
0227 numerator(:,ichan) = csdm_data(1:npts,chpair(cf_list(iref), ichan)).*conj(csdm_data(1:npts,chpair(cf_list(iref), ichan)));
0228 end
0229 end
0230
0231
0232 coh_data = numerator./((power_data(1:npts,cf_list(iref))*ones(1,nchan)) .* power_data);
0233 end
0234
0235
0236
0237
0238
0239
0240 coh_data(find(isnan(coh_data))) = zeros(size(find(isnan(coh_data))));
0241
0242
0243
0244
0245 if strcmp(cf_flag, 'freq')
0246 testcoh = sum(diag(coh_data));
0247 if testcoh ~= (nchan - length(find(bad_chans(ifile,:))))
0248 if ~isempty(findstr(csdmfiles(ifile,:), '2D'))
0249 disp(['You are a moron who failed to detect bad chans' int2str(testcoh)])
0250 else
0251 disp(['Undetected Bad Chan ' int2str(testcoh)]);
0252 end
0253 end
0254 else
0255 testcoh = sum(coh_data(1:npts,cf_list(iref)));
0256 if testcoh ~= npts
0257 disp(['Your character is suspect ' int2str(testcoh)]);
0258 end
0259 end
0260
0261 if ~isreal(coh_data)
0262 error('Coherence matrix is not real')
0263 end
0264
0265 coh_mat = coh_data;
0266
0267
0268
0269
0270
0271 coh_data = coh_data * 500;
0272
0273 iref_str = int2str(cf_list(iref));
0274 if nwin_files == 1
0275 outfname1 = [new_root iref_str];
0276 fid_mat = fopen(outfname1,'ab');
0277 fseek(fid_mat, 0, 'eof');
0278 num_written = fwrite(fid_mat,coh_data','int16');
0279 fclose(fid_mat);
0280 else
0281 for fref = 1:nwin_files
0282 text_ref = int2str(fref);
0283 outfname1 = [new_root iref_str '_w' text_ref];
0284 fid_mat = fopen(outfname1,'ab');
0285 fseek(fid_mat, 0, 'eof');
0286 num_written = fwrite(fid_mat,coh_data','int16');
0287 if num_written~= size(coh_data,1)*size(coh_data,2)
0288 error('write failed')
0289 end;
0290
0291 fclose(fid_mat);
0292 end
0293 end
0294
0295
0296
0297
0298
0299
0300 if num_written~= size(coh_data,1)*size(coh_data,2)
0301 error('write failed')
0302 end;
0303 end;
0304 end
0305 end;
0306 end;
0307 fclose('all');
0308
0309 format loose
0310 status = 0;
0311 return
0312