eeg_filter - apply a butterworth polynomial filter Usage : EEGfiltered = eeg_filter(EEGinput,sample_freq,lcf,hcf,order) - input arguments EEGinput : eeg data - M samples x N channels x P epochs sample_freq : sampling frequency lcf : low cutoff frequency (highpass, default 0.01) hcf : high cutoff frequency (lowpass, default 40) order : butterworth polynomial order (default 2) - output argument EEGfiltered : filtered EEGinput; This function uses the filtfilt and butter functions of the matlab signal processing toolbox, using the bandpass option. The filtfilt function baselines on the last point of the timeseries, so it is necessary to call *eeg_baseline* after filtering.
0001 function EEGfiltered = eeg_filter(EEGinput,sample_freq,lcf,hcf,order); 0002 0003 % eeg_filter - apply a butterworth polynomial filter 0004 % 0005 % Usage : EEGfiltered = eeg_filter(EEGinput,sample_freq,lcf,hcf,order) 0006 % 0007 % - input arguments 0008 % EEGinput : eeg data - M samples x N channels x P epochs 0009 % sample_freq : sampling frequency 0010 % lcf : low cutoff frequency (highpass, default 0.01) 0011 % hcf : high cutoff frequency (lowpass, default 40) 0012 % order : butterworth polynomial order (default 2) 0013 % 0014 % - output argument 0015 % EEGfiltered : filtered EEGinput; 0016 % 0017 % This function uses the filtfilt and butter functions of the matlab 0018 % signal processing toolbox, using the bandpass option. The filtfilt 0019 % function baselines on the last point of the timeseries, so it is 0020 % necessary to call *eeg_baseline* after filtering. 0021 % 0022 0023 % Copyright (C) 2004 Darren L. Weber 0024 % 0025 % This program is free software; you can redistribute it and/or 0026 % modify it under the terms of the GNU General Public License 0027 % as published by the Free Software Foundation; either version 2 0028 % of the License, or (at your option) any later version. 0029 % 0030 % This program is distributed in the hope that it will be useful, 0031 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0032 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0033 % GNU General Public License for more details. 0034 % 0035 % You should have received a copy of the GNU General Public License 0036 % along with this program; if not, write to the Free Software 0037 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 0038 % 0039 0040 % $Revision: 1.4 $ $Date: 2005/05/18 23:05:13 $ 0041 % Created: 10/2004, copyright 2004 Darren.Weber_at_radiology.ucsf.edu 0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0043 0044 version = '[$Revision: 1.4 $]'; 0045 fprintf('\nEEG_FILTER [v%s]\n',version(12:16)); 0046 0047 if ~exist('sample_freq','var') || isempty(sample_freq), 0048 error('no sample_freq defined'); 0049 end 0050 0051 % low cutoff frequency (default 2) 0052 if ~exist('lcf','var') || isempty(lcf), 0053 lcf = 0.01; 0054 end 0055 % high cutoff frequency (default 20) 0056 if ~exist('hcf','var') || isempty(hcf), 0057 hcf = 40; 0058 end 0059 % butter filter order (default 2) 0060 if ~exist('order','var') || isempty(order), 0061 order = 2; 0062 end 0063 0064 if hcf > (sample_freq/2), 0065 warning('hcf > sample_freq/2, setting hcf = sample_freq/2'); 0066 hcf = sample_freq / 2; 0067 end 0068 if lcf <= 0 || lcf > (sample_freq/2) || lcf >= hcf, 0069 warning('lcf value is <=0 or >(sample_freq/2) or >=hcf, setting lcf = 2'); 0070 lcf = 2; 0071 end 0072 0073 % call the butter function of the signal processing toolbox 0074 cf1 = lcf/(sample_freq/2); 0075 cf2 = hcf/(sample_freq/2); 0076 [B,A] = butter(order,[cf1 cf2]); 0077 0078 epochs = size(EEGinput,3); 0079 for epoch = 1:epochs, 0080 EEGfiltered(:,:,epoch) = filtfilt(B,A,EEGinput(:,:,epoch)); 0081 end 0082 0083 return