-
Notifications
You must be signed in to change notification settings - Fork 13
/
SpecifyOnOffOrder.m
306 lines (274 loc) · 15.6 KB
/
SpecifyOnOffOrder.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
function MRS_struct = SpecifyOnOffOrder(MRS_struct)
% Determines the subexperiment order for MEGA-PRESS and HERMES. Borrows
% heavily from osp_onOffClassifyMEGA.m and osp_onOffClassifyHERMES.m from
% Osprey.
%
% Author:
% Dr. Georg Oeltzschner (Johns Hopkins University, 2018-02-24)
%
% History:
% 2018-02-24: First version.
% 2018-11-19: Second version.
% 2020-10-13: Third version.
% 2021-06-28: Fourth version. Adopted code from Osprey.
% [1 = ON, 0 = OFF]
if MRS_struct.p.phantom
if MRS_struct.p.HERMES
ON_OFF = [];
for ii = 1:length(MRS_struct.p.ON_OFF_order)
if strcmpi(MRS_struct.p.ON_OFF_order(ii), 'A')
ON_OFF = horzcat(ON_OFF, [1 1]'); %#ok<*AGROW>
elseif strcmpi(MRS_struct.p.ON_OFF_order(ii), 'B')
ON_OFF = horzcat(ON_OFF, [1 0]');
elseif strcmpi(MRS_struct.p.ON_OFF_order(ii), 'C')
ON_OFF = horzcat(ON_OFF, [0 1]');
elseif strcmpi(MRS_struct.p.ON_OFF_order(ii), 'D')
ON_OFF = horzcat(ON_OFF, [0 0]');
end
end
MRS_struct.fids.ON_OFF = repmat(ON_OFF, [1 size(MRS_struct.fids.data,2)/4]);
else
if strcmp(MRS_struct.p.ON_OFF_order, 'offfirst')
MRS_struct.fids.ON_OFF = repmat([0 1], [1 size(MRS_struct.fids.data,2)/2]);
else
MRS_struct.fids.ON_OFF = repmat([1 0], [1 size(MRS_struct.fids.data,2)/2]);
end
end
else
spec = abs(real(fftshift(fft(MRS_struct.fids.data,[],1),1)));
freqRange = MRS_struct.p.sw(1) / MRS_struct.p.LarmorFreq(1);
freq = (MRS_struct.p.npoints(1) + 1 - (1:MRS_struct.p.npoints(1))) / MRS_struct.p.npoints(1) * freqRange + 4.68 - freqRange/2;
NAAlim = freq <= 2.3 & freq >= 1.7;
waterLim = freq <= 5 & freq >= 4;
waterLim2 = freq <= 4.25 & freq >= 3.5;
switch num2str(length(MRS_struct.p.target))
case '1'
if any(strcmp(MRS_struct.p.target, {'GABA', 'Glx', 'GABAGlx'}))
freqLim = NAAlim;
elseif strcmp(MRS_struct.p.target, 'GSH')
freqLim = waterLim;
elseif any(strcmp(MRS_struct.p.target, {'Lac', 'EtOH'}))
freqLim = waterLim2;
end
specA = mean(spec(freqLim, 1:2:end),2);
specB = mean(spec(freqLim, 2:2:end),2);
max_diffAB = max(specA - specB);
max_diffBA = max(specB - specA);
if max_diffAB > max_diffBA
MRS_struct.fids.ON_OFF = repmat([0 1], [1 size(MRS_struct.fids.data,2)/2]);
else
MRS_struct.fids.ON_OFF = repmat([1 0], [1 size(MRS_struct.fids.data,2)/2]);
end
case {'2','3'}
[max_first(1), max_second(1)] = findMaxNAAw(mean(spec(:,1:4:end),2), waterLim, NAAlim);
[max_first(2), max_second(2)] = findMaxNAAw(mean(spec(:,2:4:end),2), waterLim, NAAlim);
[max_first(3), max_second(3)] = findMaxNAAw(mean(spec(:,3:4:end),2), waterLim, NAAlim);
[max_first(4), max_second(4)] = findMaxNAAw(mean(spec(:,4:4:end),2), waterLim, NAAlim);
% Sort the intensities in ascending order
[~,order_first] = sort(max_first);
[~,order_second] = sort(max_second);
% Now loop over the subspectra indices (A = 1, B = 2, etc) to determine
% whether the respective experiments have high or low intensities
for ii = 1:4
idx_first = find(order_first == ii);
idx_second = find(order_second == ii);
if ismember(idx_second,[3 4])
GABA_ON(ii) = 0;
elseif ismember(idx_second,[1 2])
GABA_ON(ii) = 1;
end
if ismember(idx_first,[3 4])
GSH_ON(ii) = 0;
elseif ismember(idx_first,[1 2])
GSH_ON(ii) = 1;
end
if length(MRS_struct.p.target) == 3
if (GABA_ON(ii) == 1 && GSH_ON(ii) == 1) || (GABA_ON(ii) == 0 && GSH_ON(ii) == 0)
EtOH_ON(ii) = 0;
elseif (GABA_ON(ii) == 1 && GSH_ON(ii) == 0) || (GABA_ON(ii) == 0 && GSH_ON(ii) == 1)
EtOH_ON(ii) = 1;
end
end
end
if length(MRS_struct.p.target) == 2
MRS_struct.fids.ON_OFF = repmat([GABA_ON; GSH_ON], [1 size(MRS_struct.fids.data,2)/4]);
elseif length(MRS_struct.p.target) == 3
MRS_struct.fids.ON_OFF = repmat([EtOH_ON; GABA_ON; GSH_ON], [1 size(MRS_struct.fids.data,2)/4]);
end
end
end
end
function [max_w, max_NAA] = findMaxNAAw(spec, waterLim, NAAlim)
% This embedded function finds the maximum intensities of the water
% and NAA signals of an input spectrum.
% Determine maximum absolute signal
max_w = max([abs(max(spec(waterLim))), abs(min(spec(waterLim)))]);
max_NAA = max([abs(max(spec(NAAlim))), abs(min(spec(NAAlim)))]);
end
% function [max_ins, max_NAA] = findMaxNAACr(in)
% % This embedded function finds the maximum intensities of the creatine and
% % siganls at 3.9 ppm and NAA signals of an input spectrum.
%
% % Determine relevant frequency ranges
% out_ins = op_freqrange(in,3.8,4);
% out_NAA = op_freqrange(in,1.8,2.2);
%
% % Determine maximum absolute signal
% max_ins = max([abs(max(real(out_ins.specs))), abs(min(real(out_ins.specs)))]);
% max_NAA = max([abs(max(real(out_NAA.specs))), abs(min(real(out_NAA.specs)))]);
% end
% switch MRS_struct.p.ON_OFF_order
%
% case 'onfirst'
%
% if MRS_struct.p.HERMES
%
% switch MRS_struct.p.vendor
%
% case 'GE'
% if all(ismember(MRS_struct.p.target,{'GABAGlx','GSH'})) || all(ismember(MRS_struct.p.target,{'GABA','GSH'})) || all(ismember(MRS_struct.p.target,{'Glx','GSH'}))
% if strcmpi(MRS_struct.p.seqorig,'Lythgoe')
% % 1=ExpD, 2=ExpB, 3=ExpC, 4=ExpA (MM: 201013)
% MRS_struct.fids.ON_OFF = repmat([0 1 0 1; 0 0 1 1], [1 size(MRS_struct.fids.data,2)/4]);
% elseif strcmpi(MRS_struct.p.seqorig,'Noeske')
% % 1=ExpB, 2=ExpC, 3=ExpA, 4=ExpD (MM: 201013)
% MRS_struct.fids.ON_OFF = repmat([1 0 1 0; 0 1 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% else
% % 1=ExpA, 2=ExpB, 3=ExpC, 4=ExpD (MM: 171120)
% MRS_struct.fids.ON_OFF = repmat([1 1 0 0; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
% elseif all(ismember(MRS_struct.p.target,{'GSH','Lac'}))
% MRS_struct.fids.ON_OFF = repmat([0 1 1 0; 0 1 0 1], [1 size(MRS_struct.fids.data,2)/4]);
% elseif all(ismember(MRS_struct.p.target,{'EtOH','GABA','GSH'}))
% % 1=B, 2=C, 3=A, 4=D
% MRS_struct.fids.ON_OFF = repmat([1 1 0 0; 1 0 1 0; 0 1 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
%
% case {'Philips','Philips_data'}
% if ~MRS_struct.p.HERCULES
% if all(ismember(MRS_struct.p.target,{'GABAGlx','GSH'})) || all(ismember(MRS_struct.p.target,{'GABA','GSH'})) || all(ismember(MRS_struct.p.target,{'Glx','GSH'}))
% % 1=ExpC, 2=ExpB, 3=ExpA, 4=ExpD
% MRS_struct.fids.ON_OFF = repmat([0 1 1 0; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% elseif all(ismember(MRS_struct.p.target,{'GSH','Lac'}))
% % 1=ExpC, 2=ExpB, 3=ExpA, 4=ExpD
% MRS_struct.fids.ON_OFF = repmat([0 1 1 0; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
% else
% % 1=ExpC, 2=ExpD, 3=ExpA, 4=ExpB
% MRS_struct.fids.ON_OFF = repmat([0 0 1 1; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
%
% case {'Siemens_twix','Siemens_dicom'}
% if ~MRS_struct.p.HERCULES
% if all(ismember(MRS_struct.p.target,{'GABAGlx','GSH'})) || all(ismember(MRS_struct.p.target,{'GABA','GSH'})) || all(ismember(MRS_struct.p.target,{'Glx','GSH'}))
% % 1=ExpB, 2=ExpD, 3=ExpC, 4=ExpA (MM: 181210)
% MRS_struct.fids.ON_OFF = repmat([1 0 0 1; 0 0 1 1], [1 size(MRS_struct.fids.data,2)/4]);
% elseif all(ismember(MRS_struct.p.target,{'GSH','Lac'}))
% % This has not been tested with universal sequence -- 03142018 MGSaleh
% MRS_struct.fids.ON_OFF = repmat([0 1 1 0; 1 1 0 0], [1 size(MRS_struct.fids.data,2)/4]);
% elseif all(ismember(MRS_struct.p.target,{'EtOH','GABA','GSH'}))
% % 1=A, 2=D, 3=B, 4=C
% MRS_struct.fids.ON_OFF = repmat([1 0 1 0; 1 0 0 1; 0 0 1 1], [1 size(MRS_struct.fids.data,2)/4]);
% end
% else
% % 1=ExpA, 2=ExpB, 3=ExpC, 4=ExpD
% MRS_struct.fids.ON_OFF = repmat([1 1 0 0; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
%
% end
%
% else % MEGA-PRESS
%
% if strcmpi(MRS_struct.p.vendor,'Philips') && strcmpi(MRS_struct.p.seqorig,'Philips')
% MRS_struct.fids.ON_OFF = [ones(1,size(MRS_struct.fids.data,2)/2) zeros(1,size(MRS_struct.fids.data,2)/2)];
% elseif strcmpi(MRS_struct.p.vendor,'Philips_data') % Hardcode for now. This repmat depends on the value entered for NSA
% if ceil(MRS_struct.p.LarmorFreq(MRS_struct.ii)) > 290
% MRS_struct.fids.ON_OFF = repmat([ones(1,(MRS_struct.p.Navg(MRS_struct.ii)/MRS_struct.p.nrows)) ...
% zeros(1,(MRS_struct.p.Navg(MRS_struct.ii)/MRS_struct.p.nrows))], ...
% [1 size(MRS_struct.fids.data,2)/((MRS_struct.p.Navg(MRS_struct.ii)/MRS_struct.p.nrows)*2)]); % GABA @ 7T % Changed by MGSaleh -- 2017
% else
% MRS_struct.fids.ON_OFF = repmat([1 1 0 0], [1 size(MRS_struct.fids.data,2)/4]);
% %MRS_struct.fids.ON_OFF = repmat([1 0], [1 size(MRS_struct.fids.data,2)/2]); %This seems to work with the MR1 Philips_data
% end
% else
% MRS_struct.fids.ON_OFF = repmat([1 0], [1 size(MRS_struct.fids.data,2)/2]);
% end
%
% end
%
% case 'offfirst'
%
% if MRS_struct.p.HERMES
%
% switch MRS_struct.p.vendor
%
% case 'GE'
% if all(ismember(MRS_struct.p.target,{'GABAGlx','GSH'})) || all(ismember(MRS_struct.p.target,{'GABA','GSH'})) || all(ismember(MRS_struct.p.target,{'Glx','GSH'}))
% if strcmpi(MRS_struct.p.seqorig,'Lythgoe')
% % 1=ExpD, 2=ExpB, 3=ExpC, 4=ExpA (MM: 201013)
% MRS_struct.fids.ON_OFF = repmat([0 1 0 1; 0 0 1 1], [1 size(MRS_struct.fids.data,2)/4]);
% elseif strcmpi(MRS_struct.p.seqorig,'Noeske')
% % 1=ExpB, 2=ExpC, 3=ExpA, 4=ExpD (MM: 201013)
% MRS_struct.fids.ON_OFF = repmat([1 0 1 0; 0 1 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% else
% % 1=ExpA, 2=ExpB, 3=ExpC, 4=ExpD (MM: 171120)
% MRS_struct.fids.ON_OFF = repmat([1 1 0 0; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
% elseif all(ismember(MRS_struct.p.target,{'GSH','Lac'}))
% MRS_struct.fids.ON_OFF = repmat([0 1 1 0; 0 1 0 1], [1 size(MRS_struct.fids.data,2)/4]);
% end
%
% case {'Philips','Philips_data'}
% if ~MRS_struct.p.HERCULES
% if all(ismember(MRS_struct.p.target,{'GABAGlx','GSH'})) || all(ismember(MRS_struct.p.target,{'GABA','GSH'})) || all(ismember(MRS_struct.p.target,{'Glx','GSH'}))
% % 1=ExpC, 2=ExpB, 3=ExpA, 4=ExpD
% MRS_struct.fids.ON_OFF = repmat([0 1 1 0; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% elseif all(ismember(MRS_struct.p.target,{'GSH','Lac'}))
% MRS_struct.fids.ON_OFF = repmat([1 0 0 1; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
% else
% % 1=ExpC, 2=ExpD, 3=ExpA, 4=ExpB
% MRS_struct.fids.ON_OFF = repmat([0 0 1 1; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
%
% case 'Siemens_twix'
% if ~MRS_struct.p.HERCULES
% if all(ismember(MRS_struct.p.target,{'GABAGlx','GSH'})) || all(ismember(MRS_struct.p.target,{'GABA','GSH'})) || all(ismember(MRS_struct.p.target,{'Glx','GSH'}))
% % 1=ExpB, 2=ExpD, 3=ExpC, 4=ExpA (MM: 181210)
% MRS_struct.fids.ON_OFF = repmat([1 0 0 1; 0 0 1 1], [1 size(MRS_struct.fids.data,2)/4]);
% elseif all(ismember(MRS_struct.p.target,{'GSH','Lac'}))
% % This has not been tested with universal sequence -- 03142018 MGSaleh
% MRS_struct.fids.ON_OFF = repmat([0 1 1 0], [1 size(MRS_struct.fids.data,2)/4]); % GSH
% MRS_struct.fids.ON_OFF2 = repmat([0 1 0 1], [1 size(MRS_struct.fids.data,2)/4]); % Lac
% elseif all(ismember(MRS_struct.p.target,{'EtOH','GABA','GSH'}))
% % 1=?, 2=?, 3=?, 4=?
% MRS_struct.fids.ON_OFF = repmat([1 0 1 0; 1 0 0 1; 0 0 1 1], [1 size(MRS_struct.fids.data,2)/4]);
% end
% else
% % 1=ExpA, 2=ExpB, 3=ExpC, 4=ExpD
% MRS_struct.fids.ON_OFF = repmat([1 1 0 0; 1 0 1 0], [1 size(MRS_struct.fids.data,2)/4]);
% end
%
% end
%
% else % MEGA-PRESS
%
% if strcmpi(MRS_struct.p.vendor,'Philips') && strcmpi(MRS_struct.p.seqorig,'Philips')
% MRS_struct.fids.ON_OFF = [zeros(1,size(MRS_struct.fids.data,2)/2) ones(1,size(MRS_struct.fids.data,2)/2)];
% elseif strcmpi(MRS_struct.p.vendor,'Philips_data') % Hardcode for now. This repmat depends on the value entered for NSA
% if ceil(MRS_struct.p.LarmorFreq(MRS_struct.ii)) > 290
% MRS_struct.fids.ON_OFF = repmat([zeros(1,(MRS_struct.p.Navg(MRS_struct.ii)/MRS_struct.p.nrows)) ...
% ones(1,(MRS_struct.p.Navg(MRS_struct.ii)/MRS_struct.p.nrows))], ...
% [1 size(MRS_struct.fids.data,2)/((MRS_struct.p.Navg(MRS_struct.ii)/MRS_struct.p.nrows)*2)]); % GABA @ 7T % Changed by MGSaleh -- 2017
% else
% %MRS_struct.fids.ON_OFF = repmat([0 0 1 1], [1 size(MRS_struct.fids.data,2)/4]);
% MRS_struct.fids.ON_OFF = repmat([0 1], [1 size(MRS_struct.fids.data,2)/2]);
% end
% else
% MRS_struct.fids.ON_OFF = repmat([0 1], [1 size(MRS_struct.fids.data,2)/2]);
% end
%
% end
%
% end