forked from fieldtrip/fieldtrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edf2fieldtrip.m
94 lines (78 loc) · 3.27 KB
/
edf2fieldtrip.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function data = edf2fieldtrip(filename)
% EDF2FIELDTRIP reads data from a EDF file with channels that have a different
% sampling rates. It upsamples all data to the highest sampling rate and
% concatenates all channels into a raw data structure that is compatible with the
% output of FT_PREPROCESSING.
%
% Use as
% data = edf2fieldtrip(filename)
%
% For reading EDF files in which all channels have the same sampling rate, you can
% use the standard procedure with FT_DEFINETRIAL and FT_PREPROCESSING.
%
% See also FT_PREPROCESSING, FT_DEFINETRIAL, FT_REDEFINETRIAL
% Copyright (C) 2015, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
headerformat = 'edf';
dataformat = 'edf';
hdr = ft_read_header(filename, 'headerformat', headerformat);
samplerate = unique(hdr.orig.SampleRate);
data = cell(size(samplerate));
for i=1:numel(samplerate)
chanindx = find(hdr.orig.SampleRate==samplerate(i));
fprintf('reading %d channels with %g Hz sampling rate\n', numel(chanindx), samplerate(i));
% read the header and data for the selected channels
hdr = ft_read_header(filename, 'chanindx', chanindx, 'headerformat', headerformat);
dat = ft_read_data(filename, 'header', hdr, 'headerformat', headerformat, 'dataformat', dataformat);
% construct a time axis, starting at 0 seconds
time = ((1:(hdr.nTrials*hdr.nSamples)) - 1)./hdr.Fs;
% make a raw data structure
data{i}.hdr = hdr;
data{i}.label = hdr.label;
data{i}.time = {time}; % only single data segment
data{i}.trial = {dat}; % only single data segment
end
[maxrate, maxindex] = max(samplerate);
% upsample the data to the highest sampling rate
for i=1:numel(samplerate)
if i==maxindex
continue
end
fprintf('upsampling %d channels from %g to %g Hz\n', numel(data{i}.label), samplerate(i), maxrate);
cfg = [];
cfg.time = data{maxindex}.time;
data{i} = ft_resampledata(cfg, data{i});
end
% concatenate them into a single data structure
cfg = [];
data = ft_appenddata(cfg, data{:});
% reorder the channels to the original order in the EDF file
origlabel = cellstr(hdr.orig.Label);
[currentorder, origorder] = match_str(origlabel, data.label); % sorted according to the 1st input argument
data.label = data.label(origorder);
data.trial{1} = data.trial{1}(origorder,:);
% annotate the manual operation in the data structure provenance
cfg = [];
cfg.comment = 'reordered the channels to the original order in the EDF file';
data = ft_annotate(cfg, data);
if isfield(data, 'hdr')
% remove this, as otherwise it might be very confusing with the subselections
data = rmfield(data, 'hdr');
end