function filter(h,varargin)

%% Create transaction
T = tsguis.transaction;
recorder = tsguis.recorder;

%% Get filter type
if nargin==2 && strcmpi(varargin{1},'pass')
    type = 'pass';
else
    type = 'notch';
end

%% Filter in selected intervals        
for k=1:length(h.Waves)
    if ~isempty(h.Waves(k).View.SelectedInterval)
       timeunitconv = tsunitconv(h.Waves(k).DataSrc.Timeseries.TimeInfo.Units,...
           sprintf('%ss',h.AxesGrid.Xunits(5:end)));
       
       idealfilter(h.Waves(k).DataSrc.Timeseries,...
                                 h.Waves(k).View.SelectedInterval/timeunitconv,...
                                 type);
           
       if strcmp(recorder.Recording,'on')
             freqint = h.Waves(k).View.SelectedInterval/timeunitconv;
             thists = h.Waves(k).DataSrc.Timeseries;
             T.addbuffer('%% Filtering');
             if size(freqint,1)>1
                 T.addbuffer(['freqint = reshape([' num2str(freqint(:)') '],[' ...
                     num2str(size(freqint)) ']);']);
             else
                 T.addbuffer(['freqint = [' num2str(freqint) '];']);
             end
             T.addbuffer(sprintf('idealfilter(%s,freqint,%s)',...
                thists.Name,['''' type '''']),thists);
       end     
       
       %% Clear selection
       h.Waves(k).View.SelectedInterval = [];
       h.draw
    end
end

%% Store transaction
T.commit;
recorder = tsguis.recorder;
recorder.pushundo(T);
       
       