-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathstd_interp.m
160 lines (143 loc) · 5.59 KB
/
std_interp.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
% std_interp() - interpolate, if needed, a list of named data channels
% for all datasets included in a STUDY. Currently assumes
% that all channels have uniform locations across datasets.
%
% Usage: >> [STUDY ALLEEG] = std_interp(STUDY, ALLEEG, chans, method);
%
% Inputs:
% STUDY - EEGLAB STUDY structure
% ALLEEG - EEGLAB vector containing all EEG dataset structures in the STUDY.
% chans - [Cell array] cell array of channel names (labels) to interpolate
% into the data if they are missing from one of the datasets.
% method - [string] griddata() method to use for interpolation.
% See >> help griddata() {default:'invdist'}
%
% Important limitation:
% This function currently presuposes that all datasets have the same channel
% locations (with various channels from a standard set possibly missing).
% If this is not the case, the interpolation will not be performed.
%
% Output:
% STUDY - study structure.
% ALLEEG - updated datasets.
%
% Author: Arnaud Delorme, CERCO, CNRS, August 2006
%
% See also: eeg_interp()
% Copyright (C) Arnaud Delorme, CERCO, 2006, arno@salk.edu
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% $Log: not supported by cvs2svn $
% Revision 1.3 2007/02/18 19:05:05 scott
% help msg consistency and usage -sm
%
% Revision 1.2 2006/09/20 12:26:01 arno
% use function eeg_mergelocs
%
% Deprecated:
% - [chanlocs structure] channel location structure containing
% a full channel structure (missing channels in the current
% dataset are interpolated).
function [STUDY, ALLEEG] = std_interp(STUDY, ALLEEG, chans, method);
if nargin < 2
help std_interp;
return;
end;
if nargin < 3
chans = [];
end;
if nargin < 4
method = 'invdist';
end;
% union of all channel structures
% -------------------------------
alllocs = eeg_mergelocs(ALLEEG(:).chanlocs);
% Check to see if all the channels have the same coordinates
% (check only the theta field)
% ----------------------------------------------------------
for index = 1:length(STUDY.datasetinfo)
tmpind = STUDY.datasetinfo(index).index;
tmplocs = ALLEEG(tmpind).chanlocs;
[tmp id1 id2] = intersect({tmplocs.labels}, {alllocs.labels});
for ind = 1:length(id1)
if tmplocs(id1(ind)).theta ~= alllocs(id2(ind)).theta
% find datasets with different coordinates
% ----------------------------------------
for ind2 = 1:length(STUDY.datasetinfo)
tmplocs2 = ALLEEG(ind2).chanlocs;
tmpmatch = strmatch(alllocs(id2(ind)).labels, { tmplocs2.labels });
if ~isempty(tmpmatch)
if alllocs(id2(ind)).theta == tmplocs2(tmpmatch).theta
datind = ind2;
break;
end;
end;
end;
error(sprintf( [ 'Dataset %d and %d do not have the same channel location\n' ...
'for electrode ''%s''' ], datind, tmpind, tmplocs(id1(ind)).labels));
end;
end;
end;
% check electrode names to interpolate
% ------------------------------------
if iscell(chans)
alllabs = lower({ alllocs.labels });
for index = 1:length(chans)
tmpind = strmatch(lower(chans{index}), alllabs);
if isempty(tmpind)
error( sprintf('Channel named ''%s'' not found in any dataset', chans{index}));
end;
end;
end;
% read all STUDY datasets and interpolate electrodes
% ---------------------------------------------------
for index = 1:length(STUDY.datasetinfo)
tmpind = STUDY.datasetinfo(index).index;
tmplocs = ALLEEG(tmpind).chanlocs;
% build electrode location structure for interpolation
% ----------------------------------------------------
[tmp tmp2 id1] = intersect({tmplocs.labels}, {alllocs.labels});
if isempty(chans)
interplocs = alllocs;
elseif iscell(chans)
[tmp tmp2 id2] = intersect( chans, {alllocs.labels});
interplocs = alllocs(union(id1, id2));
else
interplocs = chans;
end;
if length(interplocs) ~= length(tmplocs)
% perform interpolation
% ---------------------
EEG = eeg_retrieve(ALLEEG, index);
EEG = eeg_checkset(EEG);
EEG = eeg_interp(EEG, interplocs, method);
EEG.saved = 'no';
EEG = pop_saveset(EEG, 'savemode', 'resave');
% update dataset in EEGLAB
% ------------------------
if isstr(ALLEEG(tmpind).data)
tmpdata = ALLEEG(tmpind).data;
[ ALLEEG EEG ] = eeg_store(ALLEEG, EEG, tmpind);
ALLEEG(tmpind).data = tmpdata;
ALLEEG(tmpind).saved = 'yes';
clear EEG;
else
[ ALLEEG EEG ] = eeg_store(ALLEEG, EEG, tmpind);
ALLEEG(tmpind).saved = 'yes';
end;
else
fprintf('No need for interpolation for dataset %d\n', tmpind);
end;
end;