1 | function smooth_data_out = fmri_smooth(data,fwhm_sz,voxsize,out_file) |
---|

2 | |
---|

3 | if nargin==4 |
---|

4 | writeSmoothedData = true; |
---|

5 | else |
---|

6 | writeSmoothedData = false; |
---|

7 | end |
---|

8 | |
---|

9 | if nargin<3 || isempty(voxsize) |
---|

10 | fwhmInPixels = true; |
---|

11 | else |
---|

12 | fwhmInPixels = false; |
---|

13 | end |
---|

14 | |
---|

15 | % Check if data is an Aedes structure |
---|

16 | if isstruct(data) |
---|

17 | data = data.FTDATA; |
---|

18 | elseif ischar(data) |
---|

19 | data = aedes_data_read(data); |
---|

20 | data = data.FTDATA; |
---|

21 | end |
---|

22 | |
---|

23 | smooth_data = zeros(size(data)); |
---|

24 | |
---|

25 | % Check data dimensions |
---|

26 | if any(ndims(data)==[4,3]) && size(data,3)~=1 |
---|

27 | use3Dkernel = true; |
---|

28 | else |
---|

29 | use3Dkernel = false; |
---|

30 | end |
---|

31 | |
---|

32 | % Calculate standard deviations using FWHM |
---|

33 | if fwhmInPixels |
---|

34 | stds = fwhm_sz/sqrt(8*log(2)); |
---|

35 | else |
---|

36 | stds = (fwhm_sz/sqrt(8*log(2)))./voxsize; |
---|

37 | end |
---|

38 | |
---|

39 | % Calculate kernel size using STDs |
---|

40 | kernel_sz = round(6*stds); |
---|

41 | |
---|

42 | % Construct the smoothing kernel |
---|

43 | if use3Dkernel |
---|

44 | [x,y,z] = meshgrid(-kernel_sz(2):kernel_sz(2),... |
---|

45 | -kernel_sz(1):kernel_sz(1),... |
---|

46 | -kernel_sz(3):kernel_sz(3)); |
---|

47 | s_kernel = exp(-(x).^2/(2*(stds(1)).^2)... |
---|

48 | -(y).^2/(2*(stds(2)).^2)... |
---|

49 | -(z).^2/(2*(stds(3)).^2)); |
---|

50 | s_kernel = s_kernel/sum(s_kernel(:)); |
---|

51 | else |
---|

52 | [x,y] = meshgrid(-kernel_sz(2):kernel_sz(2),... |
---|

53 | -kernel_sz(1):kernel_sz(1)); |
---|

54 | s_kernel = exp(-(x).^2/(2*(stds(1)).^2)... |
---|

55 | -(y).^2/(2*(stds(2)).^2)); |
---|

56 | s_kernel = s_kernel/sum(s_kernel(:)); |
---|

57 | end |
---|

58 | |
---|

59 | % Smooth the image data ----------------------- |
---|

60 | nVols = size(data,4); |
---|

61 | for ii=1:nVols |
---|

62 | if ii==1 |
---|

63 | fprintf(1,'Smoothing volume %d/%d',ii,nVols); |
---|

64 | bsz = length(sprintf('%d/%d',ii,nVols)); |
---|

65 | else |
---|

66 | fprintf(1,repmat('\b',1,bsz)); |
---|

67 | bsz = length(sprintf('%d/%d',ii,nVols)); |
---|

68 | fprintf(1,'%d/%d',ii,nVols); |
---|

69 | end |
---|

70 | tmp_data = data(:,:,:,ii); |
---|

71 | smooth_data(:,:,:,ii) = imfilter(tmp_data,s_kernel); |
---|

72 | end |
---|

73 | fprintf(1,'\n'); |
---|

74 | |
---|

75 | if nargout>0 |
---|

76 | smooth_data_out = smooth_data; |
---|

77 | end |
---|

78 | |
---|

79 | % Write output files |
---|

80 | if writeSmoothedData |
---|

81 | aedes_write_nifti(smooth_data,out_file) |
---|

82 | end |
---|