function filter(ts,n,d,varargin) 
%FILTER Filter data in time series object
%
%   TS2=FILTER(TS1,N,D,VARARGIN) applies a filter with transfer function 
%   n(z^-1)/d(z^-1) to the data in the time series object TS1 and returns
%   the filtered time series object TS2. Note that the
%   time series must be uniformly sampled.
%
%   See also TSDATA.TIMESERIES/TIMESERIES, TSDATA.TIMESERIES/IDEALFILTER

%   Author(s): James G. Owen
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:35:01 $

if nargin>=4  && ~isempty(varargin{1})
    colinds = varargin{1};
else
    colinds = 1:size(ts.Data,2);
end

%% Remove any NaNs
if any(isnan(ts.Data(:)))
    ts.resample(ts.Time);
end
if any(isnan(ts.Data(:)))
    error('timeseries:filter:allnans',...
        'Cannot remove all NaNs from the timeseries')
end

%% Filter
ts.Data(:,colinds) = filter(n,d,ts.Data(:,colinds));

