-
Notifications
You must be signed in to change notification settings - Fork 13
/
AlignSubSpectra_PreAlignRef.m
250 lines (201 loc) · 8.65 KB
/
AlignSubSpectra_PreAlignRef.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
function MRS_struct = AlignSubSpectra_PreAlignRef(MRS_struct, water_flag)
ii = MRS_struct.ii;
fids = MRS_struct.fids.data_align;
fids_ref = MRS_struct.fids.data;
freqRange = MRS_struct.p.sw(ii) / MRS_struct.p.LarmorFreq(ii);
freq = (size(fids,1) + 1 - (1:size(fids,1))) / size(fids,1) * freqRange + 4.68 - freqRange/2;
t = 0:(1/MRS_struct.p.sw(ii)):(size(fids,1)-1)*(1/MRS_struct.p.sw(ii));
tMax = find(t <= 0.1,1,'last');
MSEfun = @(a,b) sum((a - b).^2) / length(a);
% Pre-allocate memory
if MRS_struct.p.HERMES
n = 4;
else
n = 2;
end
D = zeros(size(fids,2)/n);
D_ref = zeros(size(fids,2)/n);
w = cell(1,n);
w_ref = cell(1,n);
data = complex(zeros(size(fids,1),n));
data_ref = complex(zeros(size(fids,1),n));
% Optimization options
lsqnonlinopts = optimoptions(@lsqnonlin);
lsqnonlinopts = optimoptions(lsqnonlinopts,'Algorithm','levenberg-marquardt','Display','off');
% Use weighted averaging to average subspectra
if size(MRS_struct.fids.data,2) <= 4
data = fids;
data_ref = fids_ref;
else
for jj = 1:n
if MRS_struct.p.HERMES
ind = jj:n:size(fids,2);
else
ind = find(MRS_struct.fids.ON_OFF == abs(jj-2));
end
for kk = 1:size(fids,2)/n
for ll = 1:size(fids,2)/n
D(kk,ll) = MSEfun(real(fids(1:tMax,ind(kk))), real(fids(1:tMax,ind(ll))));
D_ref(kk,ll) = MSEfun(real(fids_ref(1:tMax,ind(kk))), real(fids_ref(1:tMax,ind(ll))));
end
end
D(~D) = NaN;
d = mean(D,'omitnan');
w{jj} = 1./d.^2;
w{jj} = w{jj} / sum(w{jj});
w{jj} = repmat(w{jj}, [size(fids,1) 1]);
D_ref(~D_ref) = NaN;
d_ref = mean(D_ref,'omitnan');
w_ref{jj} = 1./d_ref.^2;
w_ref{jj} = w_ref{jj} / sum(w_ref{jj});
w_ref{jj} = repmat(w_ref{jj}, [size(fids,1) 1]);
if water_flag
dataLim = ceil(length(ind)/3);
data(:,jj) = sum(w{jj}(:,1:dataLim) .* fids(:,ind(1:dataLim)),2);
data_ref(:,jj) = sum(w_ref{jj}(:,1:dataLim) .* fids_ref(:,ind(1:dataLim)),2);
else
data(:,jj) = sum(w{jj} .* fids(:,ind),2);
data_ref(:,jj) = sum(w_ref{jj} .* fids_ref(:,ind),2);
end
end
end
flatdata = FlattenData(data);
ref_flatdata = FlattenData(data_ref);
% Set HERMES subexperiment indices (A, B, C, D)
if MRS_struct.p.HERMES
if ~MRS_struct.p.HERCULES
if length(MRS_struct.p.target) == 2 && all(strcmp(MRS_struct.p.target, {'GABAGlx', 'GSH'}))
switch MRS_struct.p.vendor
case 'GE'
subSpecInd = [3 2 1 4];
case {'Philips', 'Philips_data', 'Philips_raw'}
subSpecInd = [1 2 3 4];
case {'Siemens_twix', 'Siemens_rda', 'Siemens_dicom'}
subSpecInd = [3 1 4 2];
end
elseif length(MRS_struct.p.target) == 3 && all(strcmp(MRS_struct.p.target, {'EtOH', 'GABA', 'GSH'}))
switch MRS_struct.p.vendor
case {'Philips', 'Philips_data', 'Philips_raw'}
% throw an error for now
case {'Siemens_twix', 'Siemens_rda', 'Siemens_dicom'}
subSpecInd = [3 1 4 2];
end
end
else
switch MRS_struct.p.vendor
case 'GE'
subSpecInd = [3 2 1 4];
case {'Philips', 'Philips_data', 'Philips_raw'}
subSpecInd = [1 4 3 2];
case {'Siemens_twix', 'Siemens_rda', 'Siemens_dicom'}
subSpecInd = [3 2 1 4];
end
end
end
a = max(flatdata(:));
freqLim = freq <= 3.4 & freq >= 1.8;
% Align subspectra to their pre-aligned equivalent
if MRS_struct.p.HERMES
% A
in = flatdata(:,:,subSpecInd([1 4]))./a;
in(:,:,2) = ref_flatdata(:,:,subSpecInd(1))./a;
fun = @(x) objFunc(in, freq, freqLim, t, x);
param(1,:) = lsqnonlin(fun, [0 0], [], [], lsqnonlinopts);
% B
in = flatdata(:,:,subSpecInd([2 4]))./a;
in(:,:,2) = ref_flatdata(:,:,subSpecInd(2))./a;
fun = @(x) objFunc(in, freq, freqLim, t, x);
param(2,:) = lsqnonlin(fun, [0 0], [], [], lsqnonlinopts);
% C
in = flatdata(:,:,subSpecInd([3 4]))./a;
in(:,:,2) = ref_flatdata(:,:,subSpecInd(3))./a;
fun = @(x) objFunc(in, freq, freqLim, t, x);
param(3,:) = lsqnonlin(fun, [0 0], [], [], lsqnonlinopts);
% D
in = flatdata(:,:,subSpecInd([4 1]))./a;
in(:,:,2) = ref_flatdata(:,:,subSpecInd(4))./a;
fun = @(x) objFunc(in, freq, freqLim, t, x);
param(4,:) = lsqnonlin(fun, [0 0], [], [], lsqnonlinopts);
else
% ON
in(:,:,1) = flatdata(:,:,1)./a;
in(:,:,2) = ref_flatdata(:,:,1)./a;
fun = @(x) objFunc(in, freq, freqLim, t, x);
param(1,:) = lsqnonlin(fun, [0 0], [], [], lsqnonlinopts);
% OFF
in(:,:,1) = flatdata(:,:,2)./a;
in(:,:,2) = ref_flatdata(:,:,2)./a;
fun = @(x) objFunc(in, freq, freqLim, t, x);
param(2,:) = lsqnonlin(fun, [0 0], [], [], lsqnonlinopts);
end
% Apply frequency and phase corrections to subspectra
if MRS_struct.p.HERMES
ind1 = subSpecInd(1):4:size(fids,2);
ind2 = subSpecInd(2):4:size(fids,2);
ind3 = subSpecInd(3):4:size(fids,2);
ind4 = subSpecInd(4):4:size(fids,2);
fids(:,ind1) = fids(:,ind1) ...
.* exp(1i * param(1,1) * 2 * pi .* repmat(t', [1 length(ind1)])) ...
.* repmat(exp(1i * pi/180 * param(1,2)), [length(t) length(ind1)]);
fids(:,ind2) = fids(:,ind2) ...
.* exp(1i * param(2,1) * 2 * pi .* repmat(t', [1 length(ind2)])) ...
.* repmat(exp(1i * pi/180 * param(2,2)), [length(t) length(ind2)]);
fids(:,ind3) = fids(:,ind3) ...
.* exp(1i * param(3,1) * 2 * pi .* repmat(t', [1 length(ind3)])) ...
.* repmat(exp(1i * pi/180 * param(3,2)), [length(t) length(ind3)]);
fids(:,ind4) = fids(:,ind4) ...
.* exp(1i * param(4,1) * 2 * pi .* repmat(t', [1 length(ind4)])) ...
.* repmat(exp(1i * pi/180 * param(4,2)), [length(t) length(ind4)]);
MRS_struct.out.SpecReg.freq{ii}(ind1) = MRS_struct.out.SpecReg.freq{ii}(ind1) + param(1,1);
MRS_struct.out.SpecReg.phase{ii}(ind1) = MRS_struct.out.SpecReg.phase{ii}(ind1) + param(1,2);
MRS_struct.out.SpecReg.freq{ii}(ind2) = MRS_struct.out.SpecReg.freq{ii}(ind2) + param(2,1);
MRS_struct.out.SpecReg.phase{ii}(ind2) = MRS_struct.out.SpecReg.phase{ii}(ind2) + param(2,2);
MRS_struct.out.SpecReg.freq{ii}(ind3) = MRS_struct.out.SpecReg.freq{ii}(ind3) + param(3,1);
MRS_struct.out.SpecReg.phase{ii}(ind3) = MRS_struct.out.SpecReg.phase{ii}(ind3) + param(3,2);
MRS_struct.out.SpecReg.freq{ii}(ind4) = MRS_struct.out.SpecReg.freq{ii}(ind4) + param(4,1);
MRS_struct.out.SpecReg.phase{ii}(ind4) = MRS_struct.out.SpecReg.phase{ii}(ind4) + param(4,2);
else
ind1 = find(MRS_struct.fids.ON_OFF == 1);
ind2 = find(MRS_struct.fids.ON_OFF == 0);
fids(:,ind1) = fids(:,ind1) ...
.* exp(1i * param(1,1) * 2 * pi .* repmat(t', [1 length(ind1)])) ...
.* repmat(exp(1i * pi/180 * param(1,2)), [length(t) length(ind1)]);
fids(:,ind2) = fids(:,ind2) ...
.* exp(1i * param(2,1) * 2 * pi .* repmat(t', [1 length(ind2)])) ...
.* repmat(exp(1i * pi/180 * param(2,2)), [length(t) length(ind2)]);
MRS_struct.out.SpecReg.freq{ii}(ind1) = MRS_struct.out.SpecReg.freq{ii}(ind1) + param(1,1);
MRS_struct.out.SpecReg.phase{ii}(ind1) = MRS_struct.out.SpecReg.phase{ii}(ind1) + param(1,2);
MRS_struct.out.SpecReg.freq{ii}(ind2) = MRS_struct.out.SpecReg.freq{ii}(ind2) + param(2,1);
MRS_struct.out.SpecReg.phase{ii}(ind2) = MRS_struct.out.SpecReg.phase{ii}(ind2) + param(2,2);
end
MRS_struct.fids.data_align = fids;
if ishandle(44)
close(44);
end
end
function [flatdata, data] = FlattenData(data)
flatdata(:,1,:) = real(data);
flatdata(:,2,:) = imag(data);
data = real(fftshift(fft(data,[],1),1));
end
function out = objFunc(in, freq, freqLim, t, x) %#ok<INUSL>
f = x(1);
phi = x(2);
y = complex(in(:,1,1), in(:,2,1));
y = y .* exp(1i * pi * (t' * f * 2 + phi/180));
a = real(fftshift(fft(y)));
b = real(fftshift(fft(complex(in(:,1,2), in(:,2,2)))));
DIFF = a - b;
out = DIFF(freqLim);
% figure(44);
% cla;
% hold on;
% plot(freq, a, 'k');
% plot(freq, b, 'r');
% plot(freq, DIFF - 5, 'k');
% plot(freq(freqLim), out - 5, 'r');
% hold off;
% set(gca,'XDir','reverse','XLim',[1 5]);
% drawnow;
% pause(0.05);
end