


Removing the linear trend for REST by Xiao-Wei Song
Usage: rest_detrend(ADataDir, APostfix)
ADataDir where the 3d+time dataset stay, and there should be 3d EPI functional image files. It must not contain / or \ at the end.
------------------------------------------------------------------------------------------------------------------------------
Remove linear trend
Save to ADataDir_APostfix
------------------------------------------------------------------------------------------------------------------------------
Copyright(c) 2007~2010
State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
Written by Xiao-Wei Song
http://resting-fmri.sourceforge.net
Dawnwei.Song@gmail.com, Copyright 2007~2010
------------------------------------------------------------------------------------------------------------------------------
Mail to Authors: <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a>
Version=1.3;
Release=20090321;
Revised by YAN Chao-Gan 080610: NIFTI compatible
Last Revised by YAN Chao-Gan, 090321. Data in processing will not be converted to the format 'int16'.


0001 function rest_detrend(ADataDir, APostfix) 0002 %Removing the linear trend for REST by Xiao-Wei Song 0003 %Usage: rest_detrend(ADataDir, APostfix) 0004 %ADataDir where the 3d+time dataset stay, and there should be 3d EPI functional image files. It must not contain / or \ at the end. 0005 %------------------------------------------------------------------------------------------------------------------------------ 0006 % Remove linear trend 0007 % Save to ADataDir_APostfix 0008 % 0009 %------------------------------------------------------------------------------------------------------------------------------ 0010 % Copyright(c) 2007~2010 0011 % State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University 0012 % Written by Xiao-Wei Song 0013 % http://resting-fmri.sourceforge.net 0014 % Dawnwei.Song@gmail.com, Copyright 2007~2010 0015 %------------------------------------------------------------------------------------------------------------------------------ 0016 % Mail to Authors: <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a> 0017 % Version=1.3; 0018 % Release=20090321; 0019 % Revised by YAN Chao-Gan 080610: NIFTI compatible 0020 % Last Revised by YAN Chao-Gan, 090321. Data in processing will not be converted to the format 'int16'. 0021 0022 fprintf('\nRemoving the linear trend:\n'); 0023 [AllVolume,VoxelSize,theImgFileList, Header] =rest_to4d(ADataDir); 0024 0025 thePrecision ='double'; %Revised by YAN Chao-Gan, 090321. Data will not be converted to the format 'int16'. %thePrecision ='int16'; 0026 tmpData =double(squeeze(AllVolume(:, :, :, round(size(AllVolume, 4)/2)))); 0027 if mean(abs(tmpData(0~=tmpData)))<100, %I can't use mean because It use too much memory! 0028 thePrecision ='double'; 0029 end 0030 0031 % examin the dimensions of the functional images and set mask 0032 nDim1 = size(AllVolume,1); nDim2 = size(AllVolume,2); nDim3 = size(AllVolume,3); nDim4 =size(AllVolume,4); 0033 0034 % I have to create a for loop because detrend can support 2-dim at most 0035 for x=1:nDim1, 0036 rest_waitbar(x/nDim1, ... 0037 'Removing Linear Trend, wait...', ... 0038 'Detrend','Child','NeedCancelBtn'); 0039 0040 oneAxialSlice =double(AllVolume(x, :, :, :)); 0041 oneAxialSlice =reshape(oneAxialSlice, 1*nDim2*nDim3, nDim4)'; 0042 oneAxialSlice =detrend(oneAxialSlice) +repmat(mean(oneAxialSlice), [size(oneAxialSlice,1), 1]); 0043 oneAxialSlice =reshape(oneAxialSlice', 1,nDim2,nDim3, nDim4); 0044 if strcmpi(thePrecision, 'int16'), 0045 AllVolume(x, :, :, :) =uint16(oneAxialSlice); 0046 else 0047 AllVolume(x, :, :, :) =(oneAxialSlice); 0048 end 0049 0050 % dim3PlusTimeCourse =squeeze( AllVolume(x, :, :, :) ); 0051 % theTimeCourse =double(dim3PlusTimeCourse'); %detrend only can do along the column, before detrend 0052 % dim3PlusTimeCourse =detrend(theTimeCourse); 0053 % dim3PlusTimeCourse =dim3PlusTimeCourse + ... 0054 % repmat(mean(theTimeCourse), [size(dim3PlusTimeCourse,1), 1]); 0055 % dim3PlusTimeCourse =dim3PlusTimeCourse'; %detrend only can do along the column, go back 0056 %% AllVolume(x, y, :, :) =uint16(dim3PlusTimeCourse); %20071031, Dawnwei.Song revised! 0057 % if strcmpi(thePrecision, 'int16'), 0058 % AllVolume(x, :, :, :) =uint16(dim3PlusTimeCourse); 0059 % else 0060 % AllVolume(x, :, :, :) =(dim3PlusTimeCourse); 0061 % end 0062 0063 end; 0064 0065 if strcmp(ADataDir(end),filesep)==1, 0066 ADataDir=ADataDir(1:end-1); 0067 end 0068 0069 ADataDir =sprintf('%s%s',ADataDir,APostfix); 0070 ans=rmdir(ADataDir, 's');%suppress the error msg 0071 [theParentDir,theOutputDirName]=fileparts(ADataDir); 0072 mkdir(theParentDir, theOutputDirName); %Matlab 6.5 compatible 0073 0074 sampleLength =size(theImgFileList,1); 0075 for x=1:sampleLength, 0076 rest_waitbar(x/sampleLength, ... 0077 sprintf('Saving to {hdr/img} pair files\nwait...'), ... 0078 'Remove linear trend Over','Child','NeedCancelBtn'); 0079 rest_writefile(single(AllVolume(:, :, :, x)), ... 0080 sprintf('%s%s%.8d', ADataDir, filesep,x), ... 0081 [nDim1,nDim2,nDim3],VoxelSize, Header,'single'); %Revised by YAN Chao-Gan, 090321. Detrended data will be stored in 'single' format. %'int16'); 0082 if (mod(x,5)==0) %progress show 0083 fprintf('.'); 0084 end 0085 end 0086 fprintf('\n');