-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotSpecGram.m
213 lines (175 loc) · 6.16 KB
/
plotSpecGram.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
function [ax,F] = plotSpecGram(volPsd,timeLabel,metricLabel,H,volTs,respQthresh)
if ~exist('H','var'); H = []; end
if isempty(H); H = figure('WindowStyle','docked'); end
if isfield(volPsd,'dsgn') && isfield(volPsd.dsgn,'f0'); f0 = volPsd.dsgn.f0; else; f0 = []; end
if ~exist('timeLabel','var'); timeLabel = []; end
if ~exist('metricLabel','var'); metricLabel = []; end
if ~exist('volTs','var'); volTs = []; end
if ~exist('respQthresh','var'); respQthresh = []; end
if isempty(timeLabel); timeLabel = 'gram'; end % 'gram' 'trialGram' 'trialGramMD'
if isempty(metricLabel); metricLabel = 'psd'; end % 'psd' 'psdEPC' 'coh' 'cohEPC' 'cohEK'
if isempty(respQthresh); respQthresh = 0.05; end % 'psd' 'coh'
threshAvFlag = ismember(metricLabel,{'psd' 'psdEPC'}) && ~isempty(volTs) && isfield(volTs,'resp') && ~isempty(respQthresh) && respQthresh~=inf && respQthresh~=1;
switch class(H)
case 'matlab.graphics.layout.TiledChartLayout'
F = H.Parent;
case 'matlab.ui.Figure'
F = H;
otherwise
end
figure(F);
ax = {};
ax{end+1} = nexttile;
%% Select approrpiate data
switch timeLabel
case 'gram'
switch metricLabel
case 'psd'
mt = volPsd.psdGram;
vec = mt.PSD;
label = 'spectrogram';
case 'coh'
mt = volPsd.svdGram;
vec = mt.COH;
label = 'coherogram';
otherwise
dbstack; error('code trhat')
end
case 'trialGram'
% case 'av'
% title(['evoked spectrogram ' paramStr])
% case 'pc'
% title(['phase-locked evoked spectrogram ' paramStr])
% case 'MD'
% title(['discontinuous-taper evoked spectrogram ' paramStr])
% case 'MDpc'
% title(['discontinuous-taper phase-locked evoked spectrogram ' paramStr])
switch metricLabel
case 'psd'
mt = volPsd.psdTrialGram;
vec = mt.vec.psd;
label = 'evoked spectrogram';
case 'psdEPC'
mt = volPsd.psdTrialGram;
vec = mt.vec.psdPC;
label = 'phase-locked evoked spectrogram';
case 'coh'
mt = volPsd.svdTrialGram;
vec = mt.vec.coh;
label = 'evoked coherogram';
case 'cohEPC'
mt = volPsd.svdTrialGram;
vec = mt.vec.cohEPC;
label = 'phase-locked evoked coherogram';
case 'cohEK'
mt = volPsd.svdTrialGram;
vec = mt.vec.cohEK;
label = 'evoked cross-trial coherogram';
otherwise
dbstack; error('code trhat')
end
case 'trialGramMD'
switch metricLabel
case 'psd'
mt = volPsd.psdTrialGramMD;
vec = mt.vec.psd;
label = 'discontinuous-taper evoked spectrogram';
case 'psdEPC'
mt = volPsd.psdTrialGramMD;
vec = mt.vec.psdPC;
label = 'discontinuous-taper phase-locked evoked spectrogram';
case 'coh'
mt = volPsd.svdTrialGramMD;
vec = mt.vec.coh;
label = 'discontinuous-taper evoked coherogram';
case 'cohEPC'
mt = volPsd.svdTrialGramMD;
vec = mt.vec.cohEPC;
label = 'discontinuous-taper phase-locked evoked coherogram';
otherwise
dbstack; error('code trhat')
end
otherwise
dbstack; error('code trhat')
end
%% spatial average
if threshAvFlag
vec = permute(mean(vec(:,:,:,:,:,volTs.resp.Fq.vol(volPsd.vol2vec)<=respQthresh,:,1),6),[5 7 2 8 1 3 4 6]);
else
vec = permute(mean(vec(:,:,:,:,:,:,:,1),6),[5 7 2 8 1 3 4 6]);
end
f = permute(mt.f ,[5 7 2 8 1 3 4 6]);
Fs = mt.param.Fs;
t = permute(mean(mt.t(:,1,:,:,:,:,:,1),1),[5 7 2 8 1 3 4 6]);
K = mt.K;
T = mt.T;
[TW,W] = K2W(T,K,0);
imagesc(t,f,vec)
xlabel('time (s)')
ylabel('freq (Hz)')
switch metricLabel
case {'psd' 'psdEPC'}
ylabel(colorbar,'psd');
ax{end}.ColorScale = 'log';
case {'coh' 'cohEPC' 'cohEK'}
ylabel(colorbar,'coherence');
otherwise
dbstack; error('code trhat')
end
% if contains(timeLabel,'trial')
% mt.t(:,1,:,:,:,:,:)
% else
runDur = volPsd.t(end);
% end
xlim([0 runDur])
paramStr = ['(K=' num2str(K) '; 2W=' num2str(W*2,'%0.4f') 'Hz; T=' num2str(T,'%0.2f') 'sec; TW=' num2str(TW) ')'];
if threshAvFlag
title([label ' ' paramStr ' averaged across voxels with Q<=' num2str(respQthresh,'%0.2f')])
else
title([label ' ' paramStr])
end
cLim = vec(f>0.01,:);
cLim = [min(cLim(:)) max(cLim(:))];
clim(cLim)
addWin([],mt)
addW([],mt)
if isfield(volPsd,'psdTrialGram') && ~isempty(volPsd.psdTrialGram)
addFreq([],volPsd.psdTrialGram.onsetList,volPsd.psdTrialGram.durList,1)
end
if ~isempty(f0)
yline(f0,'--b')
end
ax = [ax{:}];
%% %%%%%%%%%%%%%%%%%%
% Add to timeseries %
%%%%%%%%%%%%%%%%%% %%
axTs = findobj(allchild(F.Children),'type','axes'); ttl = get(axTs,'Title'); ttl = get([ttl{:}],'String');
axTs = axTs(contains(ttl,'timeseries'));
%%% delete previous window size visual elements (magentat lines)
hLine = findobj(axTs.Children,'type','Line'); %hLine = {hLine(:)};
mLine = get(hLine,'Color'); if iscell(mLine); mLine = cat(1,mLine{:}); end
mLine = all(cat(1,mLine)==[1 0 1],2);
delete(hLine(mLine));
% % if length(hLine)==1
% % hLine = {hLine};
% % end
% %
% % mLine = get(hLine,'Color');
% % else
% % mLine = {get([axTs.Children(:)],'Color')};
% % end
% mLine = get(hLine,'Color'); if ~iscell(mLine); mLine = {mLine}; end
% ind = all(cat(1,mLine{:})==[1 0 1],2);
% mLine(ind)
%
% mLine = hLine(all(cat(1,mLine{:})==[1 0 1],2));
% delete(mLine);
% % if length(axTs.Children)>1
% % mLine = get(hLine,'Color');
% % else
% % mLine = {get([axTs.Children(:)],'Color')};
% % end
% % mLine = axTs.Children(all(cat(1,mLine{:})==[1 0 1],2));
% % delete(mLine);
%%% add window size
addWin(axTs,mt)