-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sectrogram_on_concatenated_runs.m~
392 lines (286 loc) · 10.6 KB
/
Sectrogram_on_concatenated_runs.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
clear;
close;
clc
x=input('Enter subject: 1 for Elisabetta, 2 for Marco, 3 for Giammarco \n');
%% INPUTS
%Defining the windown for epoching
%--Baseline
TimeBeforeEventBaseline=-3;
TimeAfterEventBaseline=3;
%--MI
TimeBeforeEventMI=-2;
TimeAfterEventMI=6;
%--MI termination
TimeBeforeEventMItermination=-3;
TimeAfterEventMItermination=3;
%--Cyclic frequency for spectrogram
Cyclic_freq=[5:0.1:40]; %inital:resolution:final
%% Loading paths and files
addpath(genpath('biosig'));
addpath(genpath('folder_runs'));
addpath(genpath('data'));
addpath(genpath('eeglab13_4_4b'));
addpath(genpath('folder_runs_ak6'));
addpath(genpath('folder_runs_ak5_Giammarco'));
addpath(genpath('mi614'));
load('channel_location_16_10-20_mi');
FilesEli={'ak6_run1_offlineMIterm_20181603155146.gdf', 'ak6_run2_offlineMIterm_20181603160414.gdf', 'ak6_run3_offlineMIterm_20181603162521.gdf','ak6_run4_offlineMIterm_20181603164052.gdf'};
FilesMarco={'ak4_run3.onlineMIterm.20182304112221.gdf', 'ak4_run3_offlineMIterm_20182304105005.gdf', 'ak4_run2.onlineMIterm.20182304110827.gdf'};
FilesGiam={'ak5_run1_offlineMIterm_20182003154933.gdf', 'ak5_run2_offlineMIterm_20182003160110.gdf','ak5_run3_offlineMIterm_20182003163100.gdf', 'ak5_run4_offlineMIterm_20182003164443.gdf'};
if x==1
FilesLength=4;
Files=FilesEli;
elseif x==3
FilesLength=4;
Files=FilesGiam;
elseif x==2
FilesLength=3;
Files=FilesMarco;
else
disp('Error in files assignment');
end
for j=1:FilesLength
filename = (Files{j});
[s, h]= sload(filename);
% Creating the main structure from data
session.fs=h.SampleRate;
session.data=(s)';
session.channels={chanlocs16.labels};
session.Event_type=h.EVENT.TYP;
session.Event_pos=h.EVENT.POS;
% spatial filtering on the raw data.CAR
medium_channels=mean(s');
signal_car=zeros(size(s,1),size(s,2));
for i=1:size(s,1)
signal_car(i,:)=s(i,:)-medium_channels(1,i);
end
session_filt_CAR=session;
session_filt_CAR.data=signal_car';
%% laplacian filter
load('laplacian_16_10-20_mi.mat');
signal_laplacian = s(:,1:16)*lap;
session_filt_lap=session;
session_filt_lap.data=signal_laplacian';
filt_epoch_baseline_lap=epoch_struct(session_filt_lap,200,TimeBeforeEventBaseline,TimeAfterEventBaseline);
filt_epoch_MI_lap=epoch_struct(session_filt_lap,400,TimeBeforeEventMI,TimeAfterEventMI);
filt_epoch_MI_lap_termination=epoch_struct(session_filt_lap,555,TimeBeforeEventMItermination,TimeAfterEventMItermination);
%% p_welch on car data
filt_epoch_baseline_CAR=epoch_struct(session_filt_CAR,200,TimeBeforeEventBaseline,TimeAfterEventBaseline);
filt_epoch_MI_CAR=epoch_struct(session_filt_CAR,400,TimeBeforeEventMI,TimeAfterEventMI);
filt_epoch_MI_CAR_termination=epoch_struct(session_filt_CAR,555,TimeBeforeEventMItermination,TimeAfterEventMItermination);
for i=1:size(chanlocs16,2)
[pwelch_car_bas_onechannel{i},freq_1]=pwelch_for_each_channel(i,filt_epoch_baseline_CAR,500,session_filt_CAR.fs);
[pwelch_car_MI_onechannel{i},freq_2]=pwelch_for_each_channel(i,filt_epoch_MI_CAR_termination,500,session_filt_CAR.fs);
if j==1
if i==7
subplot(2,3,1)
plot(freq_1(1:55),10*log10(pwelch_car_bas_onechannel{i}(1:55)),freq_2(1:55),10*log10(pwelch_car_MI_onechannel{i}(1:55)))
xlabel('Frequency [Hz]');
ylabel('PSD [dB]');
title(sprintf('SPD comparison for channels %s',filt_epoch_baseline_CAR.channels{1,i}));
legend('baseline','MI');
end
if i==11
subplot(2,3,3)
plot(freq_1(1:55),10*log10(pwelch_car_bas_onechannel{i}(1:55)),freq_2(1:55),10*log10(pwelch_car_MI_onechannel{i}(1:55)))
xlabel('Frequency [Hz]');
ylabel('PSD [dB]');
title(sprintf('SPD comparison for channels %s',filt_epoch_baseline_CAR.channels{1,i}));
legend('baseline','MI');
end
if i==12
subplot(2,3,4)
plot(freq_1(1:55),10*log10(pwelch_car_bas_onechannel{i}(1:55)),freq_2(1:55),10*log10(pwelch_car_MI_onechannel{i}(1:55)))
xlabel('Frequency [Hz]');
ylabel('PSD [dB]');
title(sprintf('SPD comparison for channels %s',filt_epoch_baseline_CAR.channels{1,i}));
legend('baseline','MI');
end
if i==16
subplot(2,3,6)
plot(freq_1(1:55),10*log10(pwelch_car_bas_onechannel{i}(1:55)),freq_2(1:55),10*log10(pwelch_car_MI_onechannel{i}(1:55)))
xlabel('Frequency [Hz]');
ylabel('PSD [dB]');
title(sprintf('SPD comparison for channels %s',filt_epoch_baseline_CAR.channels{1,i}));
legend('baseline','MI');
end
if i==9
subplot(2,3,2)
plot(freq_1(1:55),10*log10(pwelch_car_bas_onechannel{i}(1:55)),freq_2(1:55),10*log10(pwelch_car_MI_onechannel{i}(1:55)))
xlabel('Frequency [Hz]');
ylabel('PSD [dB]');
title(sprintf('SPD comparison for channels %s',filt_epoch_baseline_CAR.channels{1,i}));
legend('baseline','MI');
end
end
end
% Spectrogram CAR MI termination
for i=1:16
%channels={'FZ','FC3','FC1','FCz','FC2','FC4','C3','C1', 'Cz', 'C2', 'C4' ,'CP3' ,'CP1' ,'CPZ', 'CP2' ,'CP4'};
[spect_for_one_channel,t, f]=Spectrogram_function(filt_epoch_baseline_CAR, filt_epoch_MI_CAR, i, filt_epoch_baseline_CAR.fs, filt_epoch_baseline_CAR.fs-32, Cyclic_freq);
if j==1
Matrix.(chanlocs16(i).labels)(1:size(spect_for_one_channel,1), 1:size(spect_for_one_channel,2))=0;
end
if i==1
time=t;
end
Matrix.(chanlocs16(i).labels)=Matrix.(chanlocs16(i).labels) + spect_for_one_channel;
flag=flag+1;
end
end
%% saving for the grand average
if x==1
Spectrogram_Eli=Matrix;
save('Spectrogram_Eli.mat', 'Spectrogram_Eli');
elseif x==2
Spectrogram_Marco=Matrix;
save('Spectrogram_Marco.mat', 'Spectrogram_Marco');
elseif x==3
Spectrogram_Giamm=Matrix;
save('Spectrogram_Giamm.mat', 'Spectrogram_Giamm');
end
%% only plotting the good stuff
figure;
for i=1:16
if i==7
Matrix.(chanlocs16(i).labels)=Matrix.(chanlocs16(i).labels)./size(Files,2);
subplot(2,3,1);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(Matrix.(chanlocs16(i).labels)));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.chanlocs16(i).labels{1,i})));
end
if i==11
Matrix.(chanlocs16(i).labels)=Matrix.(chanlocs16(i).labels)./size(Files,2);
subplot(2,3,3);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(Matrix.(chanlocs16(i).labels)));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
if i==12
Matrix.(chanlocs16(i).labels)=Matrix.(chanlocs16(i).labels)./size(Files,2);
subplot(2,3,4);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(Matrix.(channels{i})));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
if i==16
Matrix.(channels(i,:))=Matrix.(channels(i,:))./size(Files,2);
subplot(2,3,6);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(Matrix.(channels(i,:))));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
if i==9
Matrix.(channels(i,:))=Matrix.(channels(i,:))./size(Files,2);
subplot(2,3,2);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(Matrix.(channels(i,:))));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
end
% suptitle('Spectrogram offset CAR - Elisabetta');
%% grand average
load('Spectrogram_Eli.mat');
load('Spectrogram_Marco.mat');
load('Spectrogram_Giamm.mat');
Cz_avg=(Spectrogram_Eli.Cz./4+Spectrogram_Marco.Cz./3+Spectrogram_Giamm.Cz./4)./3;
C3_avg=(Spectrogram_Eli.C3./4+Spectrogram_Marco.C3./3+Spectrogram_Giamm.C3./4)./3;
C4_avg=(Spectrogram_Eli.C4./4+Spectrogram_Marco.C4./3+Spectrogram_Giamm.C4./4)./3;
CP3_avg=(Spectrogram_Eli.CP3./4+Spectrogram_Marco.CP3./3+Spectrogram_Giamm.CP3./4)./3;
CP4_avg=(Spectrogram_Eli.CP4./4+Spectrogram_Marco.CP4./3+Spectrogram_Giamm.CP4./4)./3;
%% plotting the good stuff
channels={'FZ','FC3','FC1','FCz','FC2','FC4','C3','C1', 'Cz', 'C2', 'C4' ,'CP3' ,'CP1' ,'CPZ', 'CP2' ,'CP4'};
figure;
for i=1:16
if i==7
subplot(2,3,1);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(C3_avg));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
if i==11
subplot(2,3,3);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(C4_avg));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
if i==12
subplot(2,3,4);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(CP3_avg));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
if i==16
subplot(2,3,6);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(CP4_avg));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
if i==9
subplot(2,3,2);
time=t(1,:)+ filt_epoch_MI_CAR.time;
imagesc('XData',time,'YData',f,'CData', 10*log(Cz_avg));
axis tight;
colorbar;
caxis([-5 5]);
colormap('jet');
xlabel('time[s]');
ylabel('frequency[Hz]');
title((sprintf('Channel %s',filt_epoch_baseline_CAR.channels{1,i})));
end
end
% suptitle('Spectrogram offset CAR - Elisabetta');