%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function X_lo = lowpass(X, n, Wf, [DT]);
% Low pass filtering 
% nth order filter
% The cut-off frequency Wf must be 0.0 < Wf < 1.0, 
% with 1.0 corresponding to half the sample rate.

function X_lo = lowpass(X, n, Wf, DT);

if ~exist('DT')
  DT = 0;
end

[b,a]=butter(n,Wf);

X_lo=filtfilt(b,a,X);


if DT  % plot response function - make sure it's a good one
  [h,w]=freqz(b,a); r=abs(h); f=w/pi/2.0/DT;
  figure; plot(f,r);
  f2title=['Butterworth Response Function, N=',int2str(n),', cut at Wf=',...
	   num2str(Wf)];
  title(f2title);
  grid on
  xlabel 'frequency'
end
