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 = an2_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 | for ii=1:size(data,4) |
---|

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

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

63 | end |
---|

64 | |
---|

65 | if nargout>0 |
---|

66 | smooth_data_out = smooth_data; |
---|

67 | end |
---|

68 | |
---|

69 | % Write output files |
---|

70 | if writeSmoothedData |
---|

71 | an2_write_nifti(smooth_data,out_file) |
---|

72 | end |
---|