This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
forked from NadineDijkstra/IMAREV
/
OscillatorReactivation.m
314 lines (244 loc) · 9.56 KB
/
OscillatorReactivation.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
function OscillatorReactivation(cfg)
outputDir = fullfile(cfg.root,cfg.output);
if ~exist(outputDir,'dir'); mkdir(outputDir); end
% get the data
load(fullfile(cfg.root,cfg.dataSet),'L');
real_data = L; clear L
load(fullfile(cfg.root,cfg.controlSet),'L');
control_data = L; clear L
%% Wavelet decomposition
sf = 300;
t = -0.5:(1/sf):0.5;
w = 2*pi*10;
s = (1*2*pi)/w;
time = linspace(-0.2,1,360);
% create wavelet
mw = exp((-t.^2)/(2*s^2)+1i*w*t);
mw = mw(50:250);
% plot(real(mw))
%% Evoked oscillation - real data
% throw out last data point
L = real_data(:,1:end-1);
n = size(L,2);
time = time(1:n);
if ~exist(fullfile(outputDir,'EvokedOscillation.mat'),'file')
[mX_Evo,btstrp_Evo,z1_Evo,z2_Evo] = EvokedOscillations(L,mw,1);
w1 = ((n-size(btstrp_Evo,2))/2);
tp = time(w1+1:end-w1);
% save
save(fullfile(outputDir,'EvokedOscillation'),'mX_Evo','btstrp_Evo','z1_Evo','z2_Evo','tp')
else
% load
load(fullfile(outputDir,'EvokedOscillation'),'mX_Evo','z1_Evo','z2_Evo','tp')
end
% plot the things
figure(1); subplot(2,1,1)
h = fill([tp';flipud(tp')],[z1_Evo';flipud(z2_Evo')],[0 0 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(tp,mX_Evo,'Color','b','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation');
%% Evoked oscillation - control data
% throw out last data point
L = control_data(:,1:end-1);
n = size(L,2);
time = time(1:n);
if ~exist(fullfile(outputDir,'EvokedOscillationPerm.mat'),'file')
[mX_Evo,btstrp_Evo,z1_Evo,z2_Evo] = EvokedOscillations(L,mw,1);
w1 = ((n-size(btstrp_Evo,2))/2);
tp = time(w1+1:end-w1);
% save
save(fullfile(outputDir,'EvokedOscillationPerm'),'mX_Evo','btstrp_Evo','z1_Evo','z2_Evo','tp')
else
% load
load(fullfile(outputDir,'EvokedOscillationPerm'),'mX_Evo','btstrp_Evo','z1_Evo','z2_Evo','tp')
end
% plot the things
figure(1); subplot(2,1,1); hold on
h = fill([tp';flipud(tp')],[z1_Evo';flipud(z2_Evo')],[0.5 0.5 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(tp,mX_Evo,'Color','k','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation');
%% Evoked oscillation - compare
L1 = real_data(:,1:end-1);
L2 = control_data(:,1:end-1);
n = size(L1,2);
time = time(1:n);
if ~exist(fullfile(outputDir,'EvokedOscillationCompare.mat'),'file')
[mX_Evo,btstrp_Evo,z1_Evo,z2_Evo] = EvokedOscillationsCompare(L1,L2,mw);
w1 = ((n-size(btstrp_Evo,2))/2);
tp = time(w1+1:end-w1);
% save
save(fullfile(outputDir,'EvokedOscillationCompare'),'mX_Evo','btstrp_Evo','z1_Evo','z2_Evo','tp')
else
% load
load(fullfile(outputDir,'EvokedOscillationCompare'),'mX_Evo','btstrp_Evo','z1_Evo','z2_Evo','tp')
end
figure(2); subplot(2,1,1); hold on
h = fill([tp';flipud(tp')],[z1_Evo';flipud(z2_Evo')],[0 0.5 0],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(tp,mX_Evo,'Color','g','LineWidth',2);
hold on; plot(xlim,[0 0],'Color','k','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation');
xlim([-0.2 1])
% get p values
pVal = 1-(sum(btstrp_Evo>0)/10000);
[~,qVal] = mafdr(pVal); tmp = ones(length(tp),1)*-25;
hold on; plot(tp(qVal<0.05),tmp(qVal<0.05),'*r','LineWidth',2)
%% Induced oscillation - real data
% throw out last data point
L = real_data(:,1:end-1);
n = size(L,2);
time = time(1:n);
if ~exist(fullfile(outputDir,'InducedOscillation.mat'),'file')
[mX_Ind,btstrp_Ind,z1_Ind,z2_Ind] = InducedOscillations(L,mw);
w1 = ((n-size(btstrp_Ind,2))/2);
tp = time(w1+1:end-w1);
% save
save(fullfile(outputDir,'InducedOscillation'),'mX_Ind','btstrp_Ind','z1_Ind','z2_Ind','tp')
else
% load
load(fullfile(outputDir,'InducedOscillation'),'mX_Ind','btstrp_Ind','z1_Ind','z2_Ind','tp')
end
% plot the things
figure(1); subplot(2,1,2)
h = fill([tp';flipud(tp')],[z1_Ind';flipud(z2_Ind')],[0 0 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(tp,mX_Ind,'Color','b','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Induced oscillation');
%% Induced oscillation - control data
% throw out last data point
L = control_data(:,1:end-1);
n = size(L,2);
time = time(1:n);
if ~exist(fullfile(outputDir,'InducedOscillationPerm.mat'),'file')
[mX_Ind,btstrp_Ind,z1_Ind,z2_Ind] = InducedOscillations(L,mw);
w1 = ((n-size(btstrp_Ind,2))/2);
tp = time(w1+1:end-w1);
% save
save(fullfile(outputDir,'InducedOscillationPerm'),'mX_Ind','btstrp_Ind','z1_Ind','z2_Ind','tp')
else
% load
load(fullfile(outputDir,'InducedOscillationPerm'),'mX_Ind','btstrp_Ind','z1_Ind','z2_Ind','tp')
end
% plot the things
figure(1); subplot(2,1,2); hold on
h = fill([tp';flipud(tp')],[z1_Ind';flipud(z2_Ind')],[0.5 0.5 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(tp,mX_Ind,'Color','k','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Induced oscillation');
%% Frequency power - real data
% do hanning taper before fft focus on first 400 ms
idx = 1:241; % -0.2 to 0.6 s
L = real_data(:,idx);
L = L-mean(L,2); % remove mean
w = hann(size(L,2)); % make hanning taper
L = L.*w'; % multiply signal by hanning taper
n = size(L,2);
time = linspace(-0.2,1,360);
time = time(idx);
% Evoked power
if ~exist(fullfile(outputDir,'EvokedPower.mat'),'file')
[mX_power_evo,f,btstrp_power_evo,z1_power_evo,z2_power_evo] = EvokedPower(L,sf,1);
save(fullfile(outputDir,'EvokedPower'),'mX_power_evo','btstrp_power_evo','z1_power_evo','z2_power_evo','f');
else
load(fullfile(outputDir,'EvokedPower'),'mX_power_evo','btstrp_power_evo','z1_power_evo','z2_power_evo','f');
end
% plot it
figure(3); subplot(2,1,1); hold on
h = fill([f';flipud(f')],[z1_power_evo';flipud(z2_power_evo')],[0 0 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(f,mX_power_evo,'Color','b','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation'); xlim([2 50])
% induced power
if ~exist(fullfile(outputDir,'InducedPower.mat'),'file')
[mX_power_ind,btstrp_power_ind,z1_power_ind,z2_power_ind,f] = InducedPower(L,sf);
save(fullfile(outputDir,'InducedPower'),'mX_power_ind','btstrp_power_ind','z1_power_ind','z2_power_ind','f')
else
load(fullfile(outputDir,'InducedPower'),'mX_power_ind','btstrp_power_ind','z1_power_ind','z2_power_ind','f')
end
% plot it
figure(3); subplot(2,1,2);
h = fill([f';flipud(f')],[z1_power_ind';flipud(z2_power_ind')],[0 0 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(f,mX_power_ind,'Color','b','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation'); xlim([2 50])
%% Frequency power - control data
% do hanning taper before fft focus on first 400 ms
idx = 1:241; % -0.2 to 0.6 s
L = control_data(:,idx);
L = L-mean(L,2); % remove mean
w = hann(size(L,2)); % make hanning taper
L = L.*w'; % multiply signal by hanning taper
n = size(L,2);
time = linspace(-0.2,1,360);
time = time(idx);
% Evoked power
if ~exist(fullfile(outputDir,'EvokedPowerPerm.mat'),'file')
[mX_power_evo,f,btstrp_power_evo,z1_power_evo,z2_power_evo] = EvokedPower(L,sf,1);
save(fullfile(outputDir,'EvokedPowerPerm'),'mX_power_evo','btstrp_power_evo','z1_power_evo','z2_power_evo','f');
else
load(fullfile(outputDir,'EvokedPowerPerm'),'mX_power_evo','btstrp_power_evo','z1_power_evo','z2_power_evo','f');
end
% plot it
figure(3); subplot(2,1,1); hold on
h = fill([f';flipud(f')],[z1_power_evo';flipud(z2_power_evo')],[0.5 0.5 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(f,mX_power_evo,'Color','k','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation');
xlim([2 50])
% induced power
if ~exist(fullfile(outputDir,'InducedPowerPerm.mat'),'file')
[mX_power_ind,btstrp_power_ind,z1_power_ind,z2_power_ind,f] = InducedPower(L,sf);
save(fullfile(outputDir,'InducedPowerPerm'),'mX_power_ind','btstrp_power_ind','z1_power_ind','z2_power_ind','f')
else
load(fullfile(outputDir,'InducedPowerPerm'),'mX_power_ind','btstrp_power_ind','z1_power_ind','z2_power_ind','f')
end
% plot it
figure(3); subplot(2,1,2); hold on
h = fill([f';flipud(f')],[z1_power_ind';flipud(z2_power_ind')],[0.5 0.5 0.5],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(f,mX_power_ind,'Color','k','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation');
xlim([2 50])
%% Frequency power - compare
% do hanning taper before fft focus on first 400 ms
idx = 1:241; % -0.2 to 0.6 s
n = length(idx);
w = hann(n); % make hanning taper
L1 = real_data(:,idx);
L1 = L1-mean(L1,2); % remove mean
L1 = L1.*w'; % multiply signal by hanning taper
L2 = control_data(:,idx);
L2 = L2-mean(L2,2); % remove mean
L2 = L2.*w'; % multiply signal by hanning taper
time = linspace(-0.2,1,360);
time = time(idx);
% Evoked power
if ~exist(fullfile(outputDir,'EvokedPowerCompare.mat'),'file')
[mX_power_evo,btstrp_power_evo,z1_power_evo,z2_power_evo,f] = EvokedPowerCompare(L1,L2,sf);
save(fullfile(outputDir,'EvokedPowerCompare'),'mX_power_evo','btstrp_power_evo','z1_power_evo','z2_power_evo','f');
else
load(fullfile(outputDir,'EvokedPowerCompare'),'mX_power_evo','btstrp_power_evo','z1_power_evo','z2_power_evo','f');
end
% plot it
figure(2); subplot(2,1,2);
h = fill([f';flipud(f')],[z1_power_evo';flipud(z2_power_evo')],[0 0.5 0],'LineStyle','none');
set(h,'facealpha',.5);
hold on; plot(f,mX_power_evo,'Color','g','LineWidth',2);
xlabel('Time (s)'); ylabel('Power');
title('Evoked oscillation');
hold on; plot(xlim,[0 0],'k','LineWidth',2);
xlim([2 50])
% get p values
pVal = 1-(sum(btstrp_power_evo>0)/10000);
[~,qVal] = mafdr(pVal); tmp = ones(length(f),1)*-0.5;
hold on; plot(f(qVal<0.025),tmp(qVal<0.025),'*r','LineWidth',2)