function idealfilter(ts,intervals,type) 

%% Applies an ideal filter of type "pass" or "notch" to the specified
%% frequency intervals. If nesessary the time series is resampled
%% to be uniform and NaNs are interpolated 

%% Intervals are an nx2 array of frequencies where n = number of intervals

%% If the time series is non-uniformply sampled or has NaN values
%% then resample
if isnan(ts.TimeInfo.Increment) || any(any(isnan(ts.Data)))
    time = ts.Time;
    tuniform = linspace(time(1),time(end),length(time));
    ts.resample(tuniform);
end

%% Detrend the ordinate data
m = mean(ts.Data);
data = detrend(ts.Data,0);

%% Find the fft
Ts = ts.timeInfo.Increment;
idata = fft(data);
fdata = (0:(size(data,1)-1))/(size(data,1)*Ts);
fdata(fdata>1/(2*Ts)) = 1/Ts - fdata(fdata>1/(2*Ts));

%% Null out excluded frequencies
if strcmpi(type,'pass')
   I = true(size(fdata));
else
   I = false(size(fdata));
end
for ct=1:size(intervals,1)
   if strcmpi(type,'pass')
       I = I & (fdata<=min(intervals(ct,:)) | fdata>max(intervals(ct,:)));   
   else
       I = I | (fdata>min(intervals(ct,:)) & fdata<=max(intervals(ct,:)));
   end
end
idata(I,:) = 0;

%% Reset the time series data
ts.Data = ifft(idata)+ones(size(idata,1),1)*m;
