Software Open Access

Apnea Detection from the ECG

Chung-Kang Peng

Published: Feb. 4, 2002. Version: 1.0.0

Please include the standard citation for PhysioNet: (show more options)
Goldberger, A., Amaral, L., Glass, L., Hausdorff, J., Ivanov, P. C., Mark, R., ... & Stanley, H. E. (2000). PhysioBank, PhysioToolkit, and PhysioNet: Components of a new research resource for complex physiologic signals. Circulation [Online]. 101 (23), pp. e215–e220.


The apdet software package uses a new automated method to diagnose and quantify obstructive sleep apnea from single-lead electrocardiograms based on detection of the periodic oscillations in cardiac interbeat intervals that are often associated with prolonged cycles of sleep apnea. This technique employs the Hilbert transformation of the sinus interbeat (NN) interval time series to derive the instantaneous amplitudes and frequencies of the series, and calculates their averages and standard deviations over a moving 5-minute window. We then apply a thresholding technique and detect continuous sequences of those windows that lie within threshold limits.

When applied to the training set of the Apnea-ECG Database created for the CinC Challenge 2000, this algorithm correctly classified 26 of 30 subjects (86.6%), and correctly identified the presence or absence of sleep apnea in 13895 of 17045 minutes (82.1%). When applied to the test set of the Apnea-ECG Database, this algorithm performed even better, correctly classifying 28 out of 30 cases (93.3%) of subjects, and 14591 of 17268 minutes (84.5%).

Obtaining the apdet package

The apdet software package is available in source form and as prebuilt binaries for Linux and MS-Windows.

Before building or using the apdet software, you must install the free WFDB software package. Under MS-Windows, you will also need to install the free Cygwin environment before building or using the apdet software.

Unpacking the apdet package requires tar and gzip, and running the software requires sh (or a compatible shell such as bash), and awk, all of which can be found on all Unix, Linux, and Mac OS/X systems, and in the Cygwin environment for MS-Windows. Cygwin also includes the gcc C compiler and all other utilities needed to compile the apdet package from sources if desired.

Downloading and installing the apdet package

Once you have installed the WFDB software package (and, under MS-Windows, the Cygwin environment), download and install the apdet package of your choice:


  1. Download apdet.src.tar.gz. (You may also view or download the sources as individual files.)
  2. Unpack it:
    tar xfvz apdet.src.tar.gz
    tar is included in all versions of Unix, Linux, and MacOSX, and in Cygwin for MS-Windows. If your version of tar doesn't support the z option, unpack the package in two steps:
    gzip -d apdet.src.tar.gz
    tar xfv apdet.src.tar
  3. Enter the source directory (apdet-1.0) and compile the software:
    make all
    (under MS-Windows, make -f Makefile.w32 all).
  4. Install the software:
    make install
    (under MS-Windows, make -f Makefile.w32 install). You will probably need root (administrator) permissions to install in the default location; change the value of BINDIR in Makefile (Makefile.w32) if you need to use a different location.

Linux (x86) binaries:

  1. Download apdet.i386-linux.tar.gz.
  2. As root, unpack it:
    tar xCfvz / apdet.i386-linux.tar.gz

MS-Windows binaries:

  1. Download apdet.cygwin.tar.gz.
  2. In a Cygwin/bash window, unpack it:
    tar xCfvz / apdet.cygwin.tar.gz

Using the apdet package

Open a terminal window (under MS-Windows, a Cygwin/bash window) and type a command such as

get_apdet apnea-ecg/a03 qrs

The first argument is the record name, and the second is the annotator name for a beat annotation file. In the example above, get_apdet will analyze record a03, annotator qrs from the Apnea-ECG Database. (If you have not previously downloaded the input data into a local apnea-ecg directory, get_apdet obtains the data directly from PhysioNet.)

The get_apdet command prints the times of detection of sleep apnea heart rate oscillations and the total fraction of time with such detections. For the example above, the output is:

00:05:59 - 00:59:59
01:39:59 - 02:28:59
03:04:59 - 03:52:59
03:57:59 - 04:22:59
04:38:59 - 05:03:59
05:56:59 - 06:48:59
06:58:59 - 07:24:59
07:45:59 - 08:39:59tot = 05:33:00 / 08:42:25 = 0.637

