forked from jorgengrythe/beamforming
-
Notifications
You must be signed in to change notification settings - Fork 6
/
examplesSteeredResponse.m
464 lines (359 loc) · 13.2 KB
/
examplesSteeredResponse.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
%% Examples on delay-and-sum response for different arrays and input signals
%% 1D-array case
% Create vectors of x- and y-coordinates of microphone positions
xPos = -0.8:0.2:0.8; % 1xP vector of x-positions [m]
yPos = zeros(1, numel(xPos)); % 1xP vector of y-positions [m]
zPos = zeros(1, numel(xPos));
elementWeights = ones(1, numel(xPos))/numel(xPos); % 1xP vector of weightings
% Define arriving angles and frequency of input signals
thetaArrivalAngles = [-30 10]; % degrees
phiArrivalAngles = [0 0]; % degrees
f = 800; % [Hz]
c = 340; % [m/s]
fs = 44.1e3; % [Hz]
% Define array scanning angles (1D, so phi = 0)
thetaScanAngles = -90:0.1:90; % degrees
phiScanAngles = 0; % degrees
% Create input signal
inputSignal = createSignal(xPos, yPos, zPos, f, c, fs, thetaArrivalAngles, phiArrivalAngles);
% Create steering vector/matrix
e = steeringVector(xPos, yPos, zPos, f, c, thetaScanAngles, phiScanAngles);
% Create cross spectral matrix
R = crossSpectralMatrix(inputSignal);
% Calculate delay-and-sum steered response
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Normalise spectrum
spectrumNormalized = abs(S)/max(abs(S));
%Convert to decibel
spectrumLog = 10*log10(spectrumNormalized);
%Plot array
fig1 = figure;
fig1.Color = 'w';
ax = axes('Parent', fig1);
scatter(ax, xPos, yPos, 20, 'filled')
axis(ax, 'square')
ax.XLim = [-1 1];
ax.YLim = [-1 1];
grid(ax, 'on')
title(ax, 'Microphone positions')
%Plot steered response with indicator lines
fig2 = figure;
fig2.Color = 'w';
ax = axes('Parent', fig2);
plot(ax, thetaScanAngles, spectrumLog)
grid(ax, 'on')
ax.XLim = [thetaScanAngles(1) thetaScanAngles(end)];
for j=1:numel(thetaArrivalAngles)
indx = find(thetaScanAngles >= thetaArrivalAngles(j), 1);
line(ax, [thetaScanAngles(indx) thetaScanAngles(indx)], ax.YLim, ...
'LineWidth', 1, 'Color', 'r', 'LineStyle', '--');
end
xlabel(ax, '\theta')
ylabel(ax, 'dB')
%% 1D-array case different source strengths
%Relative amplitude difference in decibel
amplitudes = [0 -5];
% Create input signal
inputSignal = createSignal(xPos, yPos, zPos, f, c, fs, thetaArrivalAngles, phiArrivalAngles, amplitudes);
% Input signal is changed so update cross spectral matrix
R = crossSpectralMatrix(inputSignal);
% Calculate delay-and-sum steered response, steering vector/matrix is same as before
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Normalise spectrum
spectrumNormalized = abs(S)/max(abs(S));
%Convert to decibel
spectrumLog = 10*log10(spectrumNormalized);
%Plot steered response with indicator lines
fig3 = figure;
fig3.Color = 'w';
ax = axes('Parent', fig3);
plot(ax, thetaScanAngles, spectrumLog)
grid(ax, 'on')
ax.XLim = [thetaScanAngles(1) thetaScanAngles(end)];
for j=1:numel(thetaArrivalAngles)
indx = find(thetaScanAngles >= thetaArrivalAngles(j), 1);
line(ax, [thetaScanAngles(indx) thetaScanAngles(indx)], ax.YLim, ...
'LineWidth', 1, 'Color', 'r', 'LineStyle', '--');
end
xlabel(ax, '\theta')
ylabel(ax, 'dB')
%% 2D-array case, spectrum in linear scale in UV-space
% Position of sensors and weighting of 2D array
% Create circular array
nElements = 20;
radius = 0.6;
[xPos, yPos] = pol2cart((0:1/nElements:1-1/nElements)*2*pi, ones(1,nElements)*radius);
zPos = zeros(1, numel(xPos));
elementWeights = ones(1,numel(xPos))/numel(xPos);
% Define arriving angles of input signals
thetaArrivalAngles = [30 20 30];
phiArrivalAngles = [10 70 210];
% Define array scanning angles
thetaScanAngles = -90:0.5:90;
phiScanAngles = 0:1:180;
% Create input signal
inputSignal = createSignal(xPos, yPos, zPos, f, c, fs, thetaArrivalAngles, phiArrivalAngles);
% Update steering vector and also save UV-space coordinates
[e, u, v] = steeringVector(xPos, yPos, zPos, f, c, thetaScanAngles, phiScanAngles);
% Update cross spectral matrix
R = crossSpectralMatrix(inputSignal);
% Calculate delay-and-sum steered response
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Normalise spectrum
spectrumNormalized = abs(S)/max(max(abs(S)));
%Plot array
fig3 = figure;
fig3.Color = 'w';
ax = axes('Parent', fig3);
scatter(ax, xPos, yPos, 20, 'filled')
axis(ax, 'square')
ax.XLim = [-1 1];
ax.YLim = [-1 1];
grid(ax, 'on')
title(ax, 'Microphone positions')
%Plot steered response in UV-space
fig4 = figure;
ax = axes('Parent', fig4);
surf(ax, u, v, spectrumNormalized, 'edgecolor', 'none', 'FaceAlpha', 0.8)
%Do some magic to make the figure look nice (black theme)
fig4.Color = 'k';
ax.Color = 'k';
cmap = colormap;
cmap(1,:) = [1 1 1]*0.2;
colormap(ax, cmap);
view(ax, 0, 90)
axis(ax, 'square')
ax.XColor = 'w';
ax.YColor = 'w';
ax.ZColor = 'w';
ax.XTickLabel = [];
ax.YTickLabel = [];
ax.ZTickLabel = [];
ax.XMinorGrid = 'on';
ax.YMinorGrid = 'on';
ax.ZMinorGrid = 'on';
ax.MinorGridColor = 'w';
ax.MinorGridLineStyle = '-';
xlabel(ax, 'u = sin(\theta)cos(\phi)')
ylabel(ax, 'v = sin(\theta)sin(\phi)')
%% 2D-array case, different source strengths, spectrum in linear scale in UV-space
%Relative amplitude difference between sources in decibel,
%could also have written amplitudes = [3 1 0]
amplitudes = [0 -2 -3];
% Create input signal
inputSignal = createSignal(xPos, yPos, zPos, f, c, fs, thetaArrivalAngles, phiArrivalAngles, amplitudes);
% Update cross spectral matrix (same scanning angles so steering vector is the same)
R = crossSpectralMatrix(inputSignal);
% Calculate delay-and-sum steered response
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Normalise spectrum
spectrumNormalized = abs(S)/max(max(abs(S)));
%Plot steered response in UV-space
fig5 = figure;
ax = axes('Parent', fig5);
surf(ax, u, v, spectrumNormalized, 'edgecolor', 'none', 'FaceAlpha', 0.8)
%Do some magic to make the figure look nice (black theme)
fig5.Color = 'k';
ax.Color = 'k';
cmap = colormap;
cmap(1,:) = [1 1 1]*0.2;
colormap(ax, cmap);
view(ax, 0, 90)
axis(ax, 'square')
ax.XColor = 'w';
ax.YColor = 'w';
ax.ZColor = 'w';
ax.XTickLabel = [];
ax.YTickLabel = [];
ax.ZTickLabel = [];
ax.XMinorGrid = 'on';
ax.YMinorGrid = 'on';
ax.ZMinorGrid = 'on';
ax.MinorGridColor = 'w';
ax.MinorGridLineStyle = '-';
xlabel(ax, 'u = sin(\theta)cos(\phi)')
ylabel(ax, 'v = sin(\theta)sin(\phi)')
%% 2D-array case, different source strengths, spectrum in logarithmic scale in UV-space
%Convert the delay-and-sum steered response to decibel
spectrumLog = 10*log10(spectrumNormalized);
%Plot steered response in UV-space
fig6 = figure;
ax = axes('Parent', fig6);
surf(ax, u, v, spectrumLog, 'edgecolor', 'none', 'FaceAlpha', 0.8)
%Do some magic to make the figure look nice (black theme)
fig6.Color = 'k';
ax.Color = 'k';
cmap = colormap;
cmap(1,:) = [1 1 1]*0.2;
colormap(ax, cmap);
view(ax, 0, 90)
axis(ax, 'square')
ax.XColor = 'w';
ax.YColor = 'w';
ax.ZColor = 'w';
ax.XTickLabel = [];
ax.YTickLabel = [];
ax.ZTickLabel = [];
ax.XMinorGrid = 'on';
ax.YMinorGrid = 'on';
ax.ZMinorGrid = 'on';
ax.MinorGridColor = 'w';
ax.MinorGridLineStyle = '-';
xlabel(ax, 'u = sin(\theta)cos(\phi)')
ylabel(ax, 'v = sin(\theta)sin(\phi)')
%% 2D-array case, different source strengths, spectrum in logarithmic scale with dynamic range in UV-space
%Dynamic range in decibels
dynamicRange = 6;
%Plot steered response in UV-space
fig7 = figure;
ax = axes('Parent', fig7);
surf(ax, u, v, spectrumLog, 'edgecolor', 'none', 'FaceAlpha', 0.8)
%Do some magic to make the figure look nice (black theme)
fig7.Color = 'k';
ax.Color = 'k';
cmap = colormap;
cmap(1,:) = [1 1 1]*0.2;
colormap(ax, cmap);
view(ax, 0, 90)
axis(ax, 'square')
ax.XColor = 'w';
ax.YColor = 'w';
ax.ZColor = 'w';
ax.XTickLabel = [];
ax.YTickLabel = [];
ax.ZTickLabel = [];
ax.XMinorGrid = 'on';
ax.YMinorGrid = 'on';
ax.ZMinorGrid = 'on';
ax.MinorGridColor = 'w';
ax.MinorGridLineStyle = '-';
xlabel(ax, 'u = sin(\theta)cos(\phi)')
ylabel(ax, 'v = sin(\theta)sin(\phi)')
%Set the dynamic range
caxis(ax, [-dynamicRange 0])
%% 2D-array case, different source strengths, use plotSteeredResponseUV function
%The function interpolates the result, so can use fewer scanning
%angles/points
thetaScanAngles = -90:2:90;
phiScanAngles = 0:2:180;
%Changed scanning angles so update steering vector (input signal is the
%same so we can keep R as before)
[e, u, v, w] = steeringVector(xPos, yPos, zPos, f, c, thetaScanAngles, phiScanAngles);
% Calculate delay-and-sum steered response
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Plot the steered response, no need to normalise the
%spectrum or converting to decibel before input
plotSteeredResponseUV(S, u, v, w, 'uv', 'log', 'black', '2D')
%% 2D-array case, scanning plane in cartesian coordinates
%Define scanning grid
distanceToScanningPlane = 1.8; %[m]
maxScanningPlaneExtentX = 2; %[m];
maxScanningPlaneExtentY = 1.5; %[m];
numberOfScanningPointsX = 40;
numberOfScanningPointsY = 30;
%Define sources
xPosSource = [-1 0 1];
yPosSource = [-0.5 0.75 0.25];
zPosSource = ones(1, numel(xPosSource))*distanceToScanningPlane;
amplitudes = [0 -1 -2];
%Create scanning points
scanningAxisX = -maxScanningPlaneExtentX:2*maxScanningPlaneExtentX/(numberOfScanningPointsX-1):maxScanningPlaneExtentX;
scanningAxisY = -maxScanningPlaneExtentY:2*maxScanningPlaneExtentY/(numberOfScanningPointsY-1):maxScanningPlaneExtentY;
[scanningPointsX, scanningPointsY] = meshgrid(scanningAxisX, scanningAxisY);
%Get angles to scanning points and source positions
[thetaScanAngles, phiScanAngles] = convertCartesianToSpherical(scanningPointsX, scanningPointsY, distanceToScanningPlane);
[thetaArrivalAngles, phiArrivalAngles] = convertCartesianToSpherical(xPosSource, yPosSource, zPosSource);
% Create input signal
inputSignal = createSignal(xPos, yPos, zPos, f, c, fs, thetaArrivalAngles, phiArrivalAngles, amplitudes);
%Calculate steering vector
[e, u, v, w] = steeringVector(xPos, yPos, zPos, f, c, thetaScanAngles, phiScanAngles);
%Calculate cross spectral matrix
R = crossSpectralMatrix(inputSignal);
% Calculate delay-and-sum steered response
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Plot the steered response in cartesian coordinate system rather than UV
interpolationFactor = 2; %interpolate for higher resolution, 0 equals original
plotSteeredResponseXY(S, scanningPointsX, scanningPointsY, interpolationFactor)
%See the scanning grid and calculated points in UV-space as well
plotSteeredResponseUV(S, u, v, w, 'uv', 'log', 'white', '2D')
%% 3D-array case
% Position of sensors and weighting of 3D array
% Create spherical array
[xPos, yPos, zPos] = sphere(9);
xPos = xPos(:);
yPos = yPos(:);
zPos = zPos(:);
elementWeights = ones(1, numel(xPos))/numel(xPos);
% Define arriving angles of input signals
thetaArrivalAngles = [0 45 100];
phiArrivalAngles = [0 -60 -30];
% Define array scanning angles
thetaScanAngles = 0:1:360;
phiScanAngles = 0:2:180;
%Source frequency
f = 600; % [Hz]
%Default dynamic range [dB]
dynamicRange = 6;
% Create input signal
inputSignal = createSignal(xPos, yPos, zPos, f, c, fs, thetaArrivalAngles, phiArrivalAngles);
% Update steering vector and also save UV-space coordinates
[e, u, v, w] = steeringVector(xPos, yPos, zPos, f, c, thetaScanAngles, phiScanAngles);
% Update cross spectral matrix
R = crossSpectralMatrix(inputSignal);
% Calculate delay-and-sum steered response
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Normalise and convert to decibel spectrum
spectrumNormalized = abs(S)/max(max(abs(S)));
%Convert the delay-and-sum steered response to decibel
spectrumLog = 10*log10(spectrumNormalized);
% Plot array geometry
fig8 = figure;
fig8.Color = 'w';
ax = axes('Parent', fig8);
scatter3(ax, xPos, yPos, zPos, 20, 'filled')
axis(ax, 'square')
ax.XLim = [-1 1];
ax.YLim = [-1 1];
ax.ZLim = [-1 1];
grid(ax, 'on')
title(ax, 'Microphone positions')
%Plot steered response in UVW-space
fig9 = figure;
ax = axes('Parent', fig9);
surf(ax, u, v, w, spectrumLog, 'edgecolor', 'none', 'FaceAlpha', 0.8)
%Do some magic to make the figure look nice (black theme)
fig9.Color = 'k';
ax.Color = 'k';
cmap = colormap;
cmap(1,:) = [1 1 1]*0.2;
colormap(ax, cmap);
view(ax, 40, 40)
axis(ax, 'square')
ax.XColor = 'w';
ax.YColor = 'w';
ax.ZColor = 'w';
ax.XTickLabel = [];
ax.YTickLabel = [];
ax.ZTickLabel = [];
ax.XMinorGrid = 'on';
ax.YMinorGrid = 'on';
ax.ZMinorGrid = 'on';
ax.MinorGridColor = 'w';
ax.MinorGridLineStyle = '-';
xlabel(ax, 'u = sin(\theta)cos(\phi)')
ylabel(ax, 'v = sin(\theta)sin(\phi)')
zlabel(ax, 'w = cos(\theta)')
%Set the dynamic range
caxis(ax, [-dynamicRange 0])
%% 3D-array case, use plotSteeredResponseUV function
%The function interpolates the result, so can use fewer scanning angles/points
thetaScanAngles = 0:2:360;
phiScanAngles = 0:4:180;
%Changed scanning angles so update steering vector (input signal is the
%same so we can keep R as before)
[e, u, v, w] = steeringVector(xPos, yPos, zPos, f, c, thetaScanAngles, phiScanAngles);
% Calculate delay-and-sum steered response
S = steeredResponseDelayAndSum(R, e, elementWeights);
%Plot the steered response, no need to normalise the
%spectrum or converting to decibel before input
plotSteeredResponseUV(S, u, v, w, 'uvw', 'log', 'black', '3D')