-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctf_filter.m
86 lines (71 loc) · 2.52 KB
/
ctf_filter.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function ctf = ctf_filter(ctf,lcf,hcf,order);
% ctf_filter - apply a butterworth polynomial filter
%
% Usage : ctf = ctf_filter(ctf,lcf,hcf,order);
%
% - input arguments
% ctf : meg data file returned by ctf_read
% lcf : low cutoff frequency (default 0.01)
% hcf : high cutoff frequency (default 40)
% order : butterworth polynomial order (default 2)
%
% - output argument
% ctf.data : filtered replacement of ctf.data
%
% This function calls the butter and filtfilt functions of the matlab
% signal processing toolbox. The filtfilt values are baselined with
% ctf_baseline, so the baseline offsets can be slightly different from the
% input ctf.data.
%
% $Revision: 1.1 $ $Date: 2009-01-30 03:49:27 $
% Copyright (C) 2004 Darren L. Weber
%
% This program 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 2
% of the License, or (at your option) any later version.
%
% This program 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 this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
% Created: 05/2004, copyright 2004 Darren.Weber_at_radiology.ucsf.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% low cutoff frequency (default 2)
if ~exist('lcf','var') || isempty(lcf),
lcf = 0.01;
end
% high cutoff frequency (default 20)
if ~exist('hcf','var') || isempty(hcf),
hcf = 40;
end
% butter filter order (default 2)
if ~exist('order','var') || isempty(order),
order = 2;
end
sample_freq = ctf.setup.sample_rate;
if hcf > (sample_freq/2),
warning('hcf > sample_freq/2, setting hcf = sample_freq/2');
hcf = sample_freq / 2;
end
if lcf <= 0 || lcf > (sample_freq/2) || lcf >= hcf,
warning('lcf value is <=0 or >(sample_freq/2) or >=hcf, setting lcf = 2');
lcf = 2;
end
% design the Butterworth filter
cf1 = lcf/(sample_freq/2);
cf2 = hcf/(sample_freq/2);
[B,A] = butter(order,[cf1 cf2]);
% filter the data
% data should be N samples x M channels
for trial = 1:size(ctf.data,3)
data = ctf.data(:,:,trial);
ctf.data(:,:,trial) = filtfilt(B,A,data);
end
% now rebaseline the filtered data
ctf = ctf_baseline(ctf);
return