See an illustration of these detections.

get_apdet relies entirely on normal-to-normal beat interval data, which it normally derives from a beat annotation file. Beat annotation files are available for most of the PhysioBank records that include ECGs. If you wish to study a recording for which no beat annotation file is available, you may be able to create one using sqrs. If you have interbeat interval data but not a beat annotation file, you can modify get_apdet as needed, or you can use rr2ann to put your data into the standard format before processing them.

How does get_apdet work?

The sequence of steps involved in the detection of the RR interval oscillations, as implemented by get_apdet, is as follows:

  1. The normal-to-normal (NN) intervals are derived:
    rrlist $ANN $REC -a N
    If the input files ($REC.$ANN and $REC.hea) are not in the current directory, rrlist looks for them in the PhysioBank archives on PhysioNet.
  2. The NN interval series is then filtered to remove outliers:
    filt 0.2 20 -x 0.4 2.0
  3. The series is then linearly resampled at 1 Hz ...
    linsamp 1.0
  4. ... then locally detrended using a moving 40 second window ...
    ldetrend 40
  5. ... and smoothed using a moving 5 second window:
    smooth 5
  6. Next, the Hilbert transform is applied to derive instantaneous amplitudes and frequencies:
  7. The Hilbert amplitudes and frequencies are then median filtered using a 60 second window:
    htmedfilt 60
  8. The amplitudes are normalized such that the average amplitude equals 1.0 (using awk).
  9. The midpoint of the Hilbert amplitudes is found (using awk and mm), and the minimum Hilbert amplitude threshold is set to
    THRES = a + b*(mid+1)/2
    where a = -0.555 and b = 1.3 (note: the values of a and b were given incorrectly in the original publication; please refer to the posted version instead.)
  10. The mean Hilbert amplitudes and frequencies, their standard deviations and fraction of time within their specified threshold limits are calculated over 5-minute windows incremented each minute:
    htavsd 1:00 5:00 THRES
  11. Those windows where the means, standard deviations and threshold times of the Hilbert amplitudes and frequencies are all within their specified limits are selected (using awk).
  12. Apnea is detected whenever at least 15 consecutive windows are selected in the previous step, and the times of the detected apneic periods are printed:
    detruns 1:00 5:00 15:00

This program correctly identifies the presence or absence of apnea in in 82.1% of the data in the CinC test set and 84.5% of the data in the CinC training set.

It is recommended that the accuracy of the oscillation detections be verified by visual inspection of the original RR interval series.


This software was contributed by JE Mietus, C-K Peng, PCh Ivanov, AL Goldberger from Beth Israel Deaconess Medical Center and Harvard Medical School, Boston, USA


Access Policy:
Anyone can access the files, as long as they conform to the terms of the specified license.

License (for files):
Open Data Commons Attribution License v1.0


apnea sleep ecg

Corresponding Author
You must be logged in to view the contact information.


Total uncompressed size: 0 B.

Access the files
Folder Navigation: <base>
Name Size Modified
a03.html (download) 826 B 2019-04-12
a03.pdf (download) 181.5 KB 2019-04-12
a03.png (download) 42.0 KB 2019-04-12
apdet.cygwin.tar.gz (download) 18.9 KB 2019-04-12
apdet.i386-linux.tar.gz (download) 17.6 KB 2019-04-12
apdet.shtml (download) 16.3 KB 2019-04-12
apdet.src.tar.gz (download) 9.7 KB 2019-04-12
fig1.png (download) 14.6 KB 2019-04-12
fig2.png (download) 11.5 KB 2019-04-12
img10.png (download) 193 B 2019-04-12
img2.png (download) 306 B 2019-04-12
img3.png (download) 431 B 2019-04-12
img4.png (download) 248 B 2019-04-12
img5.png (download) 188 B 2019-04-12
img7.png (download) 670 B 2019-04-12
img8.png (download) 301 B 2019-04-12
img9.png (download) 185 B 2019-04-12