0001 function [] = rest_bandpass(ADataDir, ...
0002 ASamplePeriod, ALowPass_HighCutoff, AHighPass_LowCutoff, ...
0003 ARetrend, ...
0004 AMaskFilename)
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 if nargin~=6, help('rest_bandpass');error(' Error using ==> rest_bandpass. 6 arguments wanted.'); end
0035
0036 tic;
0037 fprintf('\nIdeal rectangular filter:\t"%s"', ADataDir);
0038 [AllVolume,vsize,theImgFileList, Header] =rest_to4d(ADataDir);
0039
0040 thePrecision ='double';
0041 tmpData =double(squeeze(AllVolume(:, :, :, round(size(AllVolume, 4)/2))));
0042 if mean(abs(tmpData(0~=tmpData)))<100,
0043 thePrecision ='double';
0044 end
0045
0046 nDim1 = size(AllVolume,1); nDim2 = size(AllVolume,2); nDim3 = size(AllVolume,3);
0047 isize = [nDim1 nDim2 nDim3];
0048
0049
0050
0051 theTempDatasetDirName =sprintf('BandPass_%d', fix((1e4) *rem(now, 1) ));
0052 theTempDatasetDir =[tempdir theTempDatasetDirName] ;
0053 ans=rmdir(theTempDatasetDir, 's');
0054 mkdir(tempdir, theTempDatasetDirName);
0055
0056 Save1stDimPieces(theTempDatasetDir, AllVolume, 'dim1_');
0057 clear AllVolume;
0058
0059
0060 fprintf('\n\t Load mask "%s".', AMaskFilename);
0061 mask=rest_loadmask(nDim1, nDim2, nDim3, AMaskFilename);
0062
0063 fprintf('\n\t Build band pass filtered mask.\tWait...');
0064 sampleFreq = 1/ASamplePeriod;
0065 sampleLength = size(theImgFileList,1);
0066 paddedLength = rest_nextpow2_one35(sampleLength);
0067
0068 freqPrecision= sampleFreq/paddedLength;
0069 if rest_misc( 'GetMatlabVersion')<7.3
0070 warning off MATLAB:conversionToLogical;
0071 end
0072 mask =logical(mask);
0073 if rest_misc( 'GetMatlabVersion')<7.3
0074 warning on MATLAB:conversionToLogical;
0075 end
0076 maskLowPass = repmat(mask, [1, 1, 1, paddedLength]);
0077 maskHighPass= maskLowPass;
0078 clear mask;
0079
0080 if (ALowPass_HighCutoff>=(sampleFreq/2))||(ALowPass_HighCutoff==0)
0081 maskLowPass(:,:,:,:)=1;
0082 elseif (ALowPass_HighCutoff>0)&&(ALowPass_HighCutoff< freqPrecision)
0083 maskLowPass(:,:,:,:)=0;
0084 else
0085
0086 idxCutoff =round(ALowPass_HighCutoff *paddedLength *ASamplePeriod);
0087 idxCutoff2 =paddedLength+2 -idxCutoff;
0088
0089 maskLowPass(:,:,:,idxCutoff+1:idxCutoff2-1)=0;
0090
0091 end
0092
0093 if (AHighPass_LowCutoff < freqPrecision)
0094 maskHighPass(:,:,:,:)=1;
0095 elseif (AHighPass_LowCutoff >= (sampleFreq/2))
0096 maskHighPass(:,:,:,:)=0;
0097 else
0098
0099 idxCutoff =round(AHighPass_LowCutoff *paddedLength *ASamplePeriod);
0100 idxCutoff2 =paddedLength+2 -idxCutoff;
0101 maskHighPass(:,:,:,1:idxCutoff-1)=0;
0102
0103 maskHighPass(:,:,:,idxCutoff2+1:paddedLength)=0;
0104 end
0105
0106
0107
0108 Save1stDimPieces(theTempDatasetDir, maskLowPass, 'fmLow_');
0109 Save1stDimPieces(theTempDatasetDir, maskHighPass, 'fmHigh_');
0110 clear maskLowPass maskHighPass;
0111
0112
0113 if rest_misc( 'GetMatlabVersion')>=7.3
0114 fftw('dwisdom');
0115 else
0116 end
0117 fprintf('\n\t Band Pass Filter working.\tWait...');
0118 NumPieces_Dim1 =4;
0119 NumComputingCount =floor(nDim1/NumPieces_Dim1);
0120 if NumComputingCount< (nDim1/NumPieces_Dim1),
0121 NumComputingCount =NumComputingCount +1;
0122 else
0123 end
0124 for x=1:(NumComputingCount),
0125 rest_waitbar(x/(floor(nDim1/NumPieces_Dim1) +1), ...
0126 'wait...', ...
0127 'Filter is working','Child','NeedCancelBtn');
0128
0129
0130
0131 theFilename =fullfile(theTempDatasetDir, sprintf('dim1_%.8d', x));
0132 theDim1Volume4D =Load1stDimVolume(theFilename);
0133 theDim1Volume4D =double(theDim1Volume4D);
0134
0135
0136 theTrend_Intercept=theDim1Volume4D(:,:,:, 1);
0137 theTrend_Slope= (theDim1Volume4D(:,:,:, end) -theTrend_Intercept) /double(sampleLength-1);
0138 for y=1:sampleLength
0139
0140 theDim1Volume4D(:,:,:, y)=theDim1Volume4D(:,:,:, y) -(theTrend_Intercept + y*theTrend_Slope);
0141 end
0142 theDim1Volume4D =cat(4,theDim1Volume4D,zeros(size(theDim1Volume4D,1),nDim2,nDim3,paddedLength -sampleLength));
0143
0144
0145 theDim1Volume4D =fft(theDim1Volume4D, [], 4);
0146
0147 theFilename =fullfile(theTempDatasetDir, sprintf('fmLow_%.8d', x));
0148 theDim1FilterMask4D =Load1stDimVolume(theFilename);
0149
0150 theDim1Volume4D(~theDim1FilterMask4D)=0;
0151
0152
0153 theFilename =fullfile(theTempDatasetDir, sprintf('fmHigh_%.8d', x));
0154 theDim1FilterMask4D =Load1stDimVolume(theFilename);
0155
0156 theDim1Volume4D(~theDim1FilterMask4D)=0;
0157
0158
0159 theDim1Volume4D =ifft(theDim1Volume4D, [], 4);
0160 theDim1Volume4D =theDim1Volume4D(:,:,:, 1:sampleLength);
0161
0162 if strcmpi(ARetrend, 'Yes')
0163 for y=1:sampleLength
0164
0165 theDim1Volume4D(:,:,:, y)=theDim1Volume4D(:,:,:, y) +double(theTrend_Intercept+y*theTrend_Slope);
0166 end
0167 end
0168
0169
0170
0171 theFilename =fullfile(theTempDatasetDir, sprintf('result_%.8d', x));
0172 save(theFilename, 'theDim1Volume4D');
0173 fprintf('.');
0174 end
0175 clear theDim1Volume4D theTrend_Intercept theTrend_Slope theDim1FilterMask4D;
0176
0177
0178 fprintf('\n\t ReConstructing 3D+time Dataset.\tWait...');
0179 theDataset4D=zeros(nDim1, nDim2, nDim3, sampleLength);
0180 for x=1:(NumComputingCount)
0181 rest_waitbar(x/(floor(nDim1/NumPieces_Dim1)+1), ...
0182 'wait...', ...
0183 'After filter','Child','NeedCancelBtn');
0184
0185 theFilename =fullfile(theTempDatasetDir, sprintf('result_%.8d', x));
0186
0187 if x~=(floor(nDim1/NumPieces_Dim1)+1)
0188 theDataset4D(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1),:,:,:)=Load1stDimVolume(theFilename);
0189 else
0190 theDataset4D(((x-1)*NumPieces_Dim1+1):end,:,:,:)=Load1stDimVolume(theFilename);
0191 end
0192 fprintf('.');
0193 end
0194
0195
0196 fprintf('\n\t Saving filtered images.\tWait...');
0197 if strcmp(ADataDir(end),filesep)==1
0198 ADataDir=ADataDir(1:end-1);
0199 end
0200 theResultOutputDir =sprintf('%s_filtered',ADataDir);
0201 ans=rmdir(theResultOutputDir, 's');
0202 [theParentDir,theOutputDirName]=fileparts(theResultOutputDir);
0203 mkdir(theParentDir, theOutputDirName);
0204
0205
0206 for x=1:sampleLength
0207 rest_waitbar(x/sampleLength, ...
0208 sprintf('Saving to {hdr/img} pair files\nwait...'), ...
0209 'Filter Over','Child','NeedCancelBtn');
0210 rest_writefile(single(theDataset4D(:, :, :, x)), ...
0211 sprintf('%s_filtered%s%.8d', ADataDir, filesep,x), ...
0212 isize,vsize,Header, 'single');
0213 if (mod(x,5)==0)
0214 fprintf('.');
0215 end
0216 end
0217 fprintf('\n\t Band pass filter over.\n\t');
0218 toc;
0219
0220
0221 ans=rmdir(theTempDatasetDir, 's');
0222
0223
0224 function Save1stDimPieces(ATempDir, A4DVolume, AFilenamePrefix)
0225 NumPieces_Dim1 =4;
0226 NumComputingCount =floor(size(A4DVolume,1)/NumPieces_Dim1);
0227 if NumComputingCount< (size(A4DVolume,1)/NumPieces_Dim1),
0228 NumComputingCount =NumComputingCount +1;
0229 else
0230 end
0231 for x = 1:(NumComputingCount)
0232 rest_waitbar(x/(floor(size(A4DVolume,1)/NumPieces_Dim1)+1), ...
0233 'wait...', ...
0234 'Before Filter','Child','NeedCancelBtn');
0235 theFilename =fullfile(ATempDir, sprintf('%s%.8d',AFilenamePrefix, x));
0236 if x~=(floor(size(A4DVolume,1)/NumPieces_Dim1)+1)
0237 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1), :,:,:);
0238 else
0239 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):end, :,:,:);
0240 end
0241 save(theFilename, 'the1stDim');
0242 end
0243
0244
0245 function Result=Load1stDimVolume(AFilename)
0246 Result =load(AFilename);
0247 theFieldnames=fieldnames(Result);
0248
0249 Result = Result.(theFieldnames{1});
0250