-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculateElementLocations.m
More file actions
134 lines (119 loc) · 5.81 KB
/
calculateElementLocations.m
File metadata and controls
134 lines (119 loc) · 5.81 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
function [elementCoordinates] = calculateElementLocations(cal, cnc, xdr)
% Get sorted CNC position order
cncPositions = cnc.Positions;
cncPositions(cnc.PositionOrder,:) = cncPositions;
cncPositions = reshape(cncPositions, [cnc.PositionGridSize, 3]);
% Calculate normalised measured amplitude
normalisedAmp = permute(cal.MeasuredAmplitude,[2 1]);
normalisedAmp = reshape(normalisedAmp ./ mean(normalisedAmp,'all','omitnan'), [cnc.PositionGridSize xdr.NTotalElements]);
% Filter measured distances to remove outliers
measuredDistance = reshape(permute(cal.MeasuredDistance,[2 1]), [cnc.PositionGridSize xdr.NTotalElements]);
measuredDistance = filterInvalidInputs(measuredDistance, normalisedAmp, ...
cal.OutlierAmplitudeLimit, ...
cal.OutlierDistanceLimit(1:min(end,1)), ...
cal.OutlierDistanceLimit(2:min(end,2)), ...
3, cnc.PositionGridStep(1), cnc.PositionGridStep(2));
% Experiment assumes transducer is nominally 10mm from
% hydrophone.
nominalZ = 10e-3;
elemPos = zeros(3, xdr.NTotalElements);
% Find best fit parabolas for each element
fprintf('Calculating raw element coordinates using LSQNONLIN. This may take a while...\n');
try
lsqiter = 1e4;
lsqtol = 1e-12;
for elementIdx = 1:xdr.NTotalElements
waitbar.next(1);
% Filter first to ensure the function is differentiable:
[elemData, cncData] = removeNaN(measuredDistance(:,:,elementIdx), cncPositions);
if numel(elemData) < xdr.OutlierMinObservations
% Assume dead element
elemPos(:, elementIdx) = nan;
else
% Perform parabola best fit
fun = @(xyz)euclidDistance(xyz, cncData, elemData);
elemPos(:, elementIdx) = lsqnonlin(fun, ...
[median(cnc.PositionX), median(cnc.PositionY), nominalZ ], ...
[ min(cnc.PositionX), min(cnc.PositionY), nominalZ - 5e-3], ...
[ max(cnc.PositionX), max(cnc.PositionY), nominalZ + 5e-3], ...
optimoptions('LSQNONLIN', ...
'Display','off', ...
'FunctionTolerance',lsqtol, ...
'OptimalityTolerance',lsqtol, ...
'StepTolerance',lsqtol, ...
'MaxIterations', lsqiter ...
) ...
);
end
if ~mod(elementIdx,10)
fprintf('.');
end
end
elemPos = reshape(elemPos,[3, xdr.NElements]);
catch
warning('Failed to calculate element coordinates.');
end
fprintf('\n')
delete(waitbarCleanup);
% Plot the raw calculated locations to aid debugging
fig = figure();
clf(fig);
ax = axes(fig);
scatter3(ax, 1e3.*reshape(elemPos(1,:,:),[],1), ...
1e3.*reshape(elemPos(2,:,:),[],1), ...
1e3.*reshape(elemPos(3,:,:),[],1), ...
'DisplayName','Raw Coordinates');
title(ax, 'Raw Element Coordinates');
xlabel(ax, 'Lateral (mm)');
ylabel(ax, 'Elevation (mm)');
zlabel(ax, 'Depth (mm)');
% Determine global panel rotation using rigid SVD transform
xdrIdealLateral = xdr.IdealElementLateralCoordinates;
xdrIdealElevation = xdr.IdealElementElevationCoordinates;
xdrIdealDepth = xdr.IdealElementDepthCoordinates;
xdrIdealLocations = permute(cat(3, xdrIdealLateral, xdrIdealElevation, xdrIdealDepth),[3 1 2]);
[globalRotationMatrix, globalTranslationMatrix] = rigidTransformSVD(elemPos, xdrIdealLocations);
rawElementPosn = reshape(elemPos, 3, []);
globalElementPosn = globalRotationMatrix.' * (rawElementPosn - globalTranslationMatrix);
fig = figure();
clf(fig);
ax = axes(fig);
scatter3(ax, 1e3.*(rawElementPosn(1, :) - globalTranslationMatrix(1)), ...
1e3.*(rawElementPosn(2, :) - globalTranslationMatrix(2)), ...
1e3.*(rawElementPosn(3, :) - globalTranslationMatrix(3)), ...
'DisplayName','After Global Translation','MarkerEdgeColor','#77AC30');
title(ax, 'Element Coordinates');
legend(ax, 'Location', 'north');
xlabel(ax, 'Lateral (mm)');
ylabel(ax, 'Elevation (mm)');
zlabel(ax, 'Depth (mm)');
hold(ax,'on');
scatter3(ax, 1e3.*globalElementPosn(1, :), ...
1e3.*globalElementPosn(2, :), ...
1e3.*globalElementPosn(3, :), ...
'DisplayName','After Global Rotation','MarkerEdgeColor','#D95319');
globalElementPosn = reshape(globalElementPosn, [3, xdr.NElements]);
% Determine sub-panel rotation
latSubPanelSize = floor(xdr.NElements(1) ./ xdr.NSubPanels(1));
elevSubPanelSize = floor(xdr.NElements(2) ./ xdr.NSubPanels(2));
elementCoordinates = zeros(size(elemPos));
for elevSubIdx = 1:xdr.NSubPanels(2)
% For each elevation sub-panel, process each lateral
% sub-panel.
for latSubIdx = 1:xdr.NSubPanels(1)
elevRange = (1:elevSubPanelSize) + (elevSubIdx - 1) * elevSubPanelSize;
latRange = (1:latSubPanelSize ) + (latSubIdx - 1) * latSubPanelSize ;
xdrSubpanelIdealLocations = xdrIdealLocations(:, latRange, elevRange);
subpanelElementPosn = globalElementPosn(:, latRange, elevRange);
rawSubpanelElementPosn = reshape(xdrSubpanelIdealLocations, 3, []);
[subpanelRotationMatrix, subpanelTranslationMatrix] = rigidTransformSVD(subpanelElementPosn, xdrSubpanelIdealLocations);
subpanelElementPosn = (subpanelRotationMatrix * rawSubpanelElementPosn) + subpanelTranslationMatrix;
elementCoordinates(:, latRange, elevRange) = reshape(subpanelElementPosn,[3 latSubPanelSize, elevSubPanelSize]);
end
end
scatter3(ax, 1e3.*reshape(elementCoordinates(1, :),[],1), ...
1e3.*reshape(elementCoordinates(2, :),[],1), ...
1e3.*reshape(elementCoordinates(3, :),[],1), ...
'DisplayName','After Panel Optimisation','MarkerEdgeColor','#A2142F');
elementCoordinates = reshape(elementCoordinates,[3, xdr.NElements]);
end