function rbrain = brain_preprocess(input_file, output_file); 
%BRAIN_PREPROCESS removes the linear time trend per voxel. 
%
%RBRAIN = BRAIN_PREPROCESS(INPUT_FILE, OUTPUT_FILE)
%
%INPUT_FILE: the name of a 4D fMRI image file with multiple  frames
%in ANALYZE (.img)  format.  
%
%OUTPUT_FILE: the name of the file with linear time trend removed. 

[brain] 	=  tfiReadAnalyze(input_file);

isbrain = squeeze((brain(:,:,:,1)))>300;
%residual of time trend removal
rbrain = zeros(size(brain));
for h=2: size(brain,3)-1 %exclude first and last slice
   slice = squeeze(brain(:,:,h,:));
    isslice = squeeze(isbrain(:,:,h));
    [ h  sum(sum(isslice))]
    rslice = mysmooth(slice,isslice);
    rbrain(:,:,h,:) = rslice;
end


save(output_file,'rbrain')

 
dm = fmris_read_image(input_file,1:size(brain,3));
dm.data = rbrain;
dm.file_name = strcat(output_file,'.img');
[temp]=fmris_write_analyze(dm);

