MATLAB Answers

Remove baseline wander from ECG

153 views (last 30 days)
I am using the AD8232 and the Arduino pro mini 328-3V. 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?
close all
clear all
if ~isempty(instrfind)
%close all
disp('Serial Port Closed')
s = serial('COM5','BaudRate',9600);
i = 1;
while true
x(i) = str2double(fscanf(s));
i = i + 1;


Star Strider
Star Strider on 4 Sep 2019
Use a highpass (or bandpass) filter. If you are doing this in real time, implement it in hardware.
Anna Karima
Anna Karima on 4 Sep 2019
1)So I can't use a digital filter for processing the real time signal continuously? 2)I probably can save let's say 5 minutes of the signal and then filter it with a digital filter?

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 Sep 2019
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
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.


Sign in to comment.

More Answers (1)

Anna Karima
Anna Karima on 21 Sep 2019
Hi again!!
I have read many papers and ways for filtering an Ecg signal. I tried to use some of these ways to filter my signals but I am having some problems, because I dont think the result is satisfying. Would you PLEASE be able to check out my code and/or of course make ANY changes you want? It is really important for me! I am uploading 3 signals that I have recorded: one had been recorded when I was calm and the others when I was moving, so they have a lot of noise. I would like to remove the noise and the motion artifacts as much as possible!
Here is an example of what I get from the 1st signal:
Screenshot (682).png


Show 7 older comments
Star Strider
Star Strider on 16 Jan 2020
As always, my pleasure!
Anna Karima
Anna Karima on 19 Jan 2020
Hi again!I believe I have one last question and I am really stuck!
When I plot an ecg signal, on the x axis are the samples. For example in one of my ecg signals there is an Rpeak on 117 and the next one is on 141, so their difference is 24 samples. I want to find the average heart rate and the RRinterval.
So, these 24 samples are 0,024 seconds?(duration=nsamples/Fs, Fs=1000).
Thats not right. What do you think?
Star Strider
Star Strider on 19 Jan 2020
For the data you describe:
Fs = 1000;
Ts = 1/Fs;
RR_Interval = diff([117 141])*Ts
HR = 1/RR_Interval
the R-R interval is 0.024 seconds, or 0.0004 minutes, producing a heart rate of 2500 BPM. Something is wrong.
I went back and looked at ‘ecg1.mat’ and got similarly strange numbers:
D = load('ecg1.mat');
Fs = D.Fs;
Ts = 1/Fs;
EKG = D.x;
t = D.t*Ts;
plot(t*Ts, EKG)
[pks,locs] = findpeaks(EKG, 'MinPeakHeight',2.25)
RR_Interval = mean(diff(locs)) % Mean R-R Interval (samples)
RR_Interval_s = RR_Interval*Ts % Mean R-R Interval (sec)
RR_Interval_m = RR_Interval_s/60 % Mean R-R Interval(min)
HR = 1/RR_Interval_m % Heart Rate (BPM)
Looking at that trace, it has about 8 beats/second, which would be about 480 beats/minute. Something is wrong somewhere, and it may be the reported sampling frequency.

Sign in to comment.

Sign in to answer this question.