function [hf, tf] = filtresamp(h, t, flim, Fs2, N)
%FILTRESAMP Bandpass filter and resample
% [HF, TF] = FILTRESAMP(H, T, FLIM, FS, N) filters and resamples
% the signal H(T) stored in the rows of H with a lower frequency bound
% FMIN and an upper frequency bound FMAX. The resampling is performed
% with the sampling frequency FS, and the first N samples of the new
% signal is returned.

% EMA Toolbox
% Peter Fiala, 2011

Nf = 5;
Fs = 1/(t(2)-t(1)); % current sampling freq.
[Bl,Al] = butter(Nf,flim(2)/(Fs/2), 'low'); % low pass filtering
hf = filter(Bl, Al, h.').';
if flim(1) > 0 % high pass filtering
[Bh,Ah] = butter(Nf,flim(1)/(Fs/2), 'high');
hf = filter(Bh, Ah, hf.').';
end
tf = (0:N)/Fs2 + t(1); % the new time scale
hf = interp1(t, hf.', tf).'; % resampling
end