MATLAB: Remove the baseline wander

baseline wanderecgfilter processingmotion artifacts

I want to remove the motion artifacts from my signal. I am using matlab for the signal processing and this code for the serial communication with arduino. How can I do the motion artifacts removal?
clc
close all
clear all
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
%close all
clc
disp('Serial Port Closed')
s = serial('COM5','BaudRate',9600);
fopen(s);
fprintf(s,'*IDN?');
i = 1;
while true
x(i) = str2double(fscanf(s));
i = i + 1;
plot(x)
pause(0.1)
end

Best Answer

1)So I can't use a digital filter for processing the real time signal continuously?
I’m not aware of any MATLAB ability to do real-time digital filtering. The Audio Toolbox may have this ability, however I don’t have it so I have no experience with it.
2)I probably can save let's say 5 minutes of the signal and then filter it with a digital filter?
That’s certainly an option. The highpass and bandpass functions (R2018a and later) make the filter design much easier. (Return the second argument as the designeed filter, then use it with your signals with the filtfilt function to filter other signals.) If you don’t have those functions, it is easy to design an efficient ellliptic filter to do what you want.
For example:
Fs = 1000; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Wp = [859 862]/Fn; % Passband Normalised
Ws = [855 865]/Fn; % Stopband Normalised
Rp = 1; % Passband Ripple (Irrelevant in Butterworth)
Rs = 50; % Stopband Attenuation
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp); % Zero-Pole-Gain
[sos,g] = zp2sos(z,p,k); % Second-Order Section For Stability
figure
freqz(sos, 2^16, Fs) % Filter Bode Plot
s_filt = filtfilt(sos, g, s); % Filter Signal
This designs a bandpass filter with passband frequencies of 859 to 862 Hz, and uses it fo filter signal vector ‘s’. For EKG signal processing, the passband would be 1 Hz to 100 Hz, with the stopbands of 0.5 Hz and 105 Hz.