0001 function [samp,pick12,pick22,bexp,ier]=readSBItrials(meg_dir,sysid,trials,chgdcm)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 global vbmeg_inst;
0031 define = vbmeg_inst.const;
0032
0033 pos_dir = '../pos';
0034 head_dir = '../head';
0035 mri_dir = '../mri';
0036
0037 ier=0;
0038
0039
0040
0041
0042
0043 fid=fopen(sprintf('%s%s%s%s%s',meg_dir,head_dir,filesep,sysid,...
0044 define.HEADER_EXTENSION),'r','l');
0045
0046 if fid==-1, disp('XXX Error : Cannot open head file !!\n'); ier=1; return; end
0047
0048 H_file=meg_hread('file',fid);
0049
0050
0051 H_meg=meg_hread('meg',fid,H_file.i_meg_begin);
0052 nch0=H_meg.i_nch;
0053 nsamp=H_meg.i_nsample;
0054 nprsamp=H_meg.i_nprsample;
0055 sampf=H_meg.i_samp_freq;
0056 nrept=H_meg.i_nwave;
0057 vbit=H_meg.f_ad_weight;
0058
0059
0060 kind0=[]; lock0=[]; pick10=[]; pick20=[]; amp0=[]; sens0=[];
0061
0062 for i=1:nch0
0063 H_meg_ch=meg_hread('meg_ch',fid,H_file.i_meg_ch_begin+H_file.i_meg_ch_size*(i-1));
0064 kind0(i)=H_meg_ch.i_pick_type; lock0(i)=H_meg_ch.i_meg_lock;
0065 pick10(i,1:3)=(H_meg_ch.st_dcp)'; pick10(i,4:6)=(H_meg_ch.st_dca)';
0066 pick20(i,1:3)=(H_meg_ch.st_ccp)'; pick20(i,4:6)=(H_meg_ch.st_cca)';
0067 amp0(i)=H_meg_ch.f_amp_gain; sens0(i)=H_meg_ch.f_phy_v_coef;
0068 end
0069
0070 if chgdcm,
0071
0072 H_pos=meg_hread('pos',fid,H_file.i_pos_begin);
0073 npos=H_pos.i_ncoil;
0074 H_pos_rept=meg_hread('pos_rept',fid,H_file.i_pos_rept_begin);
0075 pos_lcz_begin=H_pos_rept.i_data_begin;
0076
0077 H_mri=meg_hread('mri',fid,H_file.i_mri_begin);
0078 mark=H_mri.st_axes(1:npos,:);
0079 mrisys=H_mri.c_sysid{1};
0080
0081 H_lcz=meg_hread('lcz',fid,H_file.i_lcz_begin);
0082 sphr=H_lcz.f_model_r;
0083 sph0=H_lcz.st_model;
0084 end
0085
0086 fclose(fid); clear H_file H_meg H_meg_ch H_pos H_mri H_lcz;
0087
0088
0089
0090
0091
0092 fid=fopen(sprintf('%s%s%s',meg_dir,sysid,...
0093 define.MEG1_EXTENSION),'r','l');
0094
0095 if fid==-1, disp('XXX Error : Cannot open meg file !!\n'); ier=1; return; end
0096
0097 a=fread(fid,nrept*nch0*nsamp,'short');
0098 fclose(fid);
0099
0100 bexp00=(reshape(a,nch0*nsamp,nrept)); clear a;
0101 bexp0=bexp00(:,trials); clear bexp00;
0102 nrept=length(trials);
0103 bexp0=(reshape(bexp0,nch0,nsamp,nrept)); clear a;
0104
0105 if 0,
0106 for i=1:nrept,
0107 for j=1:nch0,
0108 bexp0(j,:,i)=bexp0(j,:,i)-mean(bexp0(j,1:100,i));
0109 end
0110 end
0111 end
0112
0113 if chgdcm,
0114
0115 fid=fopen(sprintf('%s%s%s%s%s',meg_dir,pos_dir,filesep,sysid,...
0116 define.POS_EXTENSION),'r','l');
0117
0118 if fid==-1,
0119 disp('XXX Error : Cannot open pos file !!\n'); ier=1;
0120 return;
0121 end
0122
0123 fseek(fid,pos_lcz_begin,'bof');
0124 pos=[];
0125 for i=1:npos
0126 a=fread(fid,8,'float');
0127 b=reshape(a,1,8);
0128 pos(i,:)=b(1:3);
0129 end
0130 fclose(fid); clear a b;
0131 end
0132
0133
0134
0135
0136 okix=find(kind0==1 & lock0==0);
0137 nch=size(okix,2);
0138 pick1=pick10(okix,:);
0139 pick2=pick20(okix,:);
0140
0141 for i=1:nrept,
0142 bexp(:,:,i)=bexp0(okix,:,i)./(repmat(amp0(okix),nsamp,1))'.*(repmat(sens0(okix),nsamp,1))'*vbit;
0143 end
0144
0145 clear kind0 lock0 pick10 pick20 amp0 sens0 vbit bexp0 okix;
0146
0147 if ~chgdcm,
0148 pick12=pick1; pick22=pick2;
0149 samp=struct('nch',nch, ...
0150 'nsamp',nsamp, ...
0151 'nprsamp',nprsamp, ...
0152 'sampf',sampf, ...
0153 'nrept',nrept);
0154 return;
0155 end
0156
0157
0158
0159
0160
0161
0162 fname1=sprintf('%s%s%s%s%s%s%s',meg_dir,mri_dir,filesep,mrisys,...
0163 filesep,mrisys,define.DICOM1_EXTENSION);
0164
0165 fid=fopen(fname1);
0166
0167 if fid==-1,
0168 disp('XXX Error : Cannot open mri-table file !!\n'); ier=1;
0169 return;
0170 end
0171
0172 a=fscanf(fid,'%s');
0173 fclose(fid);
0174
0175 sx=str2num(a(findstr(a,'image_x_vector=')+15:findstr(a,'image_y_vector=')-1));
0176 sy=str2num(a(findstr(a,'image_y_vector=')+15:findstr(a,'image_z_vector=')-1));
0177 sz=str2num(a(findstr(a,'image_z_vector=')+15:findstr(a,'pixel_spacing=')-1));
0178 s0=str2num(a(findstr(a,'z0_slice_origin=')+16:findstr(a,'magnification=')-1));
0179 ss=str2num(a(findstr(a,'pixel_spacing=')+14:findstr(a,'z0_slice_origin=')-1));
0180 sp1=findstr(a,'slice_pitch=')+12; sp2=findstr(a(sp1:size(a,2)),'te=')-1; sp=str2num(a(sp1:sp1+sp2-1));
0181 nsl=str2num(a(findstr(a,'number_of_slices=')+17:findstr(a,'number_of_echos=')-1));
0182
0183 b=1;
0184 clear sloc;
0185 fid=fopen(fname1);
0186
0187 while b,
0188 if ~isempty(findstr(char(fgets(fid)),'slice_location_table')), b=0; end
0189 end
0190
0191 for i=1:nsl, sloc(i)=str2num(char(fgets(fid))); end
0192 fclose(fid);
0193 scorner=sz*min(sloc)+s0;
0194 dcms0=128*sx*ss+127*sy*ss+fix(nsl/2)*sz*sp+scorner; dcms0=dcms0.*1e-3;
0195
0196 mark2=mark+repmat(dcms0,npos,1);
0197 if sphr<0, sph02=sph0+dcms0';
0198 else, sph02=[0,0,0]; disp('XXX : No suport, because not using VIVID'); ier=1; return; end
0199
0200 pick1(:,4)=mod(pick1(:,4),pi); pick1(:,5:6)=mod(pick1(:,5:6),2*pi);
0201 pick2(:,4)=mod(pick2(:,4),pi); pick2(:,5:6)=mod(pick2(:,5:6),2*pi);
0202 pick1(:,4:6)=[sin(pick1(:,4)).*cos(pick1(:,5)) sin(pick1(:,4)).*sin(pick1(:,5)) cos(pick1(:,4))];
0203 pick2(:,4:6)=[sin(pick2(:,4)).*cos(pick2(:,5)) sin(pick2(:,4)).*sin(pick2(:,5)) cos(pick2(:,4))];
0204 for i=1:npos
0205 sfromc(i)=min(sqrt(sum((pick1(:,1:3)-repmat(pos(i,:),size(pick1,1),1)).^2,2)));
0206 end
0207 sel=find(sfromc<max(sfromc));
0208 ex=pos(sel(3),:)-pos(sel(1),:); ex=ex/norm(ex);
0209 ez=cross(ex,pos(sel(2),:)-pos(sel(1),:)); ez=ez/norm(ez);
0210 ey=cross(ez,ex); mc=[ex; ey; ez];
0211 oc=sum((pos(sel(2),:)-pos(sel(1),:)).*ex)*ex + pos(sel(1),:);
0212 ex=mark2(sel(3),:)-mark2(sel(1),:); ex=ex/norm(ex);
0213 ez=cross(ex,mark2(sel(2),:)-mark2(sel(1),:)); ez=ez/norm(ez);
0214 ey=cross(ez,ex); mm=[ex; ey; ez];
0215 om=sum((mark2(sel(2),:)-mark2(sel(1),:)).*ex)*ex + mark2(sel(1),:);
0216 trans = mm'*mc;
0217 pick12(:,1:3)=(trans*(pick1(:,1:3)-repmat(oc,nch,1))')'+repmat(om,nch,1);
0218 pick22(:,1:3)=(trans*(pick2(:,1:3)-repmat(oc,nch,1))')'+repmat(om,nch,1);
0219 pick12(:,4:6)=(trans*pick1(:,4:6)')';
0220 pick22(:,4:6)=(trans*pick2(:,4:6)')';
0221 samp=struct('nch',nch, ...
0222 'nsamp',nsamp, ...
0223 'nprsamp',nprsamp, ...
0224 'sampf',sampf, ...
0225 'nrept',nrept, ...
0226 'sph0',sph02, ...
0227 'sphr',sphr, ...
0228 'mrisys',mrisys);
0229