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