EDFlib for C/C++

EDFlib is a programming library for C/C++ to read/write EDF+/BDF+ files. (It also reads old-type EDF/BDF files.)
EDF means European Data Format. BDF is the 24-bits version of EDF.
For a Java version, look here. For a Python version, look here.

The library consists of only two files: edflib.h and edflib.c
and has been tested using the GCC compiler on Linux and MinGW on windows.
MS visual C compiler has not been tested but should work.




This is free software, it is experimental and available under a very permissive BSD style license.
This means that you can use this library in (commercial) closed-source software.
Despite this software is intend to be usefull, there is no warranty, use this software at your own risk.

The sourcecode

Feedback to: teuniz@protonmail.com

EDF for Labview

A collection of VI's to write files in the European Data Format for Labview.

Other EDF software


In EDF, the sensitivity (e.g. uV/bit) and offset are stored using four parameters:
digital maximum and minimum, and physical maximum and minimum.
Here, digital means the raw data coming from a sensor or ADC. Physical means the units like uV.
The sensitivity in units/bit is calculated as follows:

units per bit = (physical max - physical min) / (digital max - digital min)

The digital offset is calculated as follows:

offset = (physical max / units per bit) - digital max

For a better explanation about the relation between digital data and physical data, read the document Coding Schemes Used with Data Converters.

note: An EDF file usually contains multiple so-called datarecords. One datarecord usually has a duration of one second (this is the default but it is not mandatory!).
In that case a file with a duration of five minutes contains 300 datarecords. The duration of a datarecord can be freely choosen but, if possible, use values from
0.1 to 1 second for easier handling. Just make sure that the total size of one datarecord, expressed in bytes, does not exceed 10MByte (15MBytes for BDF(+)).

The recommendation of a maximum datarecordsize of 61440 bytes in the EDF and EDF+ specification was usefull in the time people were still using DOS as their main operating system.
Using DOS and fast (near) pointers (16-bit pointers), the maximum allocatable block of memory was 64KByte.
This is not a concern anymore so the maximum datarecord size now is limited to 10MByte for EDF(+) and 15MByte for BDF(+).
This helps to accommodate for higher samplingrates used by modern, fast Analog to Digital Converters.

EDF header character encoding: The EDF specification says that only ASCII characters are allowed.
EDFlib will automatically convert characters with accents, umlauts, tilde, etc. to their "normal" equivalent without the accent/umlaut/tilde/etc.

The description/name of an EDF+ annotation on the other hand, is encoded in UTF-8.



How do I calculate the samplefrequency of a signal when I read a file?

samplefrequency = ((double)hdr.signalparam[edfsignal].smp_in_datarecord / (double)hdr.datarecord_duration) * EDFLIB_TIME_DIMENSION;

How do I jump to n seconds from the start of the file when I read a file?

If n is an integer (has no fraction):

long long offset;

if(!(hdr.datarecord_duration % EDFLIB_TIME_DIMENSION))
  offset = (n / (hdr.datarecord_duration / EDFLIB_TIME_DIMENSION)) * hdr.signalparam[edfsignal].smp_in_datarecord;
  offset = (long long)(((double)n / ((double)hdr.datarecord_duration / (double)EDFLIB_TIME_DIMENSION)) * (double)hdr.signalparam[edfsignal].smp_in_datarecord);

edfseek(handle, edfsignal, offset, EDFSEEK_SET)));

If n has a fraction:

long long offset;

offset = (long long)(((double)n / ((double)hdr.datarecord_duration / (double)EDFLIB_TIME_DIMENSION)) * (double)hdr.signalparam[edfsignal].smp_in_datarecord);

edfseek(handle, edfsignal, offset, EDFSEEK_SET)));

More info