-
Notifications
You must be signed in to change notification settings - Fork 0
/
hranPhysio3.m
217 lines (193 loc) · 6.13 KB
/
hranPhysio3.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
function [svdTs,svdStruct] = hranPhysio3(funTs,fRange,modeOrder,K)
% addpath(genpath('/autofs/space/takoyaki_001/users/proulxs/tools/HRAN'))
if ~exist('K','var'); K = 1; end
if ~exist('modeOrder','var'); modeOrder = 1; end
% W=[];
% cFlag = 0;
% funPsd = runPSD(funTs,W,K,cFlag);
%
% peakFreq = fRange(:,1) + diff(fRange,[],2)/2;
% hF = viewPSD2(funPsd,[peakFreq(:)' fRange(:)'],[],[],funTs.id);
%% Perform MTsvd
anaType = 'svdProulx';
svdStruct = runMTsvd2(anaType,funTs,fRange);
%% Reconstruct timeseries of the first component
sp = svdStruct.sp(:,:,modeOrder); % spatial sv (space x 1 x mode)
fm = svdStruct.fm(:,:,modeOrder); % taper sv (taper x 1 x mode)
tp = svdStruct.MTS.proj; % tapers (taper x time)
f = svdStruct.MTS.bandFreq; % frequencies (1 x freq)
fInd = svdStruct.MTS.bandInd; % frequency indices (taper x 1)
% whos sp fm tp f fInd
tsRec = fm'*tp; % WARNING! taking the conjugate here with the ' operator
%% Inspect original and reconstructed timeseries
W=[];
cFlag = 1;
funPsd = runPSD(funTs,W,K,cFlag); funPsd = vec2vol(funPsd);
% svdTs = funTs; svdTs.vol = permute(conj(tsRec).*tsRec,[1 3 4 2]);
svdTs = funTs; svdTs.vol = permute(tsRec,[1 3 4 2]);
svdTs.volsize([1 2]) = 1; svdTs.height = 1; svdTs.width = 1; svdTs.nvoxels = 1;
svdPsd = runPSD(svdTs,W,K,cFlag);
%% Plot
hF = figure('WindowStyle','docked');
tl = tiledlayout(6,5);
tl.TileSpacing = "tight"; tl.Padding = "tight";
tl.TileIndexing = 'rowmajor';
%%% mean brain
tileInd = 1;
nexttile(tileInd,[2 1])
if isfield(funPsd,'tMean') && ~all(size(funPsd.tMean)==1)
im = squeeze(funPsd.tMean);
imagesc(im)
ax = gca;
ax.Colormap = gray;
ax.YTick = []; ax.XTick = [];
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.CLim = prctile(im(:),[0 99]);
title('timeseries mean')
else
ax = gca;
ax.Visible = 'off';
end
%%% Plot psd at peak
for ii = 1:size(svdStruct.param.fpass,1)
if ii>3
break
end
tileInd = tileInd + 1;
ax = nexttile(tileInd,[2 1]);
fpass = svdStruct.param.fpass(ii,:) + [-1 1].*svdStruct.param.BW(ii);
fInd = funPsd.psd.f>fpass(1) & funPsd.psd.f<fpass(2);
if cFlag
hIm = imagesc(mean(abs(funPsd.vol(:,:,:,fInd)),4));
else
hIm = imagesc(mean(funPsd.vol(:,:,:,fInd),4));
end
ax.Colormap = jet; ax.ColorScale = 'log';
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.XAxis.Visible = 'off'; ax.YAxis.Visible = 'off';
ylabel(colorbar,[num2str(mean(fpass),'psd @ %0.2fHz')])
axPsd{ii} = ax;
end
%%% Plot first singular vector
%%%% Magnitude map
tileInd = 5;
ax = nexttile(tileInd,[2 1]);
spIm = nan(size(funPsd.vol2vec));
spIm(funPsd.vol2vec) = sp;
hIm = imagesc(abs(spIm));
ax.Colormap = jet; ax.ColorScale = 'log';
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.XAxis.Visible = 'off'; ax.YAxis.Visible = 'off';
ylabel(colorbar,'first sv weigths mag (a.u.)')
%%%% Phase map
tileInd = 15;
ax = nexttile(tileInd,[2 1]);
hIm = imagesc(angle(spIm),[-pi pi]);
scale = abs(spIm); scale = scale - min(scale(:)); scale = scale ./ max(scale(:));
hIm.AlphaData = scale;
ax.Colormap = hsv; ax.ColorScale = 'linear';
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.XAxis.Visible = 'off'; ax.YAxis.Visible = 'off';
ax.Color = [0.5 0.5 0.5];
ylabel(colorbar,'first sv weigths phase (rad)')
%%%% Phase distribution
tileInd = 14;
n = round(numel(sp)/500);
binEdges = linspace(-pi,pi,n+1);
binCent = binEdges(1:end-1)+diff(binEdges);
[~,~,bin] = histcounts(angle(sp),binEdges);
spBin = nan(1,n);
for i = 1:n
spBin(i) = mean(sp(bin==i));
end
ax = nexttile(tileInd,[2 1]);
[X,Y] = pol2cart(binCent,abs(spBin)); spBin = complex(X,Y);
polarplot(angle(spBin([1:end 1])),abs(spBin([1:end 1])));
ax = gca;
ax.ThetaAxisUnits = 'radians';
thetaTick = ax.ThetaTick(1:end-1);
minusPiInd = thetaTick<pi & thetaTick~=0;
plusPiInd = thetaTick>pi & thetaTick~=0;
ax.ThetaTickLabel(plusPiInd) = flip(strcat({'-'},ax.ThetaTickLabel(minusPiInd)));
ax.ThetaAxis.Label.String = 'sv weigth phase';
ax.RAxis.Label.String = 'sv weigth binned mag';
%%% Spectra
%%%% Spatial average
tileInd = 21;
ax = nexttile(tileInd,[1 4]);
tmpPsd = vol2vec(funPsd);
f = tmpPsd.psd.f;
y = mean(tmpPsd.vec,2,"omitmissing");
hP = plot(f,y,'k');
ax.YScale = 'log';
yLim = [min(y(2:end)) max(y(2:end))];
xLim = [min(f) max(f)];
ylim(yLim)
xlim(xLim)
grid on; ax.XMinorGrid = 'on'; ax.YMinorGrid = 'on';
y = [yLim(1) yLim(1) yLim(2) yLim(2) yLim(1)];
for i = 1:size(fRange,1)
x = [fRange(i,1) fRange(i,2) fRange(i,2) fRange(i,1) fRange(i,1)];
hPatch = patch(x,y,[1 1 1].*0.7); hPatch.EdgeColor = 'none'; uistack(hPatch,'bottom'); hPatch.FaceAlpha = 0.4;
end
% %%%% weighted avereage
% scale = abs(sp); scale = scale./norm(scale);
% spec = funPsd.psd.spec.*scale;
% spec = conj(spec).*spec;
% spec = mean(spec,3);
% spec = sum(spec,1);
% yyaxis right
% plot(f,spec)
%%%% Of reconstructed timeseries
yyaxis right
hold on
tmpPsd = vol2vec(svdPsd);
f = tmpPsd.psd.f;
y = mean(tmpPsd.vec,2,"omitmissing");
hP(2) = plot(f,y,'-');
ax.YScale = 'linear';
yLim = [min(y(2:end)) max(y(2:end))];
xLim = [min(f) max(f)];
ylim(yLim)
xlim(xLim)
grid on; ax.XMinorGrid = 'on'; ax.YMinorGrid = 'on';
legend(hP,{'unweighted mean' 'svd recon'})
%%% Eigen spectrum
tileInd = 25;
ax = nexttile(tileInd,[1 1]);
plot(svdStruct.sv/sum(svdStruct.sv).*100,'-ok'); hold on
hPtmp = plot(modeOrder,svdStruct.sv(modeOrder)/sum(svdStruct.sv).*100,'-or');
hPtmp.MarkerFaceColor = hPtmp.Color;
% plot(svdStruct.sv,'-ok')
ax.XScale = 'log';
grid on
ylabel('% variance explained')
xlabel('mode order')
%%% Timeseries
tileInd = 26;
ax = nexttile(tileInd,[1 5]);
t = 0:svdTs.tr/1000:svdTs.tr/1000.*(svdTs.nframes-1);
y = squeeze(svdTs.vol);
plot(t,real(y)); hold on
plot(t,imag(y))
xLim = [min([real(t) imag(t)]) max([real(t) imag(t)])];
xlim(xLim)
%%% Spectrogram
% K = 1;
TW = (K+1)/2;
T = funTs.tr/1000.*funTs.nframes;
W = TW/T;
param.tapers = [TW K];
param.Fs = 1/(funTs.tr/1000);
winSz = 10;
stepSz = winSz/4;
[Sx,tx,fx]=mtspecgramc(real(y),[winSz stepSz],param);
tileInd = 11;
ax = nexttile(tileInd,[2 3]);
hIm = imagesc(tx,fx,Sx');
ax.YDir = 'normal';
ax.ColorScale = 'log';
ax.Colormap = jet;
xlabel('time (s)')
ylabel('frequency (Hz)')
ylabel(colorbar,'PSD')