source: misclib/fmri_smooth.m @ 98

Last change on this file since 98 was 98, checked in by tjniskan, 10 years ago
  • 3D ROIs can now be loaded on 4D datasets if the first three

dimensions match.

  • Corrected a small bug in the time series viewing in Aedes.
  • Added Fourier domain smoothing to misclib/fmri_smooth.m
  • Removed an unnecessary matrix replication from

plugins/fmri_plugins/basic_fmri_analysis.m

M misclib/fmri_smooth.m
M aedes.m
M plugins/fmri_plugins/basic_fmri_analysis.m
M aedes_revision.m

File size: 2.2 KB
Line 
1function smooth_data_out = fmri_smooth(data,fwhm_sz,voxsize,out_file)
2
3if nargin==4
4  writeSmoothedData = true;
5else
6  writeSmoothedData = false;
7end
8
9if nargin<3 || isempty(voxsize)
10  fwhmInPixels = true;
11else
12  fwhmInPixels = false;
13end
14
15% Check if data is an Aedes structure
16if isstruct(data)
17  data = data.FTDATA;
18elseif ischar(data)
19  data = aedes_data_read(data);
20  data = data.FTDATA;
21end
22
23UseImFilter = true;
24
25smooth_data = zeros(size(data));
26
27% Check data dimensions
28if any(ndims(data)==[4,3]) && size(data,3)~=1
29  use3Dkernel = true;
30else
31  use3Dkernel = false;
32end
33
34% Calculate standard deviations using FWHM
35if fwhmInPixels
36   stds = fwhm_sz/sqrt(8*log(2));
37else
38  stds = (fwhm_sz/sqrt(8*log(2)))./voxsize;
39end
40
41% Calculate kernel size using STDs
42kernel_sz = round(6*stds);
43
44% Construct the smoothing kernel
45if use3Dkernel
46  [x,y,z] = meshgrid(-kernel_sz(2):kernel_sz(2),...
47        -kernel_sz(1):kernel_sz(1),...
48        -kernel_sz(3):kernel_sz(3));
49  s_kernel = exp(-(x).^2/(2*(stds(1)).^2)...
50        -(y).^2/(2*(stds(2)).^2)...
51        -(z).^2/(2*(stds(3)).^2));
52  s_kernel = s_kernel/sum(s_kernel(:));
53else
54  [x,y] = meshgrid(-kernel_sz(2):kernel_sz(2),...
55        -kernel_sz(1):kernel_sz(1));
56  s_kernel = exp(-(x).^2/(2*(stds(1)).^2)...
57        -(y).^2/(2*(stds(2)).^2));
58  s_kernel = s_kernel/sum(s_kernel(:));
59end
60
61% Smooth the image data -----------------------
62nVols = size(data,4);
63if ~UseImFilter
64  ind_hi=ceil(size(data(:,:,:,1))/2)+ceil(size(s_kernel)/2);
65  ind_lo=ind_hi-(size(s_kernel)-1);
66  ind_lo(ind_lo<1)=1;
67  tmp_kernel = zeros(size(data(:,:,:,1)));
68  tmp_kernel(ind_lo(1):ind_hi(1),ind_lo(2):ind_hi(2),...
69    ind_lo(3):ind_hi(3))=s_kernel;
70end
71for ii=1:nVols
72  if ii==1
73    fprintf(1,'Smoothing volume %d/%d',ii,nVols);
74    bsz = length(sprintf('%d/%d',ii,nVols));
75  else
76    fprintf(1,repmat('\b',1,bsz));
77    bsz = length(sprintf('%d/%d',ii,nVols));
78    fprintf(1,'%d/%d',ii,nVols);
79  end
80  if UseImFilter
81    tmp_data = data(:,:,:,ii);
82    smooth_data(:,:,:,ii) = imfilter(tmp_data,s_kernel);
83  else
84    tmp_data = double(data(:,:,:,ii));
85    smooth_data(:,:,:,ii) = fftshift(ifftn(fftn(tmp_data).*fftn(tmp_kernel)));
86  end
87end
88fprintf(1,'\n');
89
90if nargout>0
91  smooth_data_out = smooth_data;
92end
93
94% Write output files
95if writeSmoothedData
96  aedes_write_nifti(smooth_data,out_file)
97end
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008