-
Notifications
You must be signed in to change notification settings - Fork 709
/
ft_preproc_online_filter_apply.m
51 lines (46 loc) · 1.65 KB
/
ft_preproc_online_filter_apply.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [state, xf] = ft_preproc_online_filter_apply(state, x)
% FT_PREPROC_ONLINE_FILTER_APPLY passes a signal through the online filter and
% returns the updated filter model (delay states) and the filtered signal.
%
% Use as
% [state, dat] = ft_preproc_online_filter_apply(state, dat)
% where
% dat = Nchan x Ntime
% state = filter state, see FT_PREPROC_ONLINE_FILTER_INIT
%
% See also PREPROC
% Copyright (C) 2010, Stefan Klanke
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
[dimX, numX] = size(x);
if dimX > numX
% explicit algorithm - faster for many channels, 1 sample (=fMRI)
xf = zeros(size(x));
for k=1:numX;
z_old = state.z;
z0 = x(:,k) - z_old*state.A2;
xf(:,k) = z_old * state.B2 + z0*state.B1;
state.z(:,2:end) = z_old(:,1:end-1);
state.z(:,1) = z0;
end
else
% use built-in MATLAB stuff - faster for many samples, few channels
[xf, z] = filter(state.B, state.A, x, state.z',2);
state.z = z';
end