-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPlotPrePostAlign.m
213 lines (191 loc) · 8.95 KB
/
PlotPrePostAlign.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
function PlotPrePostAlign(MRS_struct, vox, ii, kk)
% Plot pre-/post-alignment spectra
% Updates by MGSaleh 2016, MM 2017-2024
if MRS_struct.p.HERMES
if strcmp(MRS_struct.p.alignment, 'none')
spectraToPlot = zeros(length(MRS_struct.p.target), length(MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{1}).diff));
else
spectraToPlot = zeros(2*length(MRS_struct.p.target), length(MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{1}).diff));
end
count = 0;
for jj = 1:length(MRS_struct.p.target)
if strcmp(MRS_struct.p.alignment, 'none')
spectraToPlot(1+count,:) = MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{jj}).diff(ii,:);
count = count + 1;
else
spectraToPlot((1:2)+count,:) = [MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{jj}).diff(ii,:); ...
MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{jj}).diff_noalign(ii,:)];
count = count + 2;
end
end
% Shift baselines to zero
baseRange = MRS_struct.spec.freq <= 0 & MRS_struct.spec.freq >= -0.5;
% Some bandwidth-limited acquisitions may not record anything below
% 0 ppm, in this case get the baseline from the other side of water
if sum(baseRange) == 0
baseRange = MRS_struct.spec.freq >= 7 & MRS_struct.spec.freq <= 8;
end
peakRange = zeros(length(MRS_struct.p.target), length(MRS_struct.spec.freq));
for jj = 1:length(MRS_struct.p.target)
switch MRS_struct.p.target{jj}
case {'GABA', 'Glx', 'GABAGlx'}
if MRS_struct.p.phantom
peakRange(jj,:) = MRS_struct.spec.freq <= 4.25 & MRS_struct.spec.freq >= 1.0;
else
peakRange(jj,:) = MRS_struct.spec.freq <= 4.1 & MRS_struct.spec.freq >= 2.2;
end
case 'GSH'
if ~MRS_struct.p.HERCULES
peakRange(jj,:) = MRS_struct.spec.freq <= 3.5 & MRS_struct.spec.freq >= 0.5;
else
peakRange(jj,:) = MRS_struct.spec.freq <= 4.25 & MRS_struct.spec.freq >= 3;
end
case {'Lac', 'EtOH'}
peakRange(jj,:) = MRS_struct.spec.freq <= 3 & MRS_struct.spec.freq >= 0.5;
end
end
specBaseline = mean(real(spectraToPlot(:,baseRange)),2);
spectraToPlot = spectraToPlot - repmat(specBaseline, [1 size(spectraToPlot,2)]);
% Stack spectra
if MRS_struct.p.phantom
if ~strcmp(MRS_struct.p.alignment, 'none')
shift = max(max(real(spectraToPlot(1:2,logical(peakRange(1,:)))))) - ...
min(min(real(spectraToPlot(1:2,logical(peakRange(1,:))))));
spectraToPlot(3:4,:) = spectraToPlot(3:4,:) + shift;
end
yAxisMax = max(real(spectraToPlot(3,logical(peakRange(2,:)))));
yAxisMin = min(real(spectraToPlot(1,logical(peakRange(1,:)))));
yRange = abs(yAxisMax - yAxisMin);
yAxisMax = yAxisMax + 0.1*yRange;
yAxisMin = yAxisMin - 0.1*yRange;
else
if all(ismember(MRS_struct.p.target, {'GABAGlx', 'GSH'})) || all(ismember(MRS_struct.p.target, {'GABA', 'GSH'}))
if ~strcmp(MRS_struct.p.alignment, 'none')
maxGABAGlx = abs(max(max(real(spectraToPlot(1:2,logical(peakRange(1,:)))),[],2)));
minGSH = abs(min(min(real(spectraToPlot(3:4,logical(peakRange(2,:)))),[],2)));
shift = max([maxGABAGlx minGSH]) + 0.5*min([maxGABAGlx minGSH]);
spectraToPlot(3:4,:) = spectraToPlot(3:4,:) + shift;
yAxisMax = max(real(spectraToPlot(3,logical(peakRange(2,:)))));
yAxisMin = min(real(spectraToPlot(1,logical(peakRange(1,:)))));
else
maxGABAGlx = abs(max(max(real(spectraToPlot(1,logical(peakRange(1,:)))),[],2)));
minGSH = abs(min(min(real(spectraToPlot(2,logical(peakRange(2,:)))),[],2)));
shift = max([maxGABAGlx minGSH]) + 0.5*min([maxGABAGlx minGSH]);
spectraToPlot(2,:) = spectraToPlot(2,:) + shift;
yAxisMax = max(real(spectraToPlot(2,logical(peakRange(2,:)))));
yAxisMin = min(real(spectraToPlot(1,logical(peakRange(1,:)))));
end
yRange = abs(yAxisMax - yAxisMin);
yAxisMax = yAxisMax + 0.1*yRange;
yAxisMin = yAxisMin - 0.1*yRange;
elseif all(ismember(MRS_struct.p.target, {'EtOH', 'GABA', 'GSH'}))
if ~strcmp(MRS_struct.p.alignment, 'none')
shift = abs(min(real(spectraToPlot(5,logical(peakRange(3,:))))));
spectraToPlot(5:6,:) = spectraToPlot(5:6,:) + 1.5*shift;
end
if ~strcmp(MRS_struct.p.alignment, 'none')
shift = abs(max(real(spectraToPlot(1,logical(peakRange(1,:))))));
spectraToPlot(1:2,:) = spectraToPlot(1:2,:) - 1.5*shift;
end
yAxisMax = abs(max(real(spectraToPlot(5,logical(peakRange(3,:))))));
yAxisMax = yAxisMax + 0.2*yAxisMax;
yAxisMin = max(real(spectraToPlot(1,logical(peakRange(1,:)))));
yAxisMin = yAxisMin - 4*abs(yAxisMin);
else
if ~strcmp(MRS_struct.p.alignment, 'none')
shift = abs(min(real(spectraToPlot(5,logical(peakRange(3,:))))));
spectraToPlot(5:6,:) = spectraToPlot(5:6,:) + 1.5*shift;
end
end
end
count = 0;
for jj = 1:length(MRS_struct.p.target)
hold on;
if ~strcmp(MRS_struct.p.alignment, 'none')
plot(MRS_struct.spec.freq, real(spectraToPlot(2+count*2,:)), 'Color', 'r');
plot(MRS_struct.spec.freq, real(spectraToPlot(1+count*2,:)), 'Color', 'b');
else
plot(MRS_struct.spec.freq, real(spectraToPlot(1+count,:)), 'Color', 'b');
end
hold off;
count = count + 1;
end
else
if strcmp(MRS_struct.p.alignment, 'none')
spectraToPlot = MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{1}).diff(ii,:);
else
spectraToPlot = [MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{1}).diff(ii,:); ...
MRS_struct.spec.(vox{kk}).(MRS_struct.p.target{1}).diff_noalign(ii,:)];
end
% Shift baselines to zero
baseRange = MRS_struct.spec.freq <= 0 & MRS_struct.spec.freq >= -0.5;
% Some bandwidth-limited acquisitions may not record anything below
% 0 ppm, in this case get the baseline from the other side of water
if sum(baseRange) == 0
baseRange = MRS_struct.spec.freq >= 7 & MRS_struct.spec.freq <= 8;
end
switch MRS_struct.p.target{1}
case {'GABA', 'Glx', 'GABAGlx'}
if MRS_struct.p.phantom
peakRange = MRS_struct.spec.freq <= 4.25 & MRS_struct.spec.freq >= 1.0;
else
peakRange = MRS_struct.spec.freq <= 4.1 & MRS_struct.spec.freq >= 2.26;
end
case 'GSH'
peakRange = MRS_struct.spec.freq <= 3.5 & MRS_struct.spec.freq >= 0.5;
case {'Lac', 'EtOH'}
peakRange = MRS_struct.spec.freq <= 3 & MRS_struct.spec.freq >= 0.5;
end
specBaseline = mean(real(spectraToPlot(:,baseRange)),2);
spectraToPlot = spectraToPlot - repmat(specBaseline, [1 length(spectraToPlot)]);
% Stack spectra
if ~strcmp(MRS_struct.p.alignment, 'none')
shift = max(abs(real(spectraToPlot(1,peakRange))));
spectraToPlot(2,:) = spectraToPlot(2,:) + shift + 0.05*shift;
end
hold on;
if ~strcmp(MRS_struct.p.alignment, 'none')
plot(MRS_struct.spec.freq, real(spectraToPlot(2,:)), 'Color', 'r');
plot(MRS_struct.spec.freq, real(spectraToPlot(1,:)), 'Color', 'b');
else
plot(MRS_struct.spec.freq, real(spectraToPlot(1,:)), 'Color', 'b');
end
hold off;
yAxisMax = max(max(real(spectraToPlot(:,peakRange)),[],2));
yAxisMin = min(min(real(spectraToPlot(:,peakRange)),[],2));
yRange = abs(yAxisMax - yAxisMin);
yAxisMax = yAxisMax + 0.1*yRange;
if any(strcmp(MRS_struct.p.target{1}, {'GABA', 'Glx', 'GABAGlx'}))
if MRS_struct.p.phantom
yAxisMin = yAxisMin - 0.15*abs(yAxisMin);
else
yAxisMin = yAxisMin - 0.3*yRange;
end
else
yAxisMin = yAxisMin - 0.1*yRange;
end
end
if ~strcmp(MRS_struct.p.alignment, 'none')
if strcmp(MRS_struct.p.target{1}, 'EtOH')
legend({'pre', 'post'}, 'EdgeColor', [1 1 1], 'Location', 'northwest');
else
legend({'pre', 'post'}, 'EdgeColor', [1 1 1]);
end
end
axis([0 5 yAxisMin yAxisMax]);
set(gca,'XDir', 'reverse', 'TickDir', 'out', 'box', 'off', 'XTick', 0:5);
set(get(gca, 'YAxis'), 'Visible', 'off');
xlabel('ppm');
if ~strcmp(MRS_struct.p.alignment, 'none')
if MRS_struct.p.HERMES
title({'Difference spectra'; '(pre- and post-alignment)'});
else
title({'Difference spectrum'; '(pre- and post-alignment)'});
end
else
if MRS_struct.p.HERMES
title({'Difference spectra'; '(no alignment)'});
else
title({'Difference spectrum'; '(no alignment)'});
end
end