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