function out = LPF_ECG(x1,Fs)

% This function allows to remove the high frequency noise (greather thatn 40 Hz) of an ECG signal.
% It applies a lowpass equiripple FIR filter to the signal x1.

% out = HPF_ECG(x1,Fs)
% Inputs:
% X1: ECG vector
% Fs: Sampling frequency
% Outputs:
% out: Filtered ECG signal
 
% Author: Miguel Altuve, maltuve@usb.ve
% Grupo de Bioingenieria y Biofisica Aplicada, Universidad Simon Bolivar
% Last updated: April 16, 2014

N      = 22;     % Order
Fstop1 = -45;    % First Stopband Frequency
Fpass1 = -40;    % First Passband Frequency
Fpass2 = 40;     % Second Passband Frequency
Fstop2 = 45;     % Second Stopband Frequency
Wstop1 = 1;      % First Stopband Weight
Wpass  = 7;      % Passband Weight
Wstop2 = 1;      % Second Stopband Weight
debug  = 'off';  % Debug Mode
dens   = 25;     % Density Factor

% Calculate the coefficients using the CREMEZ function.
b  = cfirpm(N, [-Fs/2 Fstop1 Fpass1 Fpass2 Fstop2 Fs/2]/(Fs/2), ...
            'lowpass', [Wstop1 Wpass Wstop2], {dens}, debug);
Hd = dfilt.dffir(b);

out = filtfilt(Hd.Numerator,1,x1);

        