0001 function image_binary = make_meg_image(data,bad_chan,plot_res,image_rows,image_columns,out_root);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 if ~ (nargin ==1|nargin ==2|nargin == 3|nargin == 5|nargin == 6)
0017 error('inappropriate number of input arguments');
0018 end;
0019
0020 if nargin < 2
0021 bad_chan = [];
0022 plot_res = 100;
0023 end
0024 if nargin < 3
0025 plot_res= 100;
0026 end;
0027 if nargin < 4
0028 image_binary = zeros(plot_res,plot_res*size(data,1));
0029 image_rows = 1;
0030 image_columns = size(data,1);
0031 else
0032 image_binary = zeros(plot_res*image_rows,plot_res*image_columns);
0033 end;
0034
0035 [xelec,yelec,zelec] = sensors(148);
0036 xelec(1)
0037 good_chan_mask = ones(1,size(data,2));
0038 ruu = sqrt(xelec(1).^2+yelec(1).^2+zelec(1).^2);
0039 [xs,ys,zs] = polgrid_meg(plot_res,ruu);
0040 if ~isempty(bad_chan)
0041 good_chan_mask(1,bad_chan) = zeros(1,size(bad_chan,2));
0042 end;
0043 good_chan = find(good_chan_mask(1,:));
0044 x = zeros(1,size(good_chan,2));
0045 y = zeros(1,size(good_chan,2));
0046 z = zeros(1,size(good_chan,2));
0047 x = xelec(good_chan);
0048 y = yelec(good_chan);
0049 z = zelec(good_chan);
0050 elec = xyz2tp(x,y,z);
0051 max_mask = 120;
0052 welec = 1;
0053 icount = 1;
0054 [k,kinv,a,ainv,e] = k_and_e(welec,x,y,z);
0055 for i = 1:image_rows
0056 for j = 1:image_columns
0057 w = data(icount,:);
0058 v = zeros(1,size(good_chan,2));
0059 v = w(good_chan);
0060 icount = icount+1;
0061 [p,q,error_check]= mateqs(welec,x,y,z,v,k,kinv,a,ainv,e);
0062 image_3d= interp_3d(welec,x,y,z,xs,ys,zs,p,q);
0063
0064 imagemat= mask_meg(xs,ys,zs,image_3d,max_mask);
0065 imagemat = flipud(imagemat);
0066 imagemat = fliplr(imagemat);
0067 image_binary(plot_res*(i-1)+1:plot_res*(i-1)+plot_res,plot_res*(j-1)+1:plot_res*j)= imagemat;
0068 end;
0069 end;
0070 if (nargin == 6)
0071 if ~isempty(out_root)
0072 out_fname = [out_root '.img'];
0073 fid = fopen(out_fname,'wb');
0074 if fid < 0
0075 error('couldnt open output file')
0076 end
0077 else
0078 fid = -1;
0079 end
0080 elseif nargin < 6
0081 fid = put_fid('wb');
0082 if fid < 0
0083 error('couldnt open output file')
0084 end
0085 end;
0086 if fid ~= -1
0087 size(image_binary);
0088 num_written = fwrite(fid, size(image_binary), 'float');
0089 if num_written ~= 2
0090 error('Failed to write header info');
0091 end
0092 fwrite(fid,plot_res,'float');
0093 num_written = fwrite(fid,image_binary','float');
0094 if num_written ~= size(image_binary,1)*size(image_binary,2)
0095 error('write-out filed')
0096 end;
0097 fclose(fid);
0098 end;
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108