forked from rehmanali1994/WaveformInversionUST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiFrequencyWaveformInvVSX.m
More file actions
292 lines (270 loc) · 13.3 KB
/
Copy pathMultiFrequencyWaveformInvVSX.m
File metadata and controls
292 lines (270 loc) · 13.3 KB
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
clear
clc
% Add Functions to Path
addpath(genpath('Functions'));
% Load Extracted Dataset
filename = 'VSX_YezitronixPhantom2'; % 1 or 2
load(['SampleData/', filename, '.mat'], ...
'time', 'transducerPositionsXY', 'full_dataset');
% Assemble Full Dataset
numElements = size(transducerPositionsXY,2);
fsa_rf_data = single(full_dataset);
clearvars full_dataset;
% Extract the Desired Frequency for Waveform Inversion
fDATA_SoS = (0.35:0.05:0.65)*(1e6); % Frequencies for SoS-only Iterations [Hz]
fDATA_SoSAtten = (0.375:0.05:0.675)*(1e6); % Frequencies for SoS/Attenuation Iterations [Hz]
fDATA = [fDATA_SoS, fDATA_SoSAtten]; % All Frequencies [Hz]
% Attenuation Iterations Always Happen After SoS Iterations
niterSoSPerFreq = [3*ones(size(fDATA_SoS)), 3*ones(size(fDATA_SoSAtten))]; % Number of Sound Speed Iterations Per Frequency
niterAttenPerFreq = [0*ones(size(fDATA_SoS)), 3*ones(size(fDATA_SoSAtten))]; % Number of Attenuation Iterations Per Frequency
% Discrete-Time Fourier Transform (DTFT) - Not an FFT though!
DTFT = exp(-1i*2*pi*fDATA'*time)*mean(diff(time));
% Geometric TOF Based on Sound Speed
c_geom = 1540; % Sound Speed [mm/us]
transducerPositionsX = transducerPositionsXY(1,:);
transducerPositionsY = transducerPositionsXY(2,:);
geomTOFs = zeros(size(transducerPositionsXY,2));
for col = 1:size(transducerPositionsXY,2)
geomTOFs(:,col) = ...
sqrt((transducerPositionsX-transducerPositionsX(col)).^2 + ...
(transducerPositionsY-transducerPositionsY(col)).^2)/c_geom;
end
clearvars col transducerPositionsXY;
% Window Time Traces - Extract the Frequencies for Waveform Inversion
REC_DATA = zeros(numElements, numElements, numel(fDATA), 'single');
sos_perc_change_pre = 0.05; sos_perc_change_post = Inf;
twinpre = sos_perc_change_pre*max(geomTOFs(:));
twinpost = sos_perc_change_post*max(geomTOFs(:));
for tx_element = 1:numElements
times_tx = geomTOFs(tx_element,:);
[TIMES_TX, TIME] = meshgrid(times_tx, time);
window = exp(-(1/2)*(subplus(TIME-TIMES_TX)/twinpost + ...
subplus(TIMES_TX-TIME)/twinpre).^2);
REC_DATA(tx_element,:,:) = ...
permute(DTFT*(window.*fsa_rf_data(:,:,tx_element)),[2,1]);
end
clearvars twin window fsa_rf_data time times_tx TIME TIMES_TX DTFT;
%% Waveform Inversion
% Create Sound Speed Map and Transducer Ring
dxi = 0.5e-3; xmax = 120e-3;
xi = -xmax:dxi:xmax; yi = xi;
Nxi = numel(xi); Nyi = numel(yi);
[Xi, Yi] = meshgrid(xi, yi);
x_circ = transducerPositionsX;
y_circ = transducerPositionsY;
x_idx = dsearchn(xi(:), x_circ(:));
y_idx = dsearchn(yi(:), y_circ(:));
ind = sub2ind([Nyi, Nxi], y_idx, x_idx);
msk = zeros(Nyi, Nxi); msk(ind) = 1;
% Parameters
h = dxi; % [m]
g = 1; % (grid spacing in Y)/(grid spacing in X)
alphaDB = 0.0; % Attenuation [dB/(MHz*mm)]
alphaNp = (log(10)/20)*alphaDB*((1e3)/(1e6)); % Attenuation [Np/(Hz*m)]
ATTEN = alphaNp*ones(Nyi,Nxi); % Make Spatially Varying Attenuation [Np/(Hz m)]
% Solve Options for Helmholtz Equation
sign_conv = -1; % Sign Convention
a0 = 10; % PML Constant
L_PML = 9.0e-3; % Thickness of PML
% Conversion of Units for Attenuation Map
Np2dB = 20/log(10);
slow2atten = (1e6)/(1e2); % Hz to MHz; m to cm
% Compute Phase Screen to Correct for Discretization of Element Positions
% Times for Discretized Positions Assuming Constant Sound Speed
x_circ_disc = xi(x_idx); y_circ_disc = yi(y_idx);
[x_circ_disc_tx, x_circ_disc_rx] = meshgrid(x_circ_disc, x_circ_disc);
[y_circ_disc_tx, y_circ_disc_rx] = meshgrid(y_circ_disc, y_circ_disc);
geomTOFs_disc = sqrt((x_circ_disc_tx-x_circ_disc_rx).^2 + ...
(y_circ_disc_tx-y_circ_disc_rx).^2)/c_geom;
% Time-of-Flight Error for Discretized Positions
geomTOFs_error = geomTOFs_disc-geomTOFs;
% Phase Screen and Gain Correction
PS = zeros(numElements, numElements, numel(fDATA));
for f_idx = 1:numel(fDATA)
PS(:,:,f_idx) = exp(1i*sign_conv*2*pi*fDATA(f_idx)*geomTOFs_error);
end
REC_DATA = REC_DATA .* PS; % Phase Screen and Gain Corrections
clearvars geom_distance col PS geomTOFs_error geomTOFs_disc geomTOFs;
% Which Subset of Transmits to Use
dwnsmp = 1; % can be 1, 2, or 4 (faster but less quality with more downsampling)
tx_include = 1:dwnsmp:numElements;
REC_DATA = REC_DATA(tx_include,:,:);
REC_DATA(isnan(REC_DATA)) = 0; % Eliminate Blank Channel
% Extract Subset of Times within Acceptance Angle
numElemLeftRightExcl = 31;
elemLeftRightExcl = -numElemLeftRightExcl:numElemLeftRightExcl;
elemInclude = true(numElements, numElements);
for tx_element = 1:numElements
elemLeftRightExclCurrent = elemLeftRightExcl + tx_element;
elemLeftRightExclCurrent(elemLeftRightExclCurrent<1) = numElements + ...
elemLeftRightExclCurrent(elemLeftRightExclCurrent<1);
elemLeftRightExclCurrent(elemLeftRightExclCurrent>numElements) = ...
elemLeftRightExclCurrent(elemLeftRightExclCurrent>numElements) - numElements;
elemInclude(tx_element,elemLeftRightExclCurrent) = false;
end
% Remove Outliers from Observed Signals Prior to Waveform Inversion
perc_outliers = 0.99; % Confidence Interval Cutoff
for f_idx = 1:numel(fDATA)
REC_DATA_SINGLE_FREQ = REC_DATA(:,:,f_idx);
signalMagnitudes = elemInclude(tx_include,:).*abs(REC_DATA_SINGLE_FREQ);
num_outliers = ceil((1-perc_outliers)*numel(signalMagnitudes));
[~,idx_outliers] = maxk(signalMagnitudes(:),num_outliers);
REC_DATA_SINGLE_FREQ(idx_outliers) = 0;
REC_DATA(:,:,f_idx) = REC_DATA_SINGLE_FREQ;
end
% Initial Constant Sound Speed Map [m/s]
c_init = 1480; % Initial Homogeneous Sound Speed [m/s] Guess
VEL_INIT = c_init*ones(Nyi,Nxi);
% Initial Constant Attenuation [Np/(Hz m)]
ATTEN_INIT = 0*alphaNp*ones(Nyi,Nxi);
% (Nonlinear) Conjugate Gradient
search_dir = zeros(Nyi,Nxi); % Conjugate Gradient Direction
gradient_img_prev = zeros(Nyi,Nxi); % Previous Gradient Image
VEL_ESTIM = VEL_INIT; ATTEN_ESTIM = ATTEN_INIT;
SLOW_ESTIM = 1./VEL_ESTIM + ...
1i*sign(sign_conv)*ATTEN_ESTIM/(2*pi); % Initial Slowness Image [s/m]
crange = [1450, 1550]; % For reconstruction display [m/s]
attenrange = 10*[-1,1]; % For reconstruction display [dB/(cm MHz)]
c0 = mean(VEL_ESTIM(:)); cutoff = 0.5; ord = 5; % Parameters for Ringing Removal Filter
% Values to Save at Each Iteration
Niter = sum(niterSoSPerFreq)+sum(niterAttenPerFreq);
VEL_ESTIM_ITER = zeros(Nyi,Nxi,Niter);
ATTEN_ESTIM_ITER = zeros(Nyi, Nxi, Niter);
GRAD_IMG_ITER = zeros(Nyi,Nxi,Niter);
SEARCH_DIR_ITER = zeros(Nyi,Nxi,Niter);
for f_idx = 1:numel(fDATA)
% Iterations at Each Frequency
for iter_f_idx = 1:(niterSoSPerFreq(f_idx)+niterAttenPerFreq(f_idx))
tic;
% Step 1: Accumulate Backprojection Over Each Element
iter = iter_f_idx + sum(niterSoSPerFreq(1:f_idx-1)) + ...
sum(niterAttenPerFreq(1:f_idx-1));
% Reset CG at Each Frequency (SoS and Attenuation)
if ((iter_f_idx == 1) || (iter_f_idx == 1+niterSoSPerFreq(f_idx)))
search_dir = zeros(Nyi, Nxi); % Conjugate Gradient Direction
gradient_img_prev = zeros(Nyi, Nxi); % Previous Gradient Image
end
% Attenuation Iterations Always Happen After SoS Iterations
if iter_f_idx > niterSoSPerFreq(f_idx)
updateAttenuation = true;
else
updateAttenuation = false;
end
gradient_img = zeros(Nyi,Nxi);
% Generate Sources
SRC = zeros(Nyi, Nxi, numel(tx_include));
for elmt_idx = 1:numel(tx_include)
% Single Element Source
x_idx_src = x_idx(tx_include(elmt_idx));
y_idx_src = y_idx(tx_include(elmt_idx));
SRC(y_idx_src, x_idx_src, elmt_idx) = 1;
end
% Forward Solve Helmholtz Equation
HS = HelmholtzSolver(xi, yi, VEL_ESTIM, ATTEN_ESTIM, ...
fDATA(f_idx), sign_conv, a0, L_PML);
[WVFIELD, VIRT_SRC] = HS.solve(SRC,false);
% Virtual Sources and Virtual (Sound Speed-Only) Wavefield for Attenuation
if updateAttenuation % Virtual Sources for Attenuation
% Modify Virtual Source for Attenuation
VIRT_SRC = 1i*sign(sign_conv)*VIRT_SRC;
end
% Build Adjoint Sources
scaling = zeros(numel(tx_include), 1);
ADJ_SRC = zeros(Nyi, Nxi, numel(tx_include));
for elmt_idx = 1:numel(tx_include)
WVFIELD_elmt = WVFIELD(:,:,elmt_idx);
REC_SIM = WVFIELD_elmt(ind(elemInclude(tx_include(elmt_idx),:)));
REC = REC_DATA(elmt_idx, ...
elemInclude(tx_include(elmt_idx),:), f_idx); REC = REC(:);
scaling(elmt_idx) = (REC_SIM(:)'*REC(:)) / ...
(REC_SIM(:)'*REC_SIM(:)); % Source Scaling
ADJ_SRC_elmt = zeros(Nyi, Nxi);
ADJ_SRC_elmt(ind(elemInclude(tx_include(elmt_idx),:))) = ...
scaling(elmt_idx)*REC_SIM - REC;
ADJ_SRC(:,:,elmt_idx) = ADJ_SRC_elmt;
end
% Backproject Error
ADJ_WVFIELD = HS.solve(ADJ_SRC,true);
SCALING = repmat(reshape(scaling, [1,1,numel(scaling)]), [Nyi, Nxi, 1]);
BACKPROJ = -real(conj(SCALING.*VIRT_SRC).*ADJ_WVFIELD);
% Accumulate Gradient Over Each Element
for elmt_idx = 1:numel(tx_include)
% Accumulate Backprojection
gradient_img = gradient_img + BACKPROJ(:,:,elmt_idx);
showAnimation = false; % Show Backprojection Animation
if showAnimation
% Visualize Numerical Solution
imagesc(xi,yi,gradient_img)
xlabel('Lateral [m]'); ylabel('Axial [m]'); axis image;
title(['Gradient Iteration ', num2str(iter)]);
colorbar; colormap gray; drawnow;
end
end
% Remove Ringing from Gradient Image
gradient_img = ringingRemovalFilt(xi, yi, gradient_img, c0, fDATA(f_idx), cutoff, ord);
% Step 2: Compute New Conjugate Gradient Search Direction from Gradient
% Conjugate Gradient Direction Scaling Factor for Updates
if ((iter_f_idx == 1) || (iter_f_idx == 1+niterSoSPerFreq(f_idx)))
beta = 0;
else
betaPR = (gradient_img(:)'*...
(gradient_img(:)-gradient_img_prev(:))) / ...
(gradient_img_prev(:)'*gradient_img_prev(:));
betaFR = (gradient_img(:)'*gradient_img(:)) / ...
(gradient_img_prev(:)'*gradient_img_prev(:));
beta = min(max(betaPR,0),betaFR);
end
search_dir = beta*search_dir-gradient_img;
gradient_img_prev = gradient_img;
% Step 3: Compute Forward Projection of Current Search Direction
PERTURBED_WVFIELD = HS.solve(VIRT_SRC.*search_dir, false);
dREC_SIM = zeros(numel(tx_include), numElements);
for elmt_idx = 1:numel(tx_include)
% Forward Projection of Search Direction Image
PERTURBED_WVFIELD_elmt = PERTURBED_WVFIELD(:,:,elmt_idx);
dREC_SIM(elmt_idx,elemInclude(tx_include(elmt_idx),:)) = -permute(scaling(elmt_idx) * ...
PERTURBED_WVFIELD_elmt(ind(elemInclude(tx_include(elmt_idx),:))),[2,1]);
end
% Step 4: Perform a Linear Approximation of Exact Line Search
perc_step_size = 1; % (<1/2) Introduced to Improve Compliance with Strong Wolfe Conditions
alpha = -(gradient_img(:)'*search_dir(:))/(dREC_SIM(:)'*dREC_SIM(:));
if updateAttenuation % Update Complex Slowness
SI = sign(sign_conv) * imag(SLOW_ESTIM) + perc_step_size * alpha * search_dir;
SLOW_ESTIM = real(SLOW_ESTIM) + 1i * sign(sign_conv) * SI;
else
SLOW_ESTIM = SLOW_ESTIM + perc_step_size * alpha * search_dir;
end
VEL_ESTIM = 1./real(SLOW_ESTIM); % Wave Velocity Estimate [m/s]
ATTEN_ESTIM = 2*pi*imag(SLOW_ESTIM)*sign(sign_conv);
% Save Intermediate Results
VEL_ESTIM_ITER(:,:,iter) = VEL_ESTIM;
ATTEN_ESTIM_ITER(:,:,iter) = ATTEN_ESTIM;
GRAD_IMG_ITER(:,:,iter) = gradient_img;
SEARCH_DIR_ITER(:,:,iter) = search_dir;
% Visualize Numerical Solution
subplot(2,2,1); imagesc(xi,yi,VEL_ESTIM,crange);
title(['Estimated Wave Velocity ', num2str(iter)]); axis image;
xlabel('Lateral [m]'); ylabel('Axial [m]'); colorbar; colormap gray;
subplot(2,2,2); imagesc(xi,yi,Np2dB*slow2atten*ATTEN_ESTIM,attenrange);
title(['Estimated Attenuation ', num2str(iter)]); axis image;
xlabel('Lateral [m]'); ylabel('Axial [m]'); colorbar; colormap gray;
subplot(2,2,3); imagesc(xi,yi,search_dir)
xlabel('Lateral [m]'); ylabel('Axial [m]'); axis image;
title(['Search Direction Iteration ', num2str(iter)]); colorbar; colormap gray;
subplot(2,2,4); imagesc(xi,yi,-gradient_img)
xlabel('Lateral [m]'); ylabel('Axial [m]'); axis image;
title(['Gradient Iteration ', num2str(iter)]); colorbar; colormap gray;
drawnow; disp(['Iteration ', num2str(iter)]); toc;
end
end
% Plot Final Reconstructions
subplot(1,2,1); imagesc(xi,yi,VEL_ESTIM,crange);
title(['Estimated Wave Velocity ', num2str(iter)]); axis image;
xlabel('Lateral [m]'); ylabel('Axial [m]'); colorbar; colormap gray;
subplot(1,2,2); imagesc(xi,yi,Np2dB*slow2atten*ATTEN_ESTIM,attenrange);
title(['Estimated Attenuation ', num2str(iter)]); axis image;
xlabel('Lateral [m]'); ylabel('Axial [m]'); colorbar; colormap gray;
% Save the Result to File
filename_results = ['Results/', filename, '_WaveformInversionResults.mat'];
save(filename_results, '-v7.3', 'xi', 'yi', 'fDATA', 'niterAttenPerFreq', ...
'niterSoSPerFreq', 'VEL_ESTIM_ITER', 'ATTEN_ESTIM_ITER', 'GRAD_IMG_ITER', 'SEARCH_DIR_ITER')