MATLAB: Which filter to use to remove baseline wander on ECG

baseline wanderecg

I have ECG values recorded for two minutes from an ECG sensor with an Arduino board with a sample rate of 60S/s. Those are in form of a value with a time stamp. I changed them to mili-voltage. Now I have a lot of baseline wander and it is not linear so I am not sure how to remove it?I have used butter filter of order 2 and does remove some of the noise but does not help at all with baseline. Any help will be appreciated.

Best Answer

Your signal is unfortunately sampled at a relatively low rate, so signal processing is difficult. Your signal also has harmonics of what appear to be a 1.5 Hz signal that are propagated through the entire signal. This noise may give the appearance of baseline wander that actually does not exist, since it is simply the summation of these harmonics. Those are essentially impossible to eliminate without also eliminating parts of your signal.
This analysis and filter appear to work acceptably well:
F = openfig('Normal ECG Signal2.fig');
f = gca;
EKG = findobj(f, 'Type','line');
t = EKG.XData; % Recover Data
s = EKG.YData;
L = numel(t); % Signal Length
Ts = mean(diff(t));
St = std(diff(t)); % Test For Uniform Sampling
tr = linspace(min(t), max(t), L); % New, Regularly-Sampled Time Vector
Tsr = mean(diff(tr)) * 1E-3; % Convert To Seconds
sr = resample(s, tr); % Resample To Regularly-Sampled Signal
Fsr = 1/Tsr; % Sampling Frequency (Hz)

Fnr = Fsr/2;
figure
plot(tr, sr)
grid
FTsr = fft(sr-mean(sr))/L; % Fourier Transform Of Mean-Corrected Signal
Fv = linspace(0, 1, fix(L/2)+1)*Fnr;
Iv = 1:numel(Fv);
figure
plot(Fv, abs(FTsr(Iv))*2)
grid
Fs = Fsr; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
Ws = 0.5/Fn; % Passband Frequency Vector (Normalised)
Wp = 1.5/Fn; % Stopband Frequency Vector (Normalised)
Rp = 1; % Passband Ripple (dB)
Rs = 150; % Stopband Attenuation (dB)
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp,'high'); % Default Here Is A Lowpass Filter
[sos,g] = zp2sos(z,p,k); % Use Second-Order-Section Implementation For Stability
sr_filtered = filtfilt(sos,g,sr); % Filter Signal (Here: ‘sr’)
figure
freqz(sos, 2^14, Fs) % Bode Plot Of Filter
set(subplot(2,1,1), 'XLim',[0 Fn]) % Optional, Change Limits As Necessary

set(subplot(2,1,2), 'XLim',[0 Fn]) % Optional, Change Limits As Necessary
figure
plot(tr, sr_filtered)
grid
I use an elliptical filter here rather than a Butterworth filter because it is shorter and therefore computationally more efficient. It also gives the filter characteristics I want.