0001 function status = make_factor(csdmfiles,outfname,win,freqlist,nfactor,bad_chan,output);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 if ~(nargin==6|nargin==7|nargin == 5)
0018 error('improper arguments')
0019 end;
0020
0021 if nargin == 5
0022 bad_chan = [];
0023 output = 'cos';
0024 elseif nargin == 6
0025 output = 'cos';
0026 end;
0027
0028
0029
0030
0031
0032 ave_hdr_offsets_v;
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 [csdm_fhdr,csdm_chdr]=get_csdm_hdr(csdmfiles(1,:));
0060
0061 if win == []
0062 win = 1:csdm_chdr(1,NObs);
0063 end
0064
0065
0066
0067
0068
0069 [ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext] = csdmhdr2avehdr(csdmfiles, win);
0070
0071
0072
0073
0074
0075 for c = 1:csdm_fhdr(NCells)
0076 ave_chdr(c,NPoints) = size(freqlist,2);
0077 end
0078
0079
0080
0081
0082
0083 if size(csdmfiles,1) > 1 & length(win) > 1
0084 nwin_files = length(win);
0085 else
0086 nwin_files = 1;
0087 end
0088
0089 nwin = length(win);
0090
0091
0092
0093
0094
0095 out_root = outfname;
0096 for i = 1:nfactor
0097 for f = 1:nwin_files
0098 if nwin_files > 1 & win ~= []
0099 outfname1 = [out_root '_mag_' int2str(win(f)) '_ft_' int2str(i)];
0100 outfname2 = [outfname '_' output '_' int2str(win(f)) '_ft_' int2str(i)];
0101 else
0102 outfname1 = [out_root '_mag_ft_' int2str(i)];
0103 outfname2 = [out_root '_' output '_ft_' int2str(i)];
0104 end
0105
0106 ave_fid = fopen(outfname1,'wb');
0107 ave_fid2 = fopen(outfname2,'wb');
0108 wt_ave_hdr_v(ave_fid,ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0109 wt_ave_hdr_v(ave_fid2,ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0110
0111 fclose(ave_fid);
0112 fclose(ave_fid2);
0113 end
0114 end;
0115
0116 ch_pair_indices;
0117
0118 [cell_data_offsets]=get_csdm_cell_off(csdm_fhdr, csdm_chdr);
0119
0120 file_mask = ones(size(csdmfiles,1),ave_fhdr(NChan));
0121 if (nargin == 7|nargin == 8)
0122 for i=1:size(bad_chan,1)
0123 bchan = find(bad_chan(i,:));
0124 if bchan ~= []
0125 file_mask(i,bad_chan(bchan)) = zeros(1,size(bchan,2));
0126 end;
0127 end;
0128 end;
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 ch_pair_indices;
0144 outfname3 = [outfname '_eig'];
0145 eig_fid = fopen(outfname3,'wb');
0146 fwrite(eig_fid,[csdm_fhdr(NCells) size(csdmfiles,1) ave_chdr(1,NPoints) nwin],'int16');
0147 for icell = 1:csdm_fhdr(NCells)
0148 for ifile = 1:size(csdmfiles,1)
0149 for f = 1:nwin
0150 [csdm_data, csdm_fhdr, csdm_chdr] = read_csdm(csdmfiles(ifile,:), icell, win(f));
0151
0152 csdm_mat = zeros(ave_fhdr(NChan));
0153 for jfreq = 1:size(freqlist,2)
0154 ifreq= freqlist(jfreq);
0155 for ichan =1:ave_fhdr(NChan)
0156 for jchan = ichan:ave_fhdr(NChan);
0157 csdm_mat(ichan,jchan) = csdm_data(ifreq,chpair(ichan,jchan));
0158 csdm_mat(jchan,ichan) = conj(csdm_mat(ichan,jchan));
0159 end;
0160 end;
0161 good_chan = find(file_mask(ifile,:));
0162 csd = zeros(size(good_chan,2));
0163 csd = csdm_mat(good_chan,good_chan);
0164
0165 [Vec_csd, val_csd] = eig(csd,'nobalance');
0166
0167 if ~isreal(val_csd)
0168
0169
0170
0171 val_csd = real(val_csd);
0172 end;
0173 vec_csd = vec_csd';
0174 abs_vec_csd = 500*abs(vec_csd);
0175 fwrite(eig_fid,[icell ifreq size(val_csd,1) f],'int16');
0176
0177
0178
0179 fwrite(eig_fid,diag(val_csd),'float');
0180
0181 phase_data = vec_csd;
0182 if output == 'cos'
0183 phase_data = 500*real(phase_data)./abs(phase_data);
0184 else
0185 real_sign = sign(real(phase_data));
0186 phase_data = angle(phase_data);
0187 ang_sign = sign(phase_data);
0188 for iph = 1:size(phase_data,1)
0189 for jph = size(phase_data,2)
0190 if real_sign(iph,jph) < 0
0191 phase_data(iph,jph) = phase_data(iph,jph) + pi*real_sign(iph,jph)*ang_sign(iph,jph);
0192 end;
0193 end;
0194 end;
0195
0196
0197 phase_data = 500*phase_data;
0198 end
0199 phase_data(find(isnan(phase_data))) = zeros(size(find(isnan(phase_data))));
0200
0201 if ~isreal(phase_data)
0202 error('Phase matrix is not real')
0203 end
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 for ifac = 1:nfactor
0214 mag = zeros(1, ave_fhdr(NChan));
0215 ph = zeros(1,ave_fhdr(NChan));
0216 mag(1,good_chan) = abs_vec_csd(ifac,:);
0217 ph(1,good_chan) = phase_data(ifac,:);
0218
0219
0220 if nwin_files > 1 & win ~= []
0221 outfname1 = [out_root '_mag_' int2str(win(f)) '_ft_' int2str(ifac)];
0222 outfname2 = [outfname '_' output '_' int2str(win(f)) '_ft_' int2str(ifac)];
0223 else
0224 outfname1 = [out_root '_mag_ft_' int2str(ifac)];
0225 outfname2 = [out_root '_' output '_ft_' int2str(ifac)];
0226 end
0227
0228 avefid1 = fopen(outfname1,'ab');
0229 avefid2= fopen(outfname2, 'ab');
0230
0231 num_written = fwrite(avefid1, mag','int16');
0232 if num_written~= size(mag,2)
0233 error('write out croaked')
0234 end;
0235 num_written = fwrite(avefid2, ph','int16');
0236 if num_written~= size(ph,2)
0237 error('write out croaked')
0238 end;
0239 fclose(avefid1);
0240 fclose(avefid2);
0241 end
0242 end;
0243 end;
0244 end;
0245 end;
0246 fclose('all');
0247
0248
0249