applies filter in design to data FORMAT Y = apply_filter(D, Y, flags) D - design, which includes a filter Y - data to filter (2D matrix or marsy data object) flags - string specifying one option, or cell array specifying more than one option, or struct with fields specifying options. Values for strings, cell contents or field names are 'no_whitening' - specifies not to use whitening matrix even if present in model 'sessions' - when used as struct field, value for field specifies sessions to apply filter for. The data size must match the length of the included sessions. Returns Y - filtered data $Id$
0001 function Y = apply_filter(D, Y, flags) 0002 % applies filter in design to data 0003 % FORMAT Y = apply_filter(D, Y, flags) 0004 % 0005 % D - design, which includes a filter 0006 % Y - data to filter (2D matrix or marsy data object) 0007 % flags - string specifying one option, or cell array specifying more 0008 % than one option, or struct with fields specifying options. 0009 % Values for strings, cell contents or field names are 0010 % 'no_whitening' - specifies not to use whitening matrix even 0011 % if present in model 0012 % 'sessions' - when used as struct field, value for field 0013 % specifies sessions to apply filter for. The 0014 % data size must match the length of the 0015 % included sessions. 0016 % 0017 % Returns 0018 % Y - filtered data 0019 % 0020 % $Id$ 0021 0022 if nargin < 2 0023 error('Need data to filter'); 0024 end 0025 if nargin < 3 0026 flags = []; 0027 end 0028 if ~isempty(flags) 0029 if ischar(flags), flags = {flags}; end 0030 if iscell(flags) 0031 flags = cell2struct(repmat({''}, size(flags)), flags, 1); 0032 end 0033 end 0034 if ~is_fmri(D) 0035 return 0036 end 0037 if ~has_filter(D) 0038 error('This FMRI design does not contain a filter'); 0039 end 0040 0041 SPM = des_struct(D); 0042 K = SPM.xX.K; 0043 if ~has_whitener(D) | isfield(flags, 'no_whitening') 0044 W = eye(n_time_points(D)); 0045 else 0046 W = SPM.xX.W; 0047 end 0048 0049 % Filtering from subset of sessions 0050 if isfield(flags, 'sessions') 0051 ss = flags.sessions; 0052 if ~isempty(ss) 0053 blk_rows = block_rows(D); 0054 if any(ss < 1 | ss > length(blk_rows)) 0055 error('Sessions appear to be out of range'); 0056 end 0057 K = K(ss); 0058 K.row = blk_rows{ss} - blk_rows{ss}(1) + 1; 0059 W = W(blk_rows{ss}, blk_rows{ss}); 0060 end 0061 end 0062 0063 if isa(Y, 'marsy') % marsy object 0064 rd = region_data(Y); 0065 for r = 1:length(rd) 0066 rd{r} = pr_spm_filter(K, W*rd{r}); 0067 end 0068 Y = region_data(Y, [], rd); 0069 else % 2D matrix 0070 Y = pr_spm_filter(K, W*Y); 0071 end