[ < ] [ > ]   [ << ] [ 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
        end

(See http://www.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://www.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] [ ? ]

Matlab wrappers

A set of wrappers for Matlab has been written and contributed by Jonas Carlson. These wrappers (wfdb_tools) provide access to almost all of the functionality of the WFDB library, including HTTP access to remote data files, to users of Matlab R13 (but not earlier versions) under GNU/Linux, Mac OS X, and MS-Windows; other platforms remain to be tested. The wrappers, together with examples and a tutorial/reference guide, are available from http://www.physionet.org/physiotools/matlab/wfdb_tools/. Using the wfdb_tools wrappers, the example program can be written in Matlab as:

 
        S = WFDB_isigopen('100s')
        DATA = WFDB_getvec(length(S), 10)

Note that length(S) is the number of available signals as determined by WFDB_isigopen (which becomes the number of columns in DATA).

It should be possible to write a set of wrapper functions similar to wfdb_tools for use with Octave (a freely available open-source language that is compatible with Matlab, available from http://www.che.wisc.edu/octave/) or 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/). Jesus Olivan Palacios has written a tutorial (available at http://www.neurotraces.com/scilab/sciteam/) on using the WFDB Software Package with Scilab.

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://www.physionet.org/physiotools/matlab/.


[ < ] [ > ]   [ << ] [ 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:

Java:

 
import wfdb.*;

public class psamples {
    static {
        System.loadLibrary("wfdbjava");
    }

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

Perl:

 
package wfdb;
use wfdb;

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

Python:

 
import wfdb, sys

def main(argv):
    siarray = wfdb.WFDB_SiginfoArray(2)
    if wfdb.isigopen("100s", siarray.cast(), 2) < 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__":
    main(sys.argv[1:])

C#:

 
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)
            Environment.Exit(1);
        WFDB_SampleArray v = new WFDB_SampleArray(2);
        for (int i = 0; i < 10; i++) {
            if (wfdb.getvec(v.cast()) < 0)
                break;
	    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 ] [ >> ]

George B. Moody (george@mit.edu)