9
9
plt.rcParams['axes.grid'] = True
10
10
11
11
"""
12
-
12
+ from __future__ import division # for Python 2.x
13
13
import numpy as np
14
14
from . import util
15
15
16
16
17
- def linear (N , dx , center = [0 , 0 , 0 ], n0 = [1 , 0 , 0 ]):
17
+ def linear (N , spacing , center = [0 , 0 , 0 ], n0 = [1 , 0 , 0 ]):
18
18
"""Linear secondary source distribution.
19
19
20
+ Parameters
21
+ ----------
22
+ N : int
23
+ Number of loudspeakers.
24
+ spacing : float
25
+ Distance (in metres) between loudspeakers.
26
+ center : (3,) array_like, optional
27
+ Coordinates of array center.
28
+ n0 : (3,) array_like, optional
29
+ Normal vector of array.
30
+
31
+ Returns
32
+ -------
33
+ positions : (N, 3) numpy.ndarray
34
+ Positions of secondary sources
35
+ directions : (N, 3) numpy.ndarray
36
+ Orientations (normal vectors) of secondary sources
37
+ weights : (N,) numpy.ndarray
38
+ Weights of secondary sources
39
+
20
40
Example
21
41
-------
22
42
.. plot::
@@ -27,13 +47,12 @@ def linear(N, dx, center=[0, 0, 0], n0=[1, 0, 0]):
27
47
plt.axis('equal')
28
48
29
49
"""
30
- center = np .squeeze (np .asarray (center , dtype = np .float64 ))
31
50
positions = np .zeros ((N , 3 ))
32
- positions [:, 1 ] = (np .arange (N ) - N / 2 + 1 / 2 ) * dx
51
+ positions [:, 1 ] = (np .arange (N ) - N / 2 + 1 / 2 ) * spacing
33
52
positions , directions = _rotate_array (positions , [1 , 0 , 0 ], [1 , 0 , 0 ], n0 )
34
- directions = np .tile (directions , (N , 1 ))
35
53
positions += center
36
- weights = dx * np .ones (N )
54
+ directions = np .tile (directions , (N , 1 ))
55
+ weights = spacing * np .ones (N )
37
56
return positions , directions , weights
38
57
39
58
@@ -50,7 +69,6 @@ def linear_nested(N, dx1, dx2, center=[0, 0, 0], n0=[1, 0, 0]):
50
69
plt.axis('equal')
51
70
52
71
"""
53
-
54
72
# first segment
55
73
x00 , n00 , a00 = linear (N // 3 , dx2 , center = [0 , - N // 6 * (dx1 + dx2 ), 0 ])
56
74
positions = x00
@@ -68,7 +86,6 @@ def linear_nested(N, dx1, dx2, center=[0, 0, 0], n0=[1, 0, 0]):
68
86
# shift and rotate array
69
87
positions , directions = _rotate_array (positions , directions , [1 , 0 , 0 ], n0 )
70
88
positions += center
71
-
72
89
return positions , directions , weights
73
90
74
91
@@ -93,14 +110,13 @@ def linear_random(N, dy1, dy2, center=[0, 0, 0], n0=[1, 0, 0]):
93
110
positions [m , 1 ] = positions [m - 1 , 1 ] + dist [m - 1 ]
94
111
# weights of secondary sources
95
112
weights = weights_linear (positions )
96
- # directions of scondary sources
113
+ # directions of secondary sources
97
114
directions = np .tile ([1 , 0 , 0 ], (N , 1 ))
98
- # shift array to center
115
+ # shift array to origin
99
116
positions [:, 1 ] -= positions [- 1 , 1 ] / 2
100
117
# shift and rotate array
101
118
positions , directions = _rotate_array (positions , directions , [1 , 0 , 0 ], n0 )
102
119
positions += center
103
-
104
120
return positions , directions , weights
105
121
106
122
@@ -113,11 +129,11 @@ def circular(N, R, center=[0, 0, 0]):
113
129
:context: close-figs
114
130
115
131
x0, n0, a0 = sfs.array.circular(16, 1)
116
- sfs.plot.loudspeaker_2d(x0, n0, a0)
132
+ sfs.plot.loudspeaker_2d(x0, n0, a0, size=0.2, show_numbers=True )
117
133
plt.axis('equal')
118
134
119
135
"""
120
- center = np . squeeze ( np . asarray ( center , dtype = np .float64 ) )
136
+ center = util . asarray_1d ( center , dtype = np .float64 )
121
137
positions = np .tile (center , (N , 1 ))
122
138
alpha = np .linspace (0 , 2 * np .pi , N , endpoint = False )
123
139
positions [:, 0 ] += R * np .cos (alpha )
@@ -138,11 +154,10 @@ def rectangular(Nx, dx, Ny, dy, center=[0, 0, 0], n0=None):
138
154
:context: close-figs
139
155
140
156
x0, n0, a0 = sfs.array.rectangular(8, 0.2, 4, 0.2)
141
- sfs.plot.loudspeaker_2d(x0, n0, a0)
157
+ sfs.plot.loudspeaker_2d(x0, n0, a0, show_numbers=True )
142
158
plt.axis('equal')
143
159
144
160
"""
145
-
146
161
# left array
147
162
x00 , n00 , a00 = linear (Ny , dy )
148
163
positions = x00
@@ -167,15 +182,14 @@ def rectangular(Nx, dx, Ny, dy, center=[0, 0, 0], n0=None):
167
182
positions = np .concatenate ((positions , x00 ))
168
183
directions = np .concatenate ((directions , n00 ))
169
184
weights = np .concatenate ((weights , a00 ))
170
- # shift array to center
171
- positions -= np . asarray ( [Nx / 2 * dx , 0 , 0 ])
185
+ # shift array to origin
186
+ positions -= [Nx / 2 * dx , 0 , 0 ]
172
187
# rotate array
173
188
if n0 is not None :
174
189
positions , directions = _rotate_array (positions , directions ,
175
190
[1 , 0 , 0 ], n0 )
176
191
# shift array to desired position
177
- positions += np .asarray (center )
178
-
192
+ positions += center
179
193
return positions , directions , weights
180
194
181
195
@@ -213,7 +227,6 @@ def rounded_edge(Nxy, Nr, dx, center=[0, 0, 0], n0=None):
213
227
plt.axis('equal')
214
228
215
229
"""
216
-
217
230
# radius of rounded edge
218
231
Nr += 1
219
232
R = 2 / np .pi * Nr * dx
@@ -253,14 +266,12 @@ def rounded_edge(Nxy, Nr, dx, center=[0, 0, 0], n0=None):
253
266
positions , directions = _rotate_array (positions , directions ,
254
267
[1 , 0 , 0 ], n0 )
255
268
# shift array to desired position
256
- positions += np .asarray (center )
257
-
269
+ positions += center
258
270
return positions , directions , weights
259
271
260
272
261
273
def planar (Ny , dy , Nz , dz , center = [0 , 0 , 0 ], n0 = None ):
262
274
"""Planar secondary source distribtion."""
263
- center = np .squeeze (np .asarray (center , dtype = np .float64 ))
264
275
# initialize vectors for later np.concatenate
265
276
positions = np .zeros ((1 , 3 ))
266
277
directions = np .zeros ((1 , 3 ))
@@ -277,14 +288,12 @@ def planar(Ny, dy, Nz, dz, center=[0, 0, 0], n0=None):
277
288
positions , directions = _rotate_array (positions , directions ,
278
289
[1 , 0 , 0 ], n0 )
279
290
# shift array to desired position
280
- positions += np .asarray (center )
281
-
291
+ positions += center
282
292
return positions , directions , weights
283
293
284
294
285
295
def cube (Nx , dx , Ny , dy , Nz , dz , center = [0 , 0 , 0 ], n0 = None ):
286
296
"""Cube shaped secondary source distribtion."""
287
- center = np .squeeze (np .asarray (center , dtype = np .float64 ))
288
297
# left array
289
298
x00 , n00 , a00 = planar (Ny , dy , Nz , dz )
290
299
positions = x00
@@ -323,15 +332,14 @@ def cube(Nx, dx, Ny, dy, Nz, dz, center=[0, 0, 0], n0=None):
323
332
positions = np .concatenate ((positions , x00 ))
324
333
directions = np .concatenate ((directions , n00 ))
325
334
weights = np .concatenate ((weights , a00 ))
326
- # shift array to center
327
- positions -= np . asarray ( [Nx / 2 * dx , 0 , 0 ])
335
+ # shift array to origin
336
+ positions -= [Nx / 2 * dx , 0 , 0 ]
328
337
# rotate array
329
338
if n0 is not None :
330
339
positions , directions = _rotate_array (positions , directions ,
331
340
[1 , 0 , 0 ], n0 )
332
341
# shift array to desired position
333
- positions += np .asarray (center )
334
-
342
+ positions += center
335
343
return positions , directions , weights
336
344
337
345
@@ -344,9 +352,8 @@ def sphere_load(fname, radius, center=[0, 0, 0]):
344
352
"""
345
353
x0 = np .loadtxt (fname )
346
354
weights = x0 [:, 3 ]
347
- directions = - x0 [:, 0 :3 ]
348
- positions = center + radius * x0 [:, 0 :3 ]
349
-
355
+ directions = - x0 [:, :3 ]
356
+ positions = center + radius * x0 [:, :3 ]
350
357
return positions , directions , weights
351
358
352
359
@@ -366,8 +373,7 @@ def load(fname, center=[0, 0, 0], n0=None):
366
373
positions , directions = _rotate_array (positions , directions ,
367
374
[1 , 0 , 0 ], n0 )
368
375
# shift array to desired position
369
- positions += np .asarray (center )
370
-
376
+ positions += center
371
377
return positions , directions , weights
372
378
373
379
@@ -384,7 +390,6 @@ def weights_linear(positions):
384
390
for m in range (1 , N - 1 ):
385
391
weights [m ] = 0.5 * (dy [m - 1 ] + dy [m ])
386
392
weights [- 1 ] = dy [- 1 ]
387
-
388
393
return np .abs (weights )
389
394
390
395
@@ -397,7 +402,7 @@ def weights_closed(positions):
397
402
contour.
398
403
399
404
"""
400
- positions = np . asarray (positions )
405
+ positions = util . asarray_of_rows (positions )
401
406
if len (positions ) == 0 :
402
407
weights = []
403
408
elif len (positions ) == 1 :
@@ -406,7 +411,7 @@ def weights_closed(positions):
406
411
successors = np .roll (positions , - 1 , axis = 0 )
407
412
d = [np .linalg .norm (b - a ) for a , b in zip (positions , successors )]
408
413
weights = [0.5 * (a + b ) for a , b in zip (d , d [- 1 :] + d )]
409
- return weights
414
+ return np . array ( weights )
410
415
411
416
412
417
def _rotate_array (x0 , n0 , n1 , n2 ):
0 commit comments