0001 function status = make_phase(csdmfiles,outfname,win,rel_chan,output);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 if ~(nargin==5|nargin==4)
0018 error('improper arguments')
0019 end;
0020
0021 if nargin == 4
0022 output ='cos';
0023 end
0024
0025
0026
0027 ave_hdr_offsets_v;
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
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 [csdm_fhdr,csdm_chdr]=get_csdm_hdr(csdmfiles(1,:));
0078
0079 if win == []
0080 win = 1:csdm_chdr(1,NObs);
0081 end
0082
0083
0084
0085
0086
0087 [ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext] = csdmhdr2avehdr(csdmfiles, win);
0088
0089
0090
0091
0092
0093 for c = 1:csdm_fhdr(NCells)
0094 ave_chdr(c,NPoints) = csdm_chdr(c,NPoints)/2;
0095 end
0096
0097
0098
0099
0100
0101 if size(csdmfiles,1) > 1 & length(win) > 1
0102 nwin_files = length(win)*size(rel_chan,2);
0103 else
0104 nwin_files = size(rel_chan,2);
0105 end;
0106
0107 nwin = length(win);
0108
0109 out_root = [outfname '_' output];
0110 if size(csdmfiles,1) > 1 & length(win) > 1
0111 for f = 1:size(win,2)
0112 for r=1:size(rel_chan,2)
0113 outfname1 = [out_root '_w_' int2str(win(f)) '_r_' int2str(rel_chan(r))];
0114 ave_fid = fopen(outfname1,'wb');
0115 wt_ave_hdr_v(ave_fid,ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0116 fclose(ave_fid);
0117
0118 end;
0119 end;
0120
0121 else
0122 for f = 1:size(rel_chan,2)
0123 outfname1 = [out_root '_r_' int2str(rel_chan(f))];
0124 ave_fid = fopen(outfname1,'wb');
0125 wt_ave_hdr_v(ave_fid,ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0126 fclose(ave_fid);
0127 end;
0128
0129
0130 end
0131
0132
0133
0134
0135
0136
0137 ch_pair_indices;
0138 for icell = 1:csdm_fhdr(NCells)
0139 for ifile = 1:size(csdmfiles,1)
0140 for iwin = 1:nwin
0141 [csdm_data, csdm_fhdr, csdm_chdr] = read_csdm(csdmfiles(ifile,:), icell, win(iwin));
0142 for irel = 1:size(rel_chan,2)
0143 phase_data = zeros(ave_chdr(icell,NPoints),ave_fhdr(NChan));
0144 for ichan = 1:ave_fhdr(NChan)
0145 phase_data(:,ichan) = csdm_data(1:ave_chdr(icell,NPoints),chpair(ichan,rel_chan(irel)));
0146 if ichan > rel_chan(irel)
0147 phase_data(:,ichan) = conj(phase_data(:,ichan));
0148 end
0149 end
0150
0151 if output == 'cos'
0152 phase_data = 500*real(phase_data)./abs(phase_data);
0153 else
0154 real_sign = sign(real(phase_data));
0155 phase_data = angle(phase_data);
0156 ang_sign = sign(phase_data);
0157 for iph = 1:size(phase_data,1)
0158 for jph = size(phase_data,2)
0159 if real_sign(iph,jph) < 0
0160 phase_data(iph,jph) = phase_data(iph,jph) + pi*real_sign(iph,jph)*ang_sign(iph,jph);
0161 end;
0162 end;
0163 end;
0164
0165
0166 phase_data = 500*phase_data;
0167 end
0168 phase_data(find(isnan(phase_data))) = zeros(size(find(isnan(phase_data))));
0169 if size(csdmfiles,1) > 1 & length(win) > 1
0170 f = iwin;
0171 r = irel;
0172 outfname1 = [out_root '_w_' int2str(win(f)) '_r_' int2str(rel_chan(r))];
0173 fid1 = fopen(outfname1,'ab');
0174 num_written = fwrite(fid1,phase_data','int16');
0175 fclose(fid1);
0176 if num_written~= size(phase_data,1)*size(phase_data,2)
0177 error('write failed')
0178 end;
0179
0180 else
0181
0182 f = irel;
0183 outfname1 = [out_root '_r_' int2str(rel_chan(f))];
0184 fid1 = fopen(outfname1,'ab');
0185 num_written = fwrite(fid1,phase_data','int16');
0186 fclose(fid1);
0187 if num_written~= size(phase_data,1)*size(phase_data,2)
0188 error('write failed')
0189 end;
0190
0191 end;
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 end
0203 end
0204 end;
0205 end;
0206 fclose('all');
0207
0208
0209