0001 function status = image_eeg_coherence(freqs,dimen,obs,paging,plot_res,ref_chan,coherencefname);
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 
0015 
0016 
0017 
0018 if nargin < 8
0019     [fid, fname, pathname] = get_fid('rb');
0020 else
0021     fid = fopen(coherencefname,'rb');
0022     pathname = [];
0023 end;
0024 version = fread(fid,1,'int16');
0025 if version ~= -2
0026     error('this is not a coherence file');
0027 end;
0028  [nfiles,obs_labels,Epoch,Window_Length,NEpoch,Nbad_chan,bad_chan,ref_flag,reference,NChan,NFreq] = rd_anal_hdr(fid);
0029 if nargin == 0
0030     freqs = [5:1:20];
0031     obs = [1:nfiles];
0032     dimen = [sqrt(size(freqs,2)) sqrt(size(freqs,2))];
0033     paging = 'byobs';
0034     plot_res = 50;
0035     ref_chan = 73;
0036 end;
0037 if nargin == 1
0038     obs = [1:nfiles];
0039     dimen = [sqrt(size(freqs,2)) sqrt(size(freqs,2))];
0040     paging = 'byobs';
0041     plot_res = 50;
0042     ref_chan = 73;
0043 end;        
0044 if nargin == 2
0045     obs = [1:nfiles];
0046     paging = 'byobs';
0047     plot_res = 50;
0048     ref_chan = 73;
0049 end;    
0050 if nargin == 3
0051     paging = 'byobs';
0052     plot_res = 50;
0053     ref_chan = 73;
0054 end;
0055 if nargin == 4
0056     plot_res = 50;
0057     ref_chan = 73;
0058 end;
0059 if nargin == 5
0060     ref_chan = 73;
0061 end;
0062 if isempty(paging)
0063     paging = 'byobs';
0064 end
0065 if isempty(plot_res)
0066     plot_res = 50;
0067 end;
0068 if isempty(dimen)
0069     dimen = [sqrt(size(freqs,2)) sqrt(size(freqs,2))];
0070 end
0071 
0072 if isempty(obs)
0073     obs = [1:nfiles];
0074 end;
0075 if isempty(ref_chan)
0076     ref_chan = 73;
0077 end;
0078 
0079 obs_mask = zeros(1,nfiles);
0080 obs_mask(obs) = ones(1,size(obs,2));
0081 ch_pair_indices;
0082 if strcmp(paging,'byobs')
0083     for i = 1:nfiles
0084         if obs_mask(i) > 0 
0085         avgcoherence = fread(fid,[NChan(i),NFreq(i)],'float');
0086         avgcoherence = avgcoherence';
0087         bins = freqs*Epoch(i) + ones(1,size(freqs,2));
0088         bchan = bad_chan(i,find(bad_chan(i,:)));
0089         imagename = [pathname deblank(setstr(obs_labels(i,:))) '.f' int2str(freqs) '.coh'];
0090         pchan = chpair2nchan(NChan(i));
0091         cohplt = zeros(bins,pchan);
0092         for i = 1:pchan
0093             cohplt(bins,i) = avgcoherence(bins,chpair(ref_chan,i));
0094         end;
0095         make_a_image(cohplt(bins,:),bchan,plot_res,dimen(1),dimen(2),imagename);
0096         else
0097         skip_bytes = NChan(i)*NFreq(i)*4;
0098         fseek(fid,skip_bytes,'cof');
0099         end;
0100     end;
0101 elseif strcmp(paging,'cross') 
0102     image_binary = zeros(dimen(1)*plot_res,dimen(2)*plot_res);
0103     iff = 1;
0104     for i = 1:nfiles
0105         if obs_mask(i) > 0 
0106         avgcoherence = fread(fid,[NChan(i),NFreq(i)],'float');
0107         avgcoherence = avgcoherence';
0108         bins = freqs*Epoch(i) + ones(1,size(freqs,2));
0109         bchan = bad_chan(i,find(bad_chan(i,:)));
0110         pchan = chpair2nchan(NChan(i));
0111         for i = 1:pchan
0112             cohplt(bins,i) = avgcoherence(bins,chpair(ref_chan,i));
0113         end;
0114         image_binary((iff-1)*plot_res+1:iff*plot_res,:) = make_a_image(cohplt(bins,:),bchan,plot_res,1,size(bins,2),[]);
0115         iff = iff + 1;
0116         else
0117         skip_bytes = NChan(i)*NFreq(i)*4;
0118         fseek(fid,skip_bytes,'cof');
0119         end;
0120     end;
0121     imagename = [pathname 'image.o' int2str(obs) '.f' int2str(freqs) '.coh.img'];
0122     ifid = fopen(imagename,'wb');
0123     if ifid < 0
0124         fclose('all');
0125         error('file open for image failed');
0126     end;
0127     num_written = fwrite(ifid, size(image_binary), 'float');
0128     if num_written ~= 2
0129             error('Failed to write header info');
0130     end 
0131 
0132     num_written = fwrite(ifid,image_binary','float');
0133     if num_written ~= size(image_binary,1)*size(image_binary,2)
0134         error('write-out failed')
0135     end;
0136 end;    
0137 status = 1;
0138 
0139 
0140 
0141 
0142 
0143 
0144 
0145