# Plotted ECG signals are not around Amplitude 0 line

For my project I have to have few datasets of ECG signals and I did get them from Physionet website.

What I Got

So I downloaded few ECG signals and plotted them in Matlab. Below shows one plot (I lead) I got.

As you can see, this signal is not in the Amplitude 0 line. But all the ECG recordings I have seen and saw in research papers are horizontal. Like in below image.

My Question

I'm new to ECG signal processing. So I'm curious to know,

1)Whether datasets having this kind of abnormal behavior of ECG are wrong?

2)Is there any Matlab function which can convert such data to view like in 2nd image I attached?

UPDATE

https://www.physionet.org/cgi-bin/atm/ATM

Go to the above link and select "PTB Diagnosed ECG database" as the Database and select a patient record (E.g: s0010_re).

• detrend – jojek Mar 9 '18 at 16:22
• @jojek Thank you for your reply. I did visit that link and it showed "The page you were looking for does not exist." – Samitha Nanayakkara Mar 9 '18 at 16:27
• Please try to google for "matlab detrend" then – jojek Mar 9 '18 at 16:32
• Thank you very much for your help. Really appreciate it. – Samitha Nanayakkara Mar 9 '18 at 16:40
• @Abyr Welcome to SE.DSP! Please get enough reputation to comment and do NOT add comments as answers, as per A_A's links. You can get reputation by asking good questions, giving good answers, or editing past posts (and have the edits accepted). – Peter K. Mar 14 '18 at 11:15

I have tried the BEADS technique we have developed for the separation of

• sparse signals, possibly positive, with sparse derivatives,
• a trend (low frequency),
• random noise.

I have played with one signal for about two minutes, and here is one outcome:

It is called BEADS, for "Baseline Estimation And Denoising with Sparsity". The Matlab BEADS code is at MatlabCentral, the paper is Chromatogram baseline estimation and denoising using sparsity (BEADS). It was initially meant for chromatographic signals, but is also used for EEG/ECG and several other spectral signal, see the BEADS page .

One nice feature is that you directly see the noise (and how uncorrelated it can be) and the shape of the trend, then tweak the parameters. Mine (probably not optimal) are:

addpath('C:\Users\duvall\Documents\MATLAB\toolbox\2014_BEADS_Baseline\');
data = val(1,:)';
% Filter parameters
fc = 0.004;     % fc : cut-off frequency (cycles/sample)
d = 1;          % d : filter order parameter (d = 1 or 2)
% Positivity bias (peaks are mostly symmetric)
r = 1;          % r : asymmetry parameter
% Regularization parameters
amp = 0.8;
lam0 = 0.5*amp;
lam1 = 5*amp;
lam2 = 4*amp;

[x1, f1, cost] = beads(data, d, fc, r, lam0, lam1, lam2);
figure(2);
subplot(4,1,1)
h=plot([data]);axis tight;grid on;set(h,'LineWidth',2);
subplot(4,1,2)
h=plot([x1]);axis tight;grid on;set(h,'LineWidth',2);
subplot(4,1,3)
h=plot([f1]);axis tight;grid on;set(h,'LineWidth',2);
subplot(4,1,4)
h=plot([data-x1-f1]);axis tight;grid on;set(h,'LineWidth',2);


Basically, it took me 10-times more to download and load the signal than to tune the parameters (90 seconds approximately), but I know the code :)

• WOW Amazing. I'm a true fan of your work. I did play with BEADS tool and it is amazing. I did use this technique on another data set and worked completely fine and provided nice result. Also I have to adjust few hundreds of datasets where each .mat file contains I to Vz, 12 lead data. I'm curious to know if there is a way to adjust all of these files at once with help of BEADS? Adjusting Parameters for 600*12 datasets would be a pain. Any idea Sir? – Samitha Nanayakkara Mar 19 '18 at 17:27
• Sorry. * I to V6 (12 Leads) – Samitha Nanayakkara Mar 19 '18 at 17:33
• I am glad you like BEADS.We are working on a more automated parameter estimation. What is surprising so far, is that given a new signal, we can find nice parameters by hand in a couple of trial and errors, but our simplex searches so far fail to find them. So, work in progress – Laurent Duval Mar 19 '18 at 17:42
• Actually, if the data don't differ too much, the same parameters should work. By the way, you gave me an idea I should try; optimize the set of parameters on a couple of signals instead of only one – Laurent Duval Mar 19 '18 at 17:45
• Some would say that LM can also correct for drift and noise... If the signals have consistent sampling, I'd probably pick a random sample of few signals, or a couple in each coarse class, find by hand "average" parameters, and run that on all files, displaying for each the removed baseline and noise. There, by quickly browsing them, check if everything goes well. If, by chance, only a few are not corrected properly, either put them outside the training first, or fine tune. I hope the drift and noise are not important features for your learning system – Laurent Duval Mar 19 '18 at 18:00

Adding to the comments: As people have already said, this is a phenomenom called baseline drift and can be handled in different ways depending on how your signal looks.

For sinusoidal signals where the frequency spectrum is known to you, it is possible to just apply bandpassfilters to remove the unwanted drift.

For your example I would probably just subtract the envelope (generated from the peaks) of the signal. This works for nonlinear shifts as well. Attached bellow is a matlab example to illustrate the technique. TAKE NOTICE: I selected a bad example intentionally, to highlight a limitation in getting perfect signal structure back between RR-intervals.

load('nonuniformdata.mat','ecgsig','t2')

[yupper,ylower] = envelope(ecgsig,700,'peak'); %% use the findpeak function to find suitable peak distance

figure; plot(t2,ecgsig)
hold on; plot(t2,ylower,'Linewidth',2)
hold on; plot(t2,yupper,'Linewidth',2)
title('ECG signal with non linear baseline variation')
xlabel('time [s]')
ylabel('Amplitude [A.U]')

figure; plot(t2,ecgsig-ylower)

figure; plot(t2,ecgsig-yupper)
title('ECG signal with upper envelope subtracted')
xlabel('time [s]')
ylabel('Amplitude [A.U]')