Last active
August 29, 2015 14:03
-
-
Save jonbrennecke/dc808e7de963eb4b97f7 to your computer and use it in GitHub Desktop.
Read EDF (European Data Format) files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [ header, records ] = edf ( filepath ) | |
% Parse EDF file information based on the EDF/EDF+ specs | |
% @see http://www.edfplus.info/specs/index.html | |
fid = fopen( filepath, 'rb' ); | |
data = fread(fid,256,'*char'); | |
% TODO regex to strip trailing whitespace | |
header = struct(); | |
header.version = data(1:8); | |
header.patient_id = data(9:88); | |
header.rec_id = data(89:168); | |
header.startdate = data(169:176); | |
header.starttime = data(177:184); | |
header.header_bytes = data(185:192); | |
header.num_items = data(237:244); | |
header.data_duration = data(245:252); | |
header.num_signals = str2double(data(253:256)); | |
% advance to the second part of the header | |
data = fread(fid, header.num_signals * 256,'*char'); | |
lengths = [ 16, 80, 8, 8, 8, 8, 8, 80, 8, 32 ]; | |
values = cell(length(lengths),header.num_signals); | |
s = 0; | |
for i=1:length(lengths) | |
for j=0:header.num_signals-1 | |
values{i,j+1} = data(s + j*lengths(i) + 1 : s + j*lengths(i)+lengths(i)); | |
end | |
s = s + j*lengths(i)+lengths(i); | |
end | |
headerkeys = { | |
'labels', | |
'transducer', | |
'dimension', | |
'phys_min', | |
'phys_max', | |
'dig_min', | |
'dig_max', | |
'prefiltering', | |
'num_samples' | |
}; | |
for i=1:length(headerkeys) | |
header.(headerkeys{i}) = { values{i,:} }; | |
end | |
% the data record begins at 256 + ( num_signals * 256 ) bytes and is stored as n records of 2 * num_samples bytes | |
% values are stored as 2 byte ascii in 2's complement | |
s = 0; | |
records = cell(header.num_signals,1); | |
for i=1:header.num_signals | |
data = fread(fid, str2double(header.num_samples{i}),'int16'); | |
records{i} = data; | |
end | |
% finally, close the file and return | |
fclose(fid); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment