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.

ECG Image 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.

ECG from a research Paper

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?


To download the dataset,


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

  • 3
    $\begingroup$ detrend $\endgroup$
    – jojeck
    Mar 9, 2018 at 16:22
  • $\begingroup$ @jojek Thank you for your reply. I did visit that link and it showed "The page you were looking for does not exist." $\endgroup$ Mar 9, 2018 at 16:27
  • 1
    $\begingroup$ Please try to google for "matlab detrend" then $\endgroup$
    – jojeck
    Mar 9, 2018 at 16:32
  • $\begingroup$ Thank you very much for your help. Really appreciate it. $\endgroup$ Mar 9, 2018 at 16:40
  • 1
    $\begingroup$ @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). $\endgroup$
    – Peter K.
    Mar 14, 2018 at 11:15

2 Answers 2


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:

BEADS baseline separation and noise filtering

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:

% load('s0010_rem.mat')
 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);
h=plot([data]);axis tight;grid on;set(h,'LineWidth',2);
h=plot([x1]);axis tight;grid on;set(h,'LineWidth',2);
h=plot([f1]);axis tight;grid on;set(h,'LineWidth',2);
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 :)

  • 1
    $\begingroup$ 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? $\endgroup$ Mar 19, 2018 at 17:27
  • $\begingroup$ Sorry. * I to V6 (12 Leads) $\endgroup$ Mar 19, 2018 at 17:33
  • 1
    $\begingroup$ 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 $\endgroup$ Mar 19, 2018 at 17:42
  • 1
    $\begingroup$ 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 $\endgroup$ Mar 19, 2018 at 17:45
  • 1
    $\begingroup$ 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 $\endgroup$ Mar 19, 2018 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.


[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]')

enter image description here enter image description here

  • $\begingroup$ Very similar to the 1st steps of hilbert huang transform. $\endgroup$
    – Royi
    Jun 24, 2023 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.