ImaGIN_NormaliseTF.m
4.08 KB
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
function D = ImaGIN_NormaliseTF(S)
% Normalise several TF files according to a baseline file
% -=============================================================================
% This function is part of the ImaGIN software:
% https://f-tract.eu/
%
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE AUTHORS
% DO NOT ASSUME ANY LIABILITY OR RESPONSIBILITY FOR ITS USE IN ANY CONTEXT.
%
% Copyright (c) 2000-2018 Inserm U1216
% =============================================================================-
%
% Authors: Olivier David
[Finter,Fgraph,CmdLine] = spm('FnUIsetup','TF',0);
%files to normalise
try
DD = S.D;
catch
DD = spm_select(inf, '\.mat$', 'Select TF EEG mat file(s) to normalise');
end
%baseline file
try
BB = S.B;
catch
BB = spm_select(inf, '\.mat$', 'Select TF EEG mat file(s) (baseline)');
end
if isempty(BB)
try
BB=S.B;
catch
BB = spm_input('Baseline time window (s)', '+1', 'r', '', 2);
end
end
try
clear tmp
for i1=1:size(DD,1)
tmp{i1} = spm_eeg_load(deblank(DD(i1,:)));
Dname{i1}=DD(i1,:);
end
DD=tmp;
catch
error(sprintf('Trouble reading file %s', DD));
end
if ~isnumeric(BB)
try
clear tmp
for i1=1:size(BB,1)
tmp{i1} = spm_eeg_load(deblank(BB(i1,:)));
Bname{i1}=BB(i1,:);
end
BB=tmp;
catch
error(sprintf('Trouble reading file %s', B));
end
end
if ~isnumeric(BB)
if isfield(BB{1}, 'Nfrequencies') && isfield(DD{1},'Nfrequencies')
s=zeros(BB{1}.Nfrequencies,BB{1}.nchannels);
m=zeros(BB{1}.Nfrequencies,BB{1}.nchannels);
for i1=1:size(BB{1},1)
tmp=[];
for i2=1:length(BB)
tmp=[tmp squeeze(double(BB{i2}(i1,:,:)))];
end
s(:,i1)=std(tmp,[],2);
m(:,i1)=mean(tmp,2);
end
s(find(s<=10*eps))=1; %because otherwise it gives inf for normalised data
for i2=1:length(DD)
D=DD{i2};
if (BB{1}.Nfrequencies == D.Nfrequencies) && (BB{1}.nchannels == D.nchannels)
data=D(:,:,:,:);
for i1=1:D.nchannels
for i2=1:D.ntrials
ss=s(:,i1)*ones(1,D.nsamples);
mm=m(:,i1)*ones(1,D.nsamples);
data(i1,:,:,i2)=(squeeze(data(i1,:,:,i2))-mm)./ss;
end
end
D.tf.Label=['Normalised ' lower(D.tf.Label)];
D=clone(D,['n' D.fname],[D.nchannels D.Nfrequencies D.nsamples D.ntrials]);
D(:,:,:,:)=data;
save(D);
end
end
else
error('No time frequency data');
end
else
if isfield(DD{1},'Nfrequencies')
for i2=1:length(DD)
nwindow=length(BB)/2;
if nwindow==1
index=find(DD{i2}.tf.time>=min(BB)&DD{i2}.tf.time<=max(BB));
else
index=[];
for i3=1:nwindow
index=[index find(DD{i2}.tf.time>=BB(2*(i3-1)+1)&DD{i2}.tf.time<=BB(2*i3))];
end
end
D=DD{i2};
data=D(:,:,:,:);
for i1=1:D.nchannels
for i2=1:D.ntrials
tmp=squeeze(double(data(i1,:,index,i2)));
ss=std(tmp,[],2)*ones(1,D.nsamples);
mm=mean(tmp,2)*ones(1,D.nsamples);
ss(find(ss<=10*eps))=1; %because otherwise it gives inf for normalised data
data(i1,:,:,i2)=(squeeze(data(i1,:,:,i2))-mm)./ss;
end
end
D.tf.Label=['Normalised ' lower(D.tf.Label)];
D=clone(D,['n' D.fname],[D.nchannels D.Nfrequencies D.nsamples D.ntrials]);
D(:,:,:,:)=data;
save(D);
end
else
error('No time frequency data');
end
end