Home > eeg > egis > scripts > make_phase.m

make_phase

PURPOSE ^

status = make_phase(csdmfiles,outfname,win,rel_chan,output);

SYNOPSIS ^

function status = make_phase(csdmfiles,outfname,win,rel_chan,output);

DESCRIPTION ^

status = make_phase(csdmfiles,outfname,win,rel_chan,output);

    this function makes phase files derived files from csdm files
    the names of the csdm files must be stored in an array where each row 
        is a filename - csdmfiles.   
    outfname = name of output file with rel_chan appended
    win = which window (observation number) to use in each file

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function status = make_phase(csdmfiles,outfname,win,rel_chan,output);
0002 %status = make_phase(csdmfiles,outfname,win,rel_chan,output);
0003 %
0004 %    this function makes phase files derived files from csdm files
0005 %    the names of the csdm files must be stored in an array where each row
0006 %        is a filename - csdmfiles.
0007 %    outfname = name of output file with rel_chan appended
0008 %    win = which window (observation number) to use in each file
0009 
0010 %    COMING SOON    if win = -1 generate one file for each observation
0011 %            if win = 0 generate one file fo reach subject
0012 %    rel_chan = index channels for phase
0013 %    output = 'ang' for angle, 'cos' for cosine phase
0014 %        defaults to 'cos'
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 % read in a csdm header in order to make an output file header
0026 %
0027 ave_hdr_offsets_v;
0028 %csdm_fid = fopen(csdmfiles(1,:),'r');
0029 %[csdm_fhdr,csdm_chdr,ename,czeros,cgains,cnames,fcom,ftext, coff]=rd_csdm_hdr_v(csdm_fid);
0030 %
0031 %[cell_data_offsets]=get_csdm_cell_off(csdm_fhdr, csdm_chdr);
0032 %
0033 %if win > csdm_chdr(1,NObs)
0034 %    error('window out of range')
0035 %end;%
0036 %
0037 %ave_fhdr = csdm_fhdr;
0038 %if csdm_fhdr(NChan) == 8385
0039 %    ave_fhdr(NChan) = 129;
0040 %elseif csdm_fhdr(NChan) == 2145
0041 %    ave_fhdr(NChan) = 65;
0042 %elseif csdm_fhdr(NChan) == 33153;
0043 %    ave_fhdr(NChan) = 257;
0044 %else
0045 %    error('Nchan is unacceptable in this csdm file')
0046 %end;
0047  
0048 
0049 % Copy average file subject specifics array
0050 %ave_chdr = concat_csdm_chdr(csdmfiles, win);
0051 %
0052 %ave_ename = ename; ave_fcom = fcom; ave_ftext = ftext;
0053 %ave_fhdr(LastDone) = size(csdmfiles,1);
0054 %ave_cnames = cnames;
0055 
0056 % Copy the experiment info from the session file to the average file tspecs
0057 %[ave_chdr] = sesfhdr2avetspecs(ses_fhdr, ave_chdr);
0058 
0059 %Fix header
0060 %for c = 1:csdm_fhdr(NCells)
0061 %        ave_chdr(c,NObs) = size(csdmfiles,1);
0062 %    ave_chdr(c,NPoints) = csdm_chdr(c,NPoints)/2;
0063 %end
0064 %for i = 1:size(rel_chan,2)
0065 %    outfname1 = [outfname '_' int2str(rel_chan(i))];
0066 %    ave_fid(i) = fopen(outfname1,'wb');
0067 %    % Write out the average file header
0068 %    wt_ave_hdr_v(ave_fid(i),ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext);
0069 %%    fseek(ave_fid(i), 0, 'eof');
0070 %end;
0071 %fclose('all');
0072 
0073 %
0074 % Read in copy of the csdm header
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 % Copy csdm headers to average header
0085 %
0086 
0087 [ave_fhdr,ave_chdr,ave_ename,ave_cnames,ave_fcom,ave_ftext] = csdmhdr2avehdr(csdmfiles, win);
0088 
0089 % Correct the NPoints field for each cell header
0090 % This must be done independently in each m-file since in some cases the
0091 % resulting matrices are NChan X NChan instead of NPoints X NChan
0092 
0093 for c = 1:csdm_fhdr(NCells)
0094         ave_chdr(c,NPoints) = csdm_chdr(c,NPoints)/2;
0095 end
0096 
0097 %
0098 % Determine the number of output files
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 %            outfname1 = [outfname '_' int2str(rel_chan(irel))];
0197 %            fid1 = fopen(outfname1,'ab');
0198 %            num_written = fwrite(fid1,phase_data','int16');
0199 %            fclose(fid1);
0200 %            if num_written~= size(phase_data,1)*size(phase_data,2)
0201 %                error('write failed')            end;
0202         end
0203            end
0204     end; %for ifile =
0205 end; % for icell
0206 fclose('all');
0207 
0208 
0209

Generated on Wed 20-Mar-2019 04:00:51 by m2html © 2003