-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMS_coherogram_fig.m
More file actions
352 lines (311 loc) · 14.4 KB
/
Copy pathMS_coherogram_fig.m
File metadata and controls
352 lines (311 loc) · 14.4 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
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
function MS_coherogram_fig(cfg_in, data)
%% MS_coherogram_fig: conputes a coherogram for two inputs across time.
% This makes use of the Chronux Toolbox (v2.11, a the time of writing this
% function).
%
%
% Inputs:
% -
% -
% -
% Outputs:
% -
% -
% -
%
% EC - 2017-01-08
%% set up variables
cfg_def = [];
cfg_def.linewidth = 3;
cfg_def.c_ord = linspecer(4);
cfg_def.Fs=2000; % sampling frequency
cfg_def.fpass=[0 100]; % frequencies of interest
cfg_def.tapers=[5 9]; % tapers
cfg_def.trialave=0; % average over trials
cfg_def.err=0; % no error computation
cfg_def.pad = -1;
cfg_def.pre_x_lim = [338 339];
cfg_def.ipsi_x_lim = [854 855];
movingwin=[1 .05]; % set the moving window dimensions
cfg = ProcessConfig2(cfg_def, cfg_in);
c_ord = linspecer(4);
global PARAMS
mkdir(PARAMS.inter_dir, 'Coherogram')
if isunix
save_dir = [PARAMS.inter_dir, 'Coherogram/'];
else
save_dir = [PARAMS.inter_dir, 'Coherogram\'];
end
addpath(genpath(PARAMS.Chronux_dir))
%% append the data
sites = fieldnames(data.pre);
for iSite =1:length(sites)
all_data = [];
if strcmp(sites{iSite}, 'ExpKeys') || strcmp(sites{iSite}, 'pos')
continue
else
%% remove the high amplitude and chewing artifacts
for ii = 1:4
csc_artif = data.(PARAMS.Phases{ii}).(sites{iSite});
csc_artif.data = abs(csc_artif.data); % detect artifacts both ways
cfg_artif_det = [];
cfg_artif_det.method = 'raw';
cfg_artif_det.threshold = std(csc_artif.data)*7;
cfg_artif_det.minlen = 0;
evt_artif = TSDtoIV(cfg_artif_det,csc_artif);
cfg_temp = []; cfg_temp.d = [-0.05 0.05];
evt_artif = ResizeIV(cfg_temp,evt_artif);
artif_idx = TSD_getidx2(data.(PARAMS.Phases{ii}).(sites{iSite}),evt_artif); % if error, try TSD_getidx (slower)
data.(PARAMS.Phases{ii}).(sites{iSite}).data(artif_idx) = NaN;
end
%%
all_data.hdr.Fs = data.pre.(sites{iSite}).cfg.hdr{1}.SamplingFrequency;
[x,y] = size(data.ipsi.(sites{iSite}).data);
if x>y
all_data.data = [data.pre.(sites{iSite}).data ; data.ipsi.(sites{iSite}).data ; data.contra.(sites{iSite}).data; data.post.(sites{iSite}).data];
else
all_data.data = [data.pre.(sites{iSite}).data , data.ipsi.(sites{iSite}).data , data.contra.(sites{iSite}).data, data.post.(sites{iSite}).data];
end
all_data.tvec = [data.pre.(sites{iSite}).tvec', data.ipsi.(sites{iSite}).tvec', data.contra.(sites{iSite}).tvec',data.post.(sites{iSite}).tvec'];
pre_norm = (data.pre.(sites{iSite}).tvec - data.pre.(sites{iSite}).tvec(1))';
ipsi_norm = ((data.ipsi.(sites{iSite}).tvec - data.ipsi.(sites{iSite}).tvec(1))+(pre_norm(end)+(pre_norm(end)-pre_norm(end-1))))';
contra_norm = ((data.contra.(sites{iSite}).tvec - data.contra.(sites{iSite}).tvec(1))+(ipsi_norm(end)+(ipsi_norm(end)-ipsi_norm(end-1))))';
post_norm = ((data.post.(sites{iSite}).tvec - data.post.(sites{iSite}).tvec(1))+(contra_norm(end)+(contra_norm(end)-contra_norm(end-1))))';
all_data.tvec_norm = [pre_norm, ipsi_norm, contra_norm, post_norm];
all_data.type = 'AMPX';
all_data.labels = sites{iSite};
Sites.(sites{iSite}) = all_data;
end
end
clear csc_artif
%% loop through the pairs of good cites to make the coherograms
for iPair =1:length(data.pre.ExpKeys.GoodPairs)
S =strsplit(data.pre.ExpKeys.GoodPairs{iPair}, '_');
if strcmp(S{1}, 'PiriO')
S{1} = 'Piri_O';
end
if strcmp(S{2}, 'PiriO')
S{2} = 'Piri_O';
end
if strcmp(S{1}, 'PiriN')
S{1} = 'Piri_N';
end
if strcmp(S{2}, 'PiriN')
S{2} = 'Piri_N';
end
S{1} = [S{1} '_pot'];
S{2} = [S{2} '_pot'];
D1 = Sites.(S{1});
D2 = Sites.(S{2});
% get the time range for each naris phase for plotting the color bars.
% there is probably a more computationally frendy way to do this...
[~,~,~,~,~,Tp,~]=cohgramc(data.pre.(S{1}).data',data.pre.(S{2}).data',movingwin, cfg);
[~,~,~,~,~,Ti,~]=cohgramc(data.ipsi.(S{1}).data',data.ipsi.(S{2}).data',movingwin, cfg);
[~,~,~,~,~,Tc,~]=cohgramc(data.contra.(S{1}).data',data.contra.(S{2}).data',movingwin, cfg);
[~,~,~,~,~,Tpo,~]=cohgramc(data.post.(S{1}).data',data.post.(S{2}).data',movingwin, cfg);
% CC = [Cp; Ci; Cc; Cpo];
% ff = [fp fi fc fpo];
% tt = [Tp Ti Tc Tpo];
[C,~,~,~,~,t,f]=cohgramc(D1.data',D2.data',movingwin, cfg);
%%
figure(1)
ax1 =imagesc(t,f,C'); axis xy
set(ax1, 'AlphaData', ~isnan(C'))
% title(['Coherogram: ' S{1} ' ' S{2}] )
xlabel('time (s)')
ylabel('frequency')
% caxis([0.2 1])
set(gca,'FontSize',20);
xlabel('Time (s)'); ylabel('Frequency (Hz)');
% add lines to distinguish phases
ylim([-6 100]);
hold on
rectangle('position', [Tp(1) -6 (Tp(end)-Tp(1)) 5.5], 'facecolor',cfg.c_ord(1,:), 'edgecolor', cfg.c_ord(1,:))
rectangle('position', [Tp(end) -6 ((Tp(end)+Ti(end))-Tp(end)) 5.5], 'facecolor',cfg.c_ord(2,:), 'edgecolor', cfg.c_ord(2,:))
rectangle('position', [(Tp(end)+Ti(end)) -6 ((Tp(end)+Ti(end)+Tc(end))-(Tp(end)+Ti(end))) 5.5], 'facecolor',cfg.c_ord(3,:), 'edgecolor', cfg.c_ord(3,:))
rectangle('position', [(Tp(end)+Ti(end)+Tc(end)) -6 ((Tp(end)+Ti(end)+Tc(end)+Tpo(end))-(Tp(end)+Ti(end)+Tc(end))) 5.5], 'facecolor',cfg.c_ord(4,:), 'edgecolor', cfg.c_ord(4,:))
text((Tp(end)-Tp(1))/2, -3.5, 'pre', 'fontsize', 20, 'horizontalAlignment', 'center')
text(median(Tp(end):(Tp(end)+Ti(end))), -3.5, 'ipsi', 'fontsize', 20, 'horizontalAlignment', 'center')
text(median((Tp(end)+Ti(end)):(Tp(end)+Ti(end)+Tc(end))), -3.5, 'contra', 'fontsize', 20, 'horizontalAlignment', 'center')
text(median((Tp(end)+Ti(end)+Tc(end)):(Tp(end)+Ti(end)+Tc(end)+Tpo(end))), -3.5, 'post', 'fontsize', 20, 'horizontalAlignment', 'center')
% plot(Tp(1):Tp(end), zeros(length(Tp(1):Tp(end)),1)-off_set,'color', cfg.c_ord(1,:), 'linewidth', 1); % pre
% % plot(Tp(end):(Tp(end)+Ti(end)), zeros(length(Tp(end):(Tp(end)+Ti(end))),1)-off_set,'color', cfg.c_ord(2,:), 'linewidth', 10); % ipsi
% plot((Tp(end)+Ti(end)):(Tp(end)+Ti(end)+Tc(end)), zeros(length((Tp(end)+Ti(end)):(Tp(end)+Ti(end)+Tc(end))),1)-off_set,'color', cfg.c_ord(3,:), 'linewidth', 10); % contra
% plot((Tp(end)+Ti(end)+Tc(end)):(Tp(end)+Ti(end)+Tc(end)+Tpo(end)), zeros(length((Tp(end)+Ti(end)+Tc(end)):(Tp(end)+Ti(end)+Tc(end)+Tpo(end))),1)-off_set,'color', cfg.c_ord(4,:), 'linewidth', 10); % post
h = vline([Tp(end),(Tp(end)+Ti(end)),(Tp(end)+Ti(end)+Tc(end))], {'k', 'k', 'k'} );
% if cfg.lfp_on
% plot(D1.tvec_norm,(D1.data*10000)+20, 'color', [1 1 1])
% plot(D2.tvec_norm, (D2.data*10000)+20, 'color', [0.5 1 1])
% end
set(h(:), 'linewidth', cfg.linewidth);
colormap('PARULA');
% drawArrow = @(x,y,L) quiver( x(1),y(1),x(2)-x(1),y(2)-y(1),0, 'LineWidth', L); % make an arrow. Matlab builtin function is just terrible.
% drawArrow([250 254], [0 10], 5)
text(median(cfg.pre_x_lim), 102, 'V', 'fontsize', 28, 'color', c_ord(1,:))
text(median(cfg.ipsi_x_lim), 102, 'V', 'fontsize', 28, 'Color', c_ord(2,:))
% tightfig
cfg_fig = [];
cfg_fig.ft_size =30;
SetFigure(cfg_fig,gcf);
hc = colorbar;
% caxis(hc, [0 1])
% set(hc,'Ticks',[0 1], 'Tickslabels', [0 1])
set(hc, 'Limits', [0 1], 'Ticks', [0 1])
set(gcf, 'position', [0 50 1800*.9 480*.9]);
% tightfig;
% ax =gca;
% ax.Clipping = 'off';
% sess = strrep(data.pre.ExpKeys.date, '-', '_');
if isfield(data.pre.ExpKeys, 'Subject')
sess = [data.contra.ExpKeys.Subject '_' strrep(data.contra.ExpKeys.date, '-', '-')];
subject = data.contra.ExpKeys.Subject;
else
sess = [data.contra.ExpKeys.ratID '_' strrep(data.contra.ExpKeys.date, '-', '-')];
subject = data.contra.ExpKeys.ratID;
end
sess = strrep(sess, '-', '_');
saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_Coherogram'], 'png')
saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_Coherogram'], 'fig')
h1 = get(gcf);
D = h1.PaperPosition; % Returns 1x4 vector [left right width height]
set(gcf, 'PaperSize', [D(3) D(4)]); %default PaperSize is [8.5 11]
% saveas(gcf, [save_dir subject '_' sess '_' sites{iSite} '_Spectrogram'], 'epsc')
fname = [sess '_' S{1} '_' S{2} '_Coherogram'];
pushdir(save_dir)
eval(sprintf('print -depsc2 -tiff -r300 -painters %s',fname));
popdir
close all
%% get a zoom in
movingwin = [0.5 0.05];
[C,~,~,~,~,t,f]=cohgramc(D1.data(1:floor(.5*length(D1.data)))',D2.data(1:floor(.5*length(D2.data)))',movingwin, cfg);
figure(111)
subplot(2,1,1)
ax1 =imagesc(t,f,C'); axis xy
set(ax1, 'AlphaData', ~isnan(C'))
xlim(cfg.pre_x_lim)
set(gca, 'xtick', [cfg.pre_x_lim(1):0.5:cfg.pre_x_lim(2)], 'xticklabel', [])
subplot(2,1,2)
hold on
plot(D1.tvec_norm(1:floor(.5*length(D1.tvec_norm))),D1.data(1:floor(.5*length(D1.data)))+0.00050, 'color', [0.4 0.4 0.4],'Linewidth', 2)
plot(D2.tvec_norm(1:floor(.5*length(D2.tvec_norm))), D2.data(1:floor(.5*length(D2.data))), 'color', [0.6 0.6 0.6], 'Linewidth', 2)
% [axL, icons] = legend(S, 'box', 'off', 'fontsize', 18, 'location');
% axL.FontSize = 30;
% set(icons, 'LineWidth', 2)
cfg_fig = [];
cfg_fig.ft_size =36;
xlim(cfg.pre_x_lim)
set(gca, 'xtick', [cfg.pre_x_lim(1):0.5:cfg.pre_x_lim(2)], 'ytick', [])
SetFigure(cfg_fig,gcf);
tightfig
saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_pre_sample'], 'png')
saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_pre_sample'], 'fig')
h1 = get(gcf);
D = h1.PaperPosition; % Returns 1x4 vector [left right width height]
set(gcf, 'PaperSize', [D(3) D(4)]); %default PaperSize is [8.5 11]
% saveas(gcf, [save_dir subject '_' sess '_' sites{iSite} '_Spectrogram'], 'epsc')
fname = [sess '_' S{1} '_' S{2} '_pre_sample'];
pushdir(save_dir);
eval(sprintf('print -depsc2 -tiff -r300 -painters %s',fname));
popdir;
close all
figure(112)
subplot(2,1,1)
ax1 =imagesc(t,f,C'); axis xy
set(ax1, 'AlphaData', ~isnan(C'))
xlim(cfg.ipsi_x_lim)
set(gca, 'xtick', [cfg.ipsi_x_lim(1):0.5:cfg.ipsi_x_lim(2)], 'xticklabel', [])
subplot(2,1,2)
hold on
plot(D1.tvec_norm,(D1.data)+0.00050, 'color', [0.4 0.4 0.4],'Linewidth', 2)
plot(D2.tvec_norm, D2.data, 'color', [0.6 0.6 0.6], 'Linewidth', 2)
xlim(cfg.ipsi_x_lim)
set(gca, 'xtick', [cfg.ipsi_x_lim(1):0.5:cfg.ipsi_x_lim(2)], 'ytick', [])
% [axL, icons] = legend(S, 'box', 'off', 'fontsize', 12);
% axL.FontSize = 30;
% set(icons, 'LineWidth', 2)
line([cfg.ipsi_x_lim(1)-0.01 cfg.ipsi_x_lim(1)-0.01],[0 0.0002],'color', 'k', 'linewidth', 0.5)
box off
cfg_fig = [];
cfg_fig.ft_size =36;
SetFigure(cfg_fig,gcf);
tightfig
saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_ipsi_sample'], 'png');
saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_ipsi_sample'], 'fig')
h1 = get(gcf);
D = h1.PaperPosition; % Returns 1x4 vector [left right width height]
set(gcf, 'PaperSize', [D(3) D(4)]); %default PaperSize is [8.5 11]
% saveas(gcf, [save_dir subject '_' sess '_' sites{iSite} '_Spectrogram'], 'epsc')
fname = [sess '_' S{1} '_' S{2} '_ipsi_sample'];
pushdir(save_dir);
eval(sprintf('print -depsc2 -tiff -r300 -painters %s',fname));
popdir;
close all
% figure(111)
% subplot(1,2,2)
% ax1 =imagesc(t,f,C'); axis xy
% xlim([254 255])
%
% subplot(3,2,4:5)
% hold on
% plot(D1.tvec_norm,(D1.data)+0.00020, 'color', [0.4 0.4 0.4])
% plot(D2.tvec_norm, D2.data, 'color', [0.6 0.6 0.6])
% xlim([254 255])
clearvars('C', 'D1', 'D2');
end
%%
% xlim([0 100])
% %% make a spectrogram of the combined data
% [~,Fp,Tp,Pp] = spectrogram(data.pre.(sites{iSite}).data,rectwin(cfg.win),cfg.noverlap,1:120,all_data.hdr.Fs);
% [~,Fi,Ti,Pi] = spectrogram(data.ipsi.(sites{iSite}).data,rectwin(cfg.win),cfg.noverlap,1:120,all_data.hdr.Fs);
% [~,Fc,Tc,Pc] = spectrogram(data.contra.(sites{iSite}).data,rectwin(cfg.win),cfg.noverlap,1:120,all_data.hdr.Fs);
% [~,Fpo,Tpo,Ppo] = spectrogram(data.post.(sites{iSite}).data,rectwin(cfg.win),cfg.noverlap,1:120,all_data.hdr.Fs);
%
%
%
% [~,F,T,P] = spectrogram(all_data.data,rectwin(cfg.win),cfg.noverlap,1:120,all_data.hdr.Fs);
% figure(100);
% P(P==inf) = NaN;
% imagesc(T,F,10*log10(P)); % converting to dB as usual
%
% caxis([-150 -80])
% h = vline([Tp(end),(Tp(end)+Ti(end)),(Tp(end)+Ti(end)+Tc(end))], {'k', 'k', 'k'} );
% % add lines to distinguish phases
% ylim([0 120]);
% hold on
% plot(0:Tp(end), zeros(length(0:Tp(end)),1),'color', cfg.c_ord(1,:), 'linewidth', 5); % pre
% plot(Tp(end):(Tp(end)+Ti(end)), zeros(length(Tp(end):(Tp(end)+Ti(end))),1),'color', cfg.c_ord(2,:), 'linewidth', 5); % ipsi
% plot((Tp(end)+Ti(end)):(Tp(end)+Ti(end)+Tc(end)), zeros(length((Tp(end)+Ti(end)):(Tp(end)+Ti(end)+Tc(end))),1),'color', cfg.c_ord(3,:), 'linewidth', 5); % contra
% plot((Tp(end)+Ti(end)+Tc(end)):(Tp(end)+Ti(end)+Tc(end)+Tpo(end)), zeros(length((Tp(end)+Ti(end)+Tc(end)):(Tp(end)+Ti(end)+Tc(end)+Tpo(end))),1),'color', cfg.c_ord(4,:), 'linewidth', 5); % post
%
% set(h(:), 'linewidth', cfg.linewidth);
% colormap('PARULA');
%
% SetFigure([],gcf);
% set(gcf, 'position', [0 50 1600*.9 420*.9]);
% tightfig;
% sess = strrep(data.pre.ExpKeys.date, '-', '_');
% if isfield(data.pre.ExpKeys, 'Subject')
% sess = [data.contra.ExpKeys.Subject '_' strrep(data.contra.ExpKeys.date, '-', '-')];
% subject = data.contra.ExpKeys.Subject;
% else
% sess = [data.contra.ExpKeys.ratID '_' strrep(data.contra.ExpKeys.date, '-', '-')];
% subject = data.contra.ExpKeys.ratID;
% end
% %% save the session wide spectrogram
% saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_Spectrogram'], 'png')
% saveas(gcf, [save_dir sess '_' S{1} '_' S{2} '_Spectrogram'], 'fig')
% h = get(gcf);
% D = h.PaperPosition; % Returns 1x4 vector [left right width height]
% set(gcf, 'PaperSize', [D(3) D(4)]); %default PaperSize is [8.5 11]
% % saveas(gcf, [save_dir subject '_' sess '_' sites{iSite} '_Spectrogram'], 'epsc')
% fname = [subject '_' sess '_' sites{iSite} '_Spectrogram'];
% pushdir(save_dir)
% eval(sprintf('print -depsc2 -tiff -r300 -painters %s',fname));
% popdir
% % print -depsc2 -tiff -r300 -painters test.eps
%
%
% end
% close all
% end
% end