[128] | 1 | function [kspace,data,msg_out]=epi_recon(kspace,Dat,procpar) |
---|
| 2 | % This is a custom VNMR k-space reconstruction code for EPI data used by |
---|
| 3 | % aedes_readvnmr. |
---|
| 4 | |
---|
| 5 | % If called without input arguments, return the sequence names that |
---|
| 6 | % this code reconstructs |
---|
| 7 | if nargin==0 |
---|
[169] | 8 | kspace = {'epi_se_rapid_sp3','epi','epi_fMRI','epip','epiT1rho'}; |
---|
[128] | 9 | return |
---|
| 10 | end |
---|
| 11 | |
---|
| 12 | data=[]; |
---|
| 13 | msg_out = ''; |
---|
| 14 | |
---|
[152] | 15 | % Number of receivers |
---|
| 16 | if isfield(procpar,'rcvrs') && ~isempty(procpar.rcvrs) |
---|
| 17 | nRcvrs = length(procpar.rcvrs{1}=='y'); |
---|
| 18 | else |
---|
| 19 | nRcvrs = 1; |
---|
| 20 | end |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | % ===================================================== |
---|
| 25 | % Reconstruct EPI sequences meaured with the old INOVA |
---|
| 26 | % ===================================================== |
---|
[128] | 27 | if isfield(procpar,'readres') && isfield(procpar,'phaseres') |
---|
| 28 | |
---|
| 29 | % Number of slices |
---|
| 30 | tmp_ns=length(procpar.pss); |
---|
| 31 | |
---|
| 32 | if isfield(procpar,'navecho') && strcmpi(procpar.navecho{1},'y') |
---|
| 33 | tmp_nv = procpar.nv-procpar.nseg; |
---|
| 34 | else |
---|
| 35 | tmp_nv = procpar.nv; |
---|
| 36 | end |
---|
| 37 | kspace = reshape(kspace,[size(kspace,1) ... |
---|
| 38 | size(kspace,2)/tmp_nv tmp_nv]); |
---|
| 39 | kspace = permute(kspace,[1 3 2]); |
---|
| 40 | |
---|
| 41 | % Reshape to 4D matrix |
---|
| 42 | kspace = reshape(kspace,[size(kspace,1) size(kspace,2) ... |
---|
| 43 | tmp_ns size(kspace,3)/tmp_ns]); |
---|
[152] | 44 | |
---|
| 45 | |
---|
[153] | 46 | data = abs(fftshift(fftshift(fft(fft(kspace,[],1),[],2),1),2)); |
---|
| 47 | data = permute(data,[2 1 3 4]); |
---|
| 48 | data = flipdim(flipdim(data,1),2); |
---|
| 49 | data(:,:,:,1) = []; % Remove reference image |
---|
| 50 | |
---|
[152] | 51 | % =================================== |
---|
| 52 | % Reconstruct VNMRj and EPIP sequences |
---|
| 53 | % =================================== |
---|
[128] | 54 | else |
---|
[152] | 55 | |
---|
[169] | 56 | if isfield(procpar,'nphase') && ... |
---|
[152] | 57 | isfield(procpar,'nread') |
---|
| 58 | isEPIP = true; |
---|
| 59 | else |
---|
| 60 | isEPIP = false; |
---|
| 61 | end |
---|
| 62 | |
---|
| 63 | % Number of navigator echos |
---|
| 64 | if isfield(procpar,'navigator') && strcmpi(procpar.navigator,'y') |
---|
| 65 | nNav = length(procpar.nav_echo); |
---|
| 66 | else |
---|
| 67 | nNav = 0; |
---|
| 68 | end |
---|
| 69 | |
---|
| 70 | % Number of reference images |
---|
| 71 | if isfield(procpar,'epiref_type') && ~isempty(procpar.epiref_type) && ... |
---|
| 72 | strcmpi(procpar.epiref_type,'triple') |
---|
| 73 | nRef = 3; |
---|
| 74 | else |
---|
| 75 | nRef = 1; |
---|
| 76 | end |
---|
| 77 | |
---|
[162] | 78 | % Number of segments |
---|
| 79 | if isfield(procpar,'nseg') && ~isempty(procpar.nseg) |
---|
| 80 | nSeg = procpar.nseg; |
---|
| 81 | else |
---|
| 82 | nSeg = 1; |
---|
| 83 | end |
---|
| 84 | |
---|
[152] | 85 | % Number of volumes |
---|
| 86 | nVols = length(procpar.image)-nRef; |
---|
| 87 | |
---|
| 88 | % Number of slices |
---|
| 89 | ns = procpar.ns; |
---|
| 90 | |
---|
[153] | 91 | |
---|
[152] | 92 | if isEPIP |
---|
[153] | 93 | nv = procpar.nphase; % Phase sampling |
---|
| 94 | np = procpar.nread/2; % Read sampling |
---|
[152] | 95 | else |
---|
| 96 | nv = procpar.nv; |
---|
| 97 | np = procpar.np/2; |
---|
| 98 | end |
---|
| 99 | |
---|
| 100 | % Get orientation |
---|
[153] | 101 | orient = procpar.orient{1}; |
---|
[152] | 102 | |
---|
[157] | 103 | if isEPIP && (nv==64 & np==64) |
---|
| 104 | % The information about navigator echoes in the procpar doesn't seem to |
---|
| 105 | % be consistent. This code tried to guess the number of navigators from |
---|
| 106 | % the k-space size |
---|
| 107 | nNav = size(kspace,1)/np-nv; |
---|
[162] | 108 | end |
---|
| 109 | |
---|
| 110 | % Calculate indexes for navigator and data echoes |
---|
| 111 | if nNav > 0 |
---|
| 112 | ind = reshape([1:(nv+nNav*nSeg)],[],nSeg); |
---|
| 113 | NavInd = ind(1:nNav,:); |
---|
| 114 | DataInd = ind(nNav+1:end,:); |
---|
| 115 | NavInd = NavInd(:).'; |
---|
| 116 | DataInd = DataInd(:).'; |
---|
[152] | 117 | else |
---|
[162] | 118 | DataInd = [1:nv]; |
---|
| 119 | NavInd = []; |
---|
[152] | 120 | end |
---|
| 121 | |
---|
| 122 | if Dat.EPIPhasedArrayData |
---|
[153] | 123 | data = zeros(nv,np,ns,nVols,nRcvrs,'single'); |
---|
[152] | 124 | else |
---|
| 125 | data = zeros(nv,np,ns,nVols,'single'); |
---|
| 126 | end |
---|
| 127 | |
---|
[162] | 128 | % Look for phase table |
---|
| 129 | pe_table = []; |
---|
| 130 | if isfield(Dat,'phasetable') && ~isempty(Dat.phasetable) |
---|
| 131 | pe_table = Dat.phasetable; |
---|
| 132 | end |
---|
| 133 | |
---|
[152] | 134 | % Reshape kspace and detach reference images |
---|
[162] | 135 | kspace = reshape(kspace,np,nv+nNav*nSeg,ns,nRcvrs,nVols+nRef); |
---|
[152] | 136 | |
---|
[162] | 137 | ref_data = kspace(:,DataInd,:,:,1:nRef); |
---|
| 138 | if ~isempty(pe_table) |
---|
| 139 | ref_data(:,pe_table,:,:,:) = ref_data; |
---|
| 140 | end |
---|
[152] | 141 | % Flip even kspace lines |
---|
[153] | 142 | ref_data(:,2:2:end,:,:,:) = flipdim(ref_data(:,2:2:end,:,:,:),1); |
---|
| 143 | %kspace(:,:,:,:,1:nRef)=[]; |
---|
[152] | 144 | |
---|
| 145 | % Calculate phase corrections for all receivers |
---|
| 146 | phase_e = ref_data(:,:,:,:,1); |
---|
| 147 | rev_phase = ref_data(:,:,:,:,1); |
---|
| 148 | for ii=1:nRcvrs |
---|
| 149 | if nRef==1 |
---|
[153] | 150 | ref_im = ref_data(:,:,:,ii,1); |
---|
[152] | 151 | phase1 = exp(-sqrt(-1)*angle(fft(ref_im,[],1))); |
---|
| 152 | rev_phase = 1; |
---|
[153] | 153 | phase_e(:,:,:,ii,:) = phase1; |
---|
[152] | 154 | elseif nRef==3 |
---|
| 155 | ref1_ind = find(procpar.image==0,1); |
---|
[153] | 156 | ref1 = ref_data(:,:,:,ii,ref1_ind); |
---|
[152] | 157 | phase1 = exp(-sqrt(-1)*angle(fft(ref1,[],1))); |
---|
| 158 | |
---|
| 159 | ref2_ind = find(procpar.image==-2,1); |
---|
[153] | 160 | ref2 = flipdim(ref_data(:,:,:,ii,ref2_ind),1); |
---|
[152] | 161 | phase2 = exp(-sqrt(-1)*angle(fft(ref2,[],1))); |
---|
| 162 | |
---|
| 163 | im1_ind = find(procpar.image==-1,1); |
---|
[153] | 164 | im1 = flipdim(ref_data(:,:,:,ii,im1_ind),1); |
---|
[152] | 165 | |
---|
[153] | 166 | rev_phase(:,:,:,ii,:) = fft(im1,[],1).*phase2; |
---|
| 167 | phase_e(:,:,:,ii,:) = phase1; |
---|
[152] | 168 | end |
---|
| 169 | end |
---|
| 170 | |
---|
| 171 | % Reconstruct in blocks |
---|
| 172 | kssz=size(kspace); |
---|
| 173 | blksz = Dat.EPIBlockSize; % Process EPI data in 100 volume blocks (default) |
---|
| 174 | nBlocks = ceil(nVols/blksz); |
---|
| 175 | lnum = length(num2str(nBlocks)); |
---|
| 176 | lnumstr = num2str(lnum); |
---|
| 177 | bsl = lnum*2+1; |
---|
| 178 | fprintf(1,'Processing data in blocks of %d volumes\n',blksz) |
---|
| 179 | fprintf(1,['Processing block...%0',lnumstr,'d/%0',lnumstr,'d'],1,nBlocks); |
---|
| 180 | |
---|
| 181 | for ii=1:nBlocks |
---|
| 182 | fprintf(1,repmat('\b',1,bsl)); |
---|
| 183 | fprintf(1,['%0',lnumstr,'d/%0',lnumstr,'d'],ii,nBlocks); |
---|
| 184 | tmp_data = []; |
---|
| 185 | if ii==nBlocks |
---|
[153] | 186 | vol_inds = (ii-1)*blksz+1+nRef:nVols+nRef; |
---|
[152] | 187 | else |
---|
[153] | 188 | vol_inds = (ii-1)*blksz+1+nRef:ii*blksz+nRef; |
---|
[152] | 189 | end |
---|
| 190 | |
---|
[162] | 191 | % At the moment navigator echoes are not used... |
---|
| 192 | %tmp_kspace = kspace(:,nNav+1:end,:,:,vol_inds); |
---|
| 193 | tmp_kspace = kspace(:,DataInd,:,:,vol_inds); |
---|
| 194 | if ~isempty(pe_table) |
---|
| 195 | tmp_kspace(:,pe_table,:,:,:) = tmp_kspace; |
---|
| 196 | end |
---|
| 197 | |
---|
[153] | 198 | % Flip even lines |
---|
| 199 | tmp_kspace(:,2:2:end,:,:,:) = flipdim(tmp_kspace(:,2:2:end,:,:,:),1); |
---|
| 200 | |
---|
[152] | 201 | for kk=1:nRcvrs |
---|
| 202 | if nRef==3 |
---|
| 203 | for tt=1:length(vol_inds) |
---|
[153] | 204 | tmp_kspace(:,:,:,kk,tt) = ifft(rev_phase(:,:,:,kk,:)+fft(tmp_kspace(:,:,:,kk,tt),[],1).*phase_e(:,:,:,kk,:)); |
---|
[152] | 205 | end |
---|
| 206 | else |
---|
| 207 | for tt=1:length(vol_inds) |
---|
[153] | 208 | tmp_kspace(:,:,:,kk,tt) = ifft(fft(tmp_kspace(:,:,:,kk,tt),[],1).*phase_e(:,:,:,kk,:)); |
---|
[152] | 209 | end |
---|
| 210 | end |
---|
| 211 | tmp_data = fftshift(fftshift(fft(fft(tmp_kspace,[],1),[],2),1),2); |
---|
| 212 | end |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | if Dat.EPIPhasedArrayData |
---|
| 216 | data_block = abs(tmp_data); |
---|
| 217 | % Permute to correct orientation |
---|
| 218 | if strcmpi(orient,'trans90') |
---|
[153] | 219 | data_block = permute(data_block,[2 1 3 5 4]); |
---|
| 220 | data_block = flipdim(data_block,2); |
---|
| 221 | else |
---|
| 222 | data_block = permute(data_block,[1 2 3 5 4]); |
---|
[152] | 223 | end |
---|
[158] | 224 | data(:,:,:,vol_inds-nRef,:) = data_block; |
---|
[152] | 225 | else |
---|
[162] | 226 | if nRcvrs == 1 |
---|
| 227 | data_block = abs(tmp_data); |
---|
| 228 | else |
---|
| 229 | data_block = sqrt(sum(tmp_data.*conj(tmp_data),4)); |
---|
| 230 | end |
---|
[152] | 231 | % Permute to correct orientation |
---|
| 232 | if strcmpi(orient,'trans90') |
---|
[153] | 233 | data_block = permute(data_block,[2 1 3 5 4]); |
---|
| 234 | data_block = flipdim(data_block,2); |
---|
[152] | 235 | end |
---|
[153] | 236 | data(:,:,:,vol_inds-nRef) = squeeze(data_block); |
---|
[152] | 237 | end |
---|
| 238 | |
---|
| 239 | end |
---|
| 240 | fprintf(1,'\n') |
---|
| 241 | end |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | |
---|