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