-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotBeamPattern2.m
executable file
·320 lines (269 loc) · 9.44 KB
/
plotBeamPattern2.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
function plotBeamPattern(B,varargin)
% PLOTBEAMPATTERN plots an individual measured 3D beam
%
% Beam is a struct containing the 3-dimensional array of complex beam data,
% and indices for each dimension.
%
% beam.FFT - i x j x k complex matrix azimuth, elevation, and frequency
% beam.HSA1 - i x j x k complex matrix azimuth, elevation, and frequency
% beam.HSA2 - i x j x k complex matrix azimuth, elevation, and frequency
% beam.el - i-element vector of elevation indices
% beam.az - j-element vector of azimuth indices
% beam.f - k-element vector of frequency indices
% beam.EL - i x j matrix of elevation for each element in Z
% beam.AZ - i x j matrix of azimuth for each element in Z
%
% To generate X and Y use:
% [beam.AZ, beam.EL] = meshgrid(beam.az, beam.el);
fprintf('\n\n*****************************************\n')
fprintf('Plotting beam pattern\n\n')
%% set default parameters
% data "massaging" parameters
smMETH = 'box'; % kernel type for smooth3.m
smSIZE = 11; % kernel size for smooth3.m (set to 1 for disable)
% volume plotting options
%TBD
% surface plotting options
cMap = 'hot';%flipud(hot); %jet; % colormap
dBrange = 35; % colorscale depth
dBnorm = false; % normalize to peak?
azView = 20; % azimuth angle
elView = 20; % elevation angle
zRange = [-80 -30];
% contour plotting options
contourLev = -3; % dB contour level for each frequency line [dB]
colors = {'k','b','g','m','c','r'};
%% optional parameters
FREQ = [];
PLOTMODE = 'surf'; %'cont'
DATAMODE = 'fft'; %'hsa1'; %
FH = nan;
switch (nargin)
case 1
case 2
FREQ = varargin{1};
case 3
FREQ = varargin{1};
PLOTMODE = varargin{2};
case 4
FREQ = varargin{1};
PLOTMODE = varargin{2};
DATAMODE = varargin{3};
case 5
FREQ = varargin{1};
PLOTMODE = varargin{2};
DATAMODE = varargin{3};
FH = varargin{4};
otherwise
error('Incorrect number of input arguments')
end
% use specified data set
if strcmp(DATAMODE,'fft')
B.Z = B.FFT;
elseif strcmp(DATAMODE,'hsa1')
B.Z = B.HSA1;
elseif strcmp(DATAMODE,'hsa2')
B.Z = B.HSA2;
else
error('Unknown DATAMODE parameter. Should be one of ''fft'', ''hsa1'', or ''hsa2''.')
end
% get indices of desired frequency points
if isempty(FREQ)
fIdx = 1:numel(B.f);
else
fIdx = zeros(1,numel(FREQ));
for i = 1:numel(FREQ)
res = find(B.f >= FREQ(i), 1, 'first');
if ~isempty(res)
fIdx(i) = res;
end
end
fIdx = unique(fIdx); % remove dups
fIdx(fIdx == 0) = []; % remove zeros
end
% figure handle
if ishandle(FH)
fh = FH; % get users figure handle
%if numel(fh) ~= numel(fIdx)
%fh(end+1) = repmat(fh(end),1,numel(fIdx)-numel(fh));
%end
else
fh = nan;%(1,numel(fIdx)); % create array of figure handles to use
end
%% smooth out data before plotting
B.Z = smoothn(B.Z,1e-4); % interpolate NaN values (especially at missing corners)
B.Z = smooth3(B.Z,smMETH,smSIZE); % smooth data in all 3 dimensions
%% plot resulting beam patterns
switch PLOTMODE
% plot volumetric plot
case 'vol'
% convert to volumetric data
%%% use TriScatteredInterp with 3D uniform meshgrid
% N = 10; % number of intensity levels
%
% x = repmat(B.AZ,[1 1 N]);
% y = repmat(B.EL,[1 1 N]);
% z = zeros(numel(B.el), numel(B.az), N);
% for i = 1:numel(B.f)
% z(:,:,i) = B.f(i);
% end
% construct 3D patches for each surface
for i = fIdx
if ishandle(fh)
figure(fh);
else
fh = figure;
end
fvc = surf2patch(B.AZ,B.EL,B.FFT(:,:,i),B.FFT(:,:,i));
p{i} = patch(fvc);
shading faceted;
set(p{i},'FaceColor','r')
view(3)
drawnow
end
% see http:https://www.mathworks.com/matlabcentral/newsreader/view_thread/169205
case 'sph'
% iterate over each frequency beam
for i = fIdx
% convert (az,el,rho) data points to cartesian coordinates
z = -B.Z(:,:,i) .* cos(B.AZ*pi/180) .* cos(B.EL*pi/180);
x = B.Z(:,:,i) .* sin(B.AZ*pi/180) .* cos(B.EL*pi/180);
y = B.Z(:,:,i) .* sin(B.EL*pi/180);
% find peak value
dBpeak = max(max(B.Z(:,:,i)));
fprintf('Peak @ %g kHz = %2.1f dB\n', B.f(i)*1e-3, dBpeak)
% plot interpolated surface
if ishandle(fh)
figure(fh);
else
fh = figure;
end
surf(x,y,z+dBpeak);
% view(azView,elView)
shading interp
lh = light;
lighting phong
%lightangle(45,45)
%axis equal
%set(gca,'ZLim',[dBpeak-55 dBpeak]);
colormap(cMap);
if dBnorm
cRange = [-dBrange 0];
else
cRange = [dBpeak-dBrange dBpeak];
end
set(gca,'CLim',cRange);
colorbar
title(sprintf('%g kHz', B.f(i)*1e-3),'fontsize',16)
xlabel('az (\circ)','fontsize',16)
ylabel('el (\circ)','fontsize',16)
zlabel('dB','fontsize',16)
set(gca,'fontsize',16);
set(gcf,'color','w');
end
% plot 3D surface plots for each frequency bin
case 'surf'
% iterate over each frequency beam
for i = fIdx
% find peak value
dBpeak = max(max(B.Z(:,:,i)));
fprintf('Peak @ %g kHz = %2.1f dB\n', B.f(i)*1e-3, dBpeak)
% plot interpolated surface
if ishandle(fh)
figure(fh);
else
fh = figure;
end
if dBnorm
surfc(B.AZ, B.EL, B.Z(:,:,i)-dBpeak);
else
surfc(B.AZ, B.EL, B.Z(:,:,i));
end
view(azView,elView)
shading interp
lh = light;
lighting phong
%lightangle(45,45)
%axis equal
%set(gca,'ZLim',[dBpeak-55 dBpeak]);
colormap(cMap);
if dBnorm
cRange = [-dBrange 0];
else
cRange = zRange;
set(gca,'ZLim',zRange);
end
set(gca,'CLim',cRange);
colorbar
title(sprintf('%g kHz', B.f(i)*1e-3),'fontsize',16)
xlabel('az (\circ)','fontsize',16)
ylabel('el (\circ)','fontsize',16)
zlabel('dB','fontsize',16)
set(gca,'fontsize',16);
set(gcf,'color','w');
end
% plot contours over all frequencies
case 'cont'
if ishandle(fh)
figure(fh);
else
fh = figure;
end
hold on;
dBlevels = contourLev * [1 1];
for i = fIdx
dBpeak = max(max(B.Z(:,:,i)));
fprintf('Peak @ %g kHz = %2.1f dB\n', B.f(i)*1e-3, dBpeak)
contour(B.AZ, B.EL, B.Z(:,:,i),dBpeak+dBlevels,colors{mod(i-1,length(colors))+1},'linewidth',2);
end
grid on;
legend(num2str(1e-3*B.f(fIdx)'))
% plot horizontal slice
case 'horz'
k = 28; % select an elevation
if ishandle(fh)
figure(fh);
else
fh = figure;
end
hold on;
for i = fIdx
% find peak value
dBpeak = max(max(B.Z(:,:,i)));
fprintf('Peak @ %g kHz = %2.1f dB\n', B.f(i)*1e-3, dBpeak)
if dBnorm, zMod = dBpeak; else, zMod = 0; end
plot(B.az, B.Z(k,:,i) - zMod, ...
colors{mod(i-1,length(colors))+1}, ...
'linewidth',2)
end
grid on;
legend(num2str(1e-3*B.f(fIdx)'))
xlabel('Azimuth (deg)')
ylabel('Magnitude (dB)')
title(sprintf('Horizontal Beam Pattern @ %.1f degrees',B.el(k)))
% plot vertical slice
case 'vert'
k = 16; % select an azimuth
if ishandle(fh)
figure(fh);
else
fh = figure;
end
hold on;
for i = fIdx
% find peak value
dBpeak = max(max(B.Z(:,:,i)));
fprintf('Peak @ %g kHz = %2.1f dB\n', B.f(i)*1e-3, dBpeak)
if dBnorm, zMod = dBpeak; else, zMod = 0; end
plot(B.el, B.Z(:,k,i) - zMod, ...
colors{mod(i-1,length(colors))+1}, ...
'linewidth',2)
end
grid on;
legend(num2str(1e-3*B.f(fIdx)'))
xlabel('Elevation (deg)')
ylabel('Magnitude (dB)')
title(sprintf('Horizontal Beam Pattern @ %.1f degrees',B.el(k)))
otherwise
error('Unknown PLOTMODE parameter. Should be one of ''vol'', ''surf'', or ''cont''.')
end