Home > fmri > utils > simple_replace.m

simple_replace

PURPOSE ^

simple_replace(epi_dir,epi_stub,bad_vol,good_vols);

SYNOPSIS ^

function simple_replace(epi_dir,epi_stub,bad_vol,good_vols)

DESCRIPTION ^

 simple_replace(epi_dir,epi_stub,bad_vol,good_vols);

 Replaces specified volume with average of other specified volumes.
 Good for fixing slice-shuffled volumes.

 Assumes that the last portion of the file names has the format:
     i%04d.img, e.g. i0037.img

 WARNING: Make sure you have a copy of the original data, as this will
 overwrite the existing image.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function simple_replace(epi_dir,epi_stub,bad_vol,good_vols)
0002 % simple_replace(epi_dir,epi_stub,bad_vol,good_vols);
0003 %
0004 % Replaces specified volume with average of other specified volumes.
0005 % Good for fixing slice-shuffled volumes.
0006 %
0007 % Assumes that the last portion of the file names has the format:
0008 %     i%04d.img, e.g. i0037.img
0009 %
0010 % WARNING: Make sure you have a copy of the original data, as this will
0011 % overwrite the existing image.
0012 
0013 % v.0.0.1 6/20/02 Petr Janata
0014 % v.0.0.2 08/09/02 PJ  Data are read in via SPM functions to avoid potential scaling
0015 %                      factor problems.
0016 
0017 if nargin < 4
0018   error('Too few arguments')
0019 end
0020 
0021 nrep = length(good_vols);
0022 
0023 bad_vol_fname = spm_get('Files',epi_dir,sprintf('%s*i%04d.img', epi_stub,bad_vol));
0024 if isempty(bad_vol_fname)
0025   error(sprintf('Could not locate a file in %s starting with %s and volume number %d', epi_dir, epi_stub, bad_vol))
0026 end
0027 
0028 Vbad = spm_vol(bad_vol_fname);
0029 
0030 good_data = zeros(Vbad.dim(1:3));
0031 nslice = Vbad.dim(3);
0032 
0033 for irep = 1:nrep
0034   good_vol_fname = spm_get('Files',epi_dir,sprintf('%s*i%04d.img',epi_stub,good_vols(irep)));
0035 
0036   if isempty(good_vol_fname)
0037     error(sprintf('Could not locate a file in %s starting with %s and volume number %d', epi_dir, epi_stub, good_vols(irep)))
0038   end
0039   
0040   Vgood = spm_vol(good_vol_fname);
0041   for islice = 1:nslice
0042     good_data(:,:,islice) = good_data(:,:,islice) + ...
0043     spm_slice_vol(Vgood,spm_matrix([0 0 islice]),Vgood.dim(1:2),0);
0044   end
0045 end
0046 
0047 good_data = good_data/nrep; % get the mean
0048 
0049 % Write out the modified volume
0050 Vbad = spm_write_vol(Vbad,good_data);
0051

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003