1 | function filtered_data_out = fmri_filter(data,TR,varargin) |
---|
2 | |
---|
3 | % Defaults |
---|
4 | hipass = []; |
---|
5 | lowpass = []; |
---|
6 | detrending = false; |
---|
7 | reserve_mean = true; |
---|
8 | out_file = ''; |
---|
9 | |
---|
10 | % Parse varargin |
---|
11 | for ii=1:2:length(varargin) |
---|
12 | param = varargin{ii}; |
---|
13 | value = varargin{ii+1}; |
---|
14 | switch lower(param) |
---|
15 | case 'hipass' |
---|
16 | hipass = value; |
---|
17 | case 'lowpass' |
---|
18 | lowpass = value; |
---|
19 | case 'detrending' |
---|
20 | if strcmpi(value,'on') |
---|
21 | detrending = true; |
---|
22 | end |
---|
23 | case 'reservemean' |
---|
24 | if strcmpi(value,'off') |
---|
25 | reserve_mean = false; |
---|
26 | end |
---|
27 | case 'filename' |
---|
28 | out_file = value; |
---|
29 | otherwise |
---|
30 | error('Unknown parameter "%s"',param) |
---|
31 | end |
---|
32 | end |
---|
33 | |
---|
34 | if isstruct(data) |
---|
35 | data = data.FTDATA; |
---|
36 | elseif ischar(data) |
---|
37 | data = an2_data_read(data); |
---|
38 | data = data.FTDATA; |
---|
39 | end |
---|
40 | filtered_data = double(data); |
---|
41 | |
---|
42 | if isempty(hipass) && isempty(lowpass) && ~detrend |
---|
43 | return |
---|
44 | end |
---|
45 | |
---|
46 | % Construct hi-pass filter |
---|
47 | if ~isempty(hipass) |
---|
48 | [B_hipass,A_hipass]=butter(4,hipass/((1/TR)/2),'high'); |
---|
49 | end |
---|
50 | |
---|
51 | % Construct low-pass filter |
---|
52 | if ~isempty(lowpass) |
---|
53 | [B_lowpass,A_lowpass]=butter(15,lowpass/((1/TR)/2),'low'); |
---|
54 | end |
---|
55 | |
---|
56 | |
---|
57 | % Filter image data ----------------------------- |
---|
58 | if ~isempty(lowpass) || ~isempty(hipass) || detrending |
---|
59 | |
---|
60 | do_lp = true; |
---|
61 | do_hp = true; |
---|
62 | if isempty(lowpass) |
---|
63 | do_lp = false; |
---|
64 | end |
---|
65 | |
---|
66 | if isempty(hipass) |
---|
67 | do_hp = false; |
---|
68 | end |
---|
69 | |
---|
70 | % Waitbar |
---|
71 | wbh=an2_wbar(0,'Filtering image data...'); |
---|
72 | |
---|
73 | counter=1; |
---|
74 | sz = ones(1,4); |
---|
75 | sz(1:length(size(data)))=size(data); |
---|
76 | nVox = prod(sz(1:3)); |
---|
77 | for ii=1:size(data,1) |
---|
78 | for kk=1:size(data,2) |
---|
79 | for zz=1:size(data,3) |
---|
80 | tmp = squeeze(double(data(ii,kk,zz,:))); |
---|
81 | mean_tmp = mean(tmp); |
---|
82 | if do_hp |
---|
83 | tmp = filtfilt(B_hipass,A_hipass,tmp); |
---|
84 | if reserve_mean |
---|
85 | tmp = tmp+mean_tmp; |
---|
86 | end |
---|
87 | end |
---|
88 | if do_lp |
---|
89 | tmp = filtfilt(B_lowpass,A_lowpass,tmp); |
---|
90 | end |
---|
91 | if detrending |
---|
92 | tmp = detrend(tmp,'linear'); |
---|
93 | if reserve_mean |
---|
94 | tmp = tmp+mean_tmp; |
---|
95 | end |
---|
96 | end |
---|
97 | filtered_data(ii,kk,zz,:) = tmp; |
---|
98 | an2_wbar(counter/nVox,wbh) |
---|
99 | counter=counter+1; |
---|
100 | end |
---|
101 | end |
---|
102 | end |
---|
103 | close(wbh) |
---|
104 | end |
---|
105 | |
---|
106 | if nargout>0 |
---|
107 | filtered_data_out = filtered_data; |
---|
108 | end |
---|
109 | |
---|
110 | % Write output to file |
---|
111 | if ~isempty(out_file) |
---|
112 | an2_write_nifti(filtered_data,out_file); |
---|
113 | end |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | |
---|