0001 function [] = alff(ADataDir,ASamplePeriod, ALowPass_HighCutoff, AHighPass_LowCutoff, AMaskFilename,AResultFilename)
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 if nargin~=6
0027 error(' Error using ==> alff. 6 arguments wanted.');
0028 end
0029
0030 theElapsedTime =cputime;
0031 fprintf('\nComputing ALFF with:\t"%s"', ADataDir);
0032 [AllVolume,vsize,theImgFileList, Header] =rest_to4d(ADataDir);
0033
0034
0035 [nDim1 nDim2 nDim3 nDim4]=size(AllVolume);
0036
0037 isize = [nDim1 nDim2 nDim3];
0038
0039
0040
0041 theTempDatasetDirName =sprintf('ALFF_%d', fix((1e4) *rem(now, 1) ));
0042 theTempDatasetDir =[tempdir theTempDatasetDirName] ;
0043 ans=rmdir(theTempDatasetDir, 's');
0044 mkdir(tempdir, theTempDatasetDirName);
0045
0046 Save1stDimPieces(theTempDatasetDir, AllVolume, 'dim1_');
0047 clear AllVolume;
0048
0049 fprintf('\n\t Load mask "%s".', AMaskFilename);
0050 mask=rest_loadmask(nDim1, nDim2, nDim3, AMaskFilename);
0051
0052 fprintf('\n\t Build ALFF filtered mask.\tWait...');
0053 sampleFreq = 1/ASamplePeriod;
0054 sampleLength = size(theImgFileList,1);
0055 paddedLength = rest_nextpow2_one35(sampleLength);
0056 freqPrecision= sampleFreq/paddedLength;
0057
0058 mask =logical(mask);
0059 maskLowPass = repmat(mask, [1, 1, 1, paddedLength]);
0060 maskHighPass= maskLowPass;
0061 clear mask;
0062
0063
0064 idxLowPass_HighCutoff =round(ALowPass_HighCutoff *paddedLength *ASamplePeriod);
0065
0066 if (ALowPass_HighCutoff>=sampleFreq/2)||(ALowPass_HighCutoff==0)
0067 maskLowPass(:,:,:,:)=1;
0068 elseif (ALowPass_HighCutoff>0)&&(ALowPass_HighCutoff< freqPrecision)
0069 maskLowPass(:,:,:,:)=0;
0070 else
0071
0072 idxLowPass_HighCutoff =round(ALowPass_HighCutoff *paddedLength *ASamplePeriod);
0073 idxLowPass_HighCutoff2 =paddedLength+2 -idxLowPass_HighCutoff;
0074
0075 maskLowPass(:,:,:,idxLowPass_HighCutoff+1:idxLowPass_HighCutoff2-1)=0;
0076
0077 end
0078
0079
0080 idxHighPass_LowCutoff =round(AHighPass_LowCutoff *paddedLength *ASamplePeriod);
0081
0082 if (AHighPass_LowCutoff < freqPrecision)
0083 maskHighPass(:,:,:,:)=1;
0084 elseif (AHighPass_LowCutoff >= sampleFreq/2)
0085 maskHighPass(:,:,:,:)=0;
0086 else
0087
0088 idxHighPass_LowCutoff =round(AHighPass_LowCutoff *paddedLength *ASamplePeriod);
0089 idxHighPass_LowCutoff2 =paddedLength+2 -idxHighPass_LowCutoff;
0090 maskHighPass(:,:,:,1:idxHighPass_LowCutoff-1)=0;
0091
0092 maskHighPass(:,:,:,idxHighPass_LowCutoff2+1:paddedLength)=0;
0093 end
0094
0095
0096
0097 Save1stDimPieces(theTempDatasetDir, maskLowPass, 'fmLow_');
0098 Save1stDimPieces(theTempDatasetDir, maskHighPass, 'fmHigh_');
0099 clear maskLowPass maskHighPass;
0100
0101
0102 if rest_misc( 'GetMatlabVersion')>=7.3
0103 fftw('dwisdom');
0104 end
0105 fprintf('\n\t ALFF computing.\tWait...');
0106 NumPieces_Dim1 =4;
0107 NumComputingCount =floor(nDim1/NumPieces_Dim1);
0108 if NumComputingCount< (nDim1/NumPieces_Dim1),
0109 NumComputingCount =NumComputingCount +1;
0110 else
0111 end
0112 for x=1:(NumComputingCount),
0113
0114 rest_waitbar(x/(floor(nDim1/NumPieces_Dim1) +1), ...
0115 'Computing ALFF. Please wait...', ...
0116 'REST working','Child','NeedCancelBtn');
0117
0118
0119
0120 theFilename =fullfile(theTempDatasetDir, sprintf('dim1_%.8d', x));
0121 theDim1Volume4D =Load1stDimVolume(theFilename);
0122 theDim1Volume4D =double(theDim1Volume4D);
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 for xx=1:size(theDim1Volume4D,1),
0142 oneAxialSlice =double(theDim1Volume4D(xx, :, :, :));
0143 oneAxialSlice =reshape(oneAxialSlice, 1*nDim2*nDim3, nDim4)';
0144 oneAxialSlice =detrend(oneAxialSlice);
0145 oneAxialSlice =reshape(oneAxialSlice', 1,nDim2,nDim3, nDim4);
0146 theDim1Volume4D(xx, :, :, :) =(oneAxialSlice);
0147 end;
0148
0149 theDim1Volume4D =cat(4,theDim1Volume4D,zeros(size(theDim1Volume4D,1),nDim2,nDim3,paddedLength -sampleLength));
0150
0151
0152 theDim1Volume4D =fft(theDim1Volume4D, [], 4);
0153
0154 theFilename =fullfile(theTempDatasetDir, sprintf('fmLow_%.8d', x));
0155 theDim1FilterMask4D =Load1stDimVolume(theFilename);
0156
0157 theDim1Volume4D(~theDim1FilterMask4D)=0;
0158
0159
0160 theFilename =fullfile(theTempDatasetDir, sprintf('fmHigh_%.8d', x));
0161 theDim1FilterMask4D =Load1stDimVolume(theFilename);
0162
0163 theDim1Volume4D(~theDim1FilterMask4D)=0;
0164
0165
0166 theDim1Volume4D =theDim1Volume4D(:, :, :, 2:(paddedLength/2+1));
0167 theDim1Volume4D =abs(theDim1Volume4D);
0168
0169 theDim1Volume4D =2*(theDim1Volume4D .* theDim1Volume4D) /sampleLength;
0170 theDim1Volume4D(:,:,:, end) =theDim1Volume4D(:,:,:, end) /2;
0171
0172
0173
0174 theDim1Volume4D =sqrt(theDim1Volume4D);
0175
0176 theDim1Volume4D =sum(theDim1Volume4D,4);
0177 theDim1Volume4D =theDim1Volume4D /(idxLowPass_HighCutoff -idxHighPass_LowCutoff +1);
0178
0179
0180 theFilename =fullfile(theTempDatasetDir, sprintf('result_%.8d', x));
0181 save(theFilename, 'theDim1Volume4D');
0182 fprintf('.');
0183 end
0184 clear theDim1Volume4D theTrend_Intercept theTrend_Slope theDim1FilterMask4D oneAxialSlice;
0185
0186
0187 fprintf('\n\t ReConstructing 3D Dataset ALFF.\tWait...');
0188 theDataset3D=zeros(nDim1, nDim2, nDim3);
0189 for x=1:(NumComputingCount)
0190 rest_waitbar(x/(floor(nDim1/NumPieces_Dim1)+1), ...
0191 'ALFF 3D Brain reconstructing. Please wait...', ...
0192 'REST working','Child','NeedCancelBtn');
0193
0194 theFilename =fullfile(theTempDatasetDir, sprintf('result_%.8d', x));
0195
0196 if x~=(floor(nDim1/NumPieces_Dim1)+1)
0197 theDataset3D(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1),:,:)=Load1stDimVolume(theFilename);
0198 else
0199 theDataset3D(((x-1)*NumPieces_Dim1+1):end,:,:)=Load1stDimVolume(theFilename);
0200 end
0201 fprintf('.');
0202 end
0203
0204
0205 fprintf('\n\t Saving ALFF map.\tWait...');
0206 rest_writefile(single(theDataset3D), ...
0207 AResultFilename, ...
0208 isize,vsize,Header, 'single');
0209
0210 theElapsedTime =cputime - theElapsedTime;
0211 fprintf('\n\t ALFF compution over, elapsed time: %g seconds.\n', theElapsedTime);
0212
0213
0214 ans=rmdir(theTempDatasetDir, 's');
0215
0216
0217
0218 function Save1stDimPieces(ATempDir, A4DVolume, AFilenamePrefix)
0219 NumPieces_Dim1 =4;
0220 NumComputingCount =floor(size(A4DVolume,1)/NumPieces_Dim1);
0221 if NumComputingCount< (size(A4DVolume,1)/NumPieces_Dim1),
0222 NumComputingCount =NumComputingCount +1;
0223 else
0224 end
0225 for x = 1:(NumComputingCount),
0226
0227 rest_waitbar(x/(floor(size(A4DVolume,1)/NumPieces_Dim1)+1), ...
0228 'Cut one Big 3D+time Dataset into pieces of 3D+time Dataset Before ALFF. Please wait...', ...
0229 'REST working','Child','NeedCancelBtn');
0230
0231 theFilename =fullfile(ATempDir, sprintf('%s%.8d',AFilenamePrefix, x));
0232 if x~=(floor(size(A4DVolume,1)/NumPieces_Dim1)+1)
0233 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1), :,:,:);
0234 else
0235 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):end, :,:,:);
0236 end
0237 save(theFilename, 'the1stDim');
0238 end
0239
0240
0241 function Result=Load1stDimVolume(AFilename)
0242 Result =load(AFilename);
0243 theFieldnames=fieldnames(Result);
0244
0245 Result = Result.(theFieldnames{1});