[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3 Using the WFDB library with other languages

Bindings and wrappers are available so that programs written in a number of other programming languages can make use of the WFDB library.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

C++ bindings

If you prefer to write your applications in C++, you may do so, but note that the WFDB library is written in C. (Most C++ compilers can be run in ANSI/ISO C compatibility mode in order to compile the WFDB library itself.) Each C++ source file that uses WFDB library functions must include ‘<wfdb/wfdb.h>’, in order to instruct your compiler to use C conventions for argument passing and to use unmangled names for the WFDB library functions. In order for this to work, your C++ compiler should predefine ‘__cplusplus’ or ‘c_plusplus’; if it predefines neither of these symbols, modify ‘<wfdb/wfdb.h>’ so that the symbols ‘wfdb_CPP’ and ‘wfdb_PROTO’ are defined at the top of the file, or define ‘__cplusplus’ in each of your source files before including ‘<wfdb/wfdb.h>’. Compile and link your program using whatever standard methods are supported by your compiler for linking C++ programs with C libraries. See your compiler manual for further information.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

Fortran wrappers

A set of wrapper functions is also available for those who wish to use the WFDB library together with applications written in Fortran. These functions, defined in wfdbf.c, provide a thin ‘wrapper’ around the WFDB library functions, by accepting Fortran-compatible arguments (there are no structures, and all arguments are passed by reference rather than by value). For example, here is the Fortran equivalent of the example program in the previous section:

        integer i, v(2), g

        i = isigopen("100s", 2)
        do i = 1, 10
         g = getvec(v)
         write (6,3) v(1), v(2)
 3      format("v(1) = ", i4, "    v(2) = ", i4)
        end do

(See http://physionet.org/physiotools/wfdb/fortran/fsamples.f for a copy of this program; an extensively commented version of this program is also available, at http://physionet.org/physiotools/wfdb/fortran/example.f.)

To compile this program using g77 (the GNU Fortran compiler), save it as ‘fsamples.f’ in the current directory, copy ‘wfdbf.c’ (which can be found in the same directory as ‘wfdb.h’, usually ‘/usr/include/wfdb’) to the current directory, then type:

g77 -o fsamples -DFIXSTRINGS fsamples.f wfdbf.c -lwfdb

The Fortran wrapper functions are not discussed in this guide; for further information, refer to fortran/README in the WFDB Software Package.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

SWIG wrappers for Java, Perl, Python, and other languages

Isaac Henry has contributed WFDB wrappers for Java, Perl, and Python, as well as for .NET languages such as C#, created using the Simplified Wrapper Interface Generator (SWIG, http://www.swig.org/). Using these wrappers, the example program can be written in any of these languages:


import wfdb.*;

public class psamples {
    static {

    public static void main(String argv[]) {
        WFDB_SiginfoArray siarray = new WFDB_SiginfoArray(2);
        if (wfdb.isigopen ("100s", siarray.cast(), 2) < 2)
        WFDB_SampleArray v = new WFDB_SampleArray(2);
        for (int i = 0; i < 10; i++) {
            if (wfdb.getvec(v.cast()) < 0)
            System.out.println("\t" + v.getitem(0) +"\t"+ v.getitem(1));


package wfdb;
use wfdb;

$siarray = new wfdb::WFDB_SiginfoArray(2);
if ($nsig = isigopen("100s", $siarray->cast(), 2) < 2) {
$v = new wfdb::WFDB_SampleArray(2);
for ($i=0; $i < 10; $i++) {
    if (getvec($v->cast()) < 0) {
    print "\t", $v->getitem(0), "\t", $v->getitem(1), "\n";


import wfdb, sys

def main(argv):
    siarray = wfdb.isigopen("100s")
    if siarray.nsig < 2: sys.exit(1)
    v = wfdb.WFDB_SampleArray(2)
    for i in range(0,10):
        if wfdb.getvec(v.cast()) < 0: sys.exit(2)
        print "\t%d\t%d" % (v[0], v[1])

if __name__ == "__main__":


using System;
using Wfdb;

public class psamples {
    static void Main(string[] argv) {
        WFDB_SiginfoArray siarray = new WFDB_SiginfoArray(2);
        if (wfdb.isigopen("100s", siarray.cast(), 2) < 2)
        WFDB_SampleArray v = new WFDB_SampleArray(2);
        for (int i = 0; i < 10; i++) {
            if (wfdb.getvec(v.cast()) < 0)
	    Console.WriteLine("\t" + v.getitem(0) + "\t" + v.getitem(1));

All SWIG wrappers for the WFDB library are generated using a single interface file, ‘wfdb.i’. In principle, this file might be used to generate wrappers for other programming languages supported by SWIG, including several versions of LISP, Modula-3, PHP, Ruby, and Tcl.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

Matlab toolbox

The WFDB Toolbox for Matlab, contributed by Michael Craig and available from http://physionet.org/physiotools/matlab/wfdb-swig-matlab/, is a collection of WFDB applications implemented as functions in Matlab, built on the SWIG Java wrappers for the WFDB library. For example, using it in Matlab, one can read and plot the first five seconds of the same signals as in the example program above, by:

r = rdsamp('100s', 'maxt', ':5');
plot(r(:,1), r(:,2));

The WFDB Toolbox for Matlab replaces the wfdb_tools library of wrapper functions contributed by Jonas Carlson, since current versions of Matlab are no longer compatible with the wfdb_tools library.

The WFDB Software Package includes wfdb2mat, an application that converts all or any desired segment of a signal file into a .mat file that can be read directly by Matlab.

Jesus Olivan Palacios has written a tutorial (available at http://www.neurotraces.com/scilab/sciteam/) that includes a detailed section on using the WFDB Software Package with Scilab (an open-source scientific software package for numerical computations, with a language similar to that of Matlab, available from http://www-rocq.inria.fr/scilab/). The methods described in this tutorial can also be adapted for use with GNU Octave (another free language that is mostly compatible with Matlab, available from http://www.gnu.org/software/octave/).

Also available is a reimplementation of a useful subset of the WFDB library in native m-code (contributed by Jose Garcia Moros and Salvador Olmos) at http://physionet.org/physiotools/matlab/.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

George B. Moody (george@mit.edu)