99 plt.rcParams['axes.grid'] = True
1010
1111"""
12-
12+ from __future__ import division # for Python 2.x
1313import numpy as np
1414from . import util
1515
1616
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 ]):
1818 """Linear secondary source distribution.
1919
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+
2040 Example
2141 -------
2242 .. plot::
@@ -27,13 +47,12 @@ def linear(N, dx, center=[0, 0, 0], n0=[1, 0, 0]):
2747 plt.axis('equal')
2848
2949 """
30- center = np .squeeze (np .asarray (center , dtype = np .float64 ))
3150 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
3352 positions , directions = _rotate_array (positions , [1 , 0 , 0 ], [1 , 0 , 0 ], n0 )
34- directions = np .tile (directions , (N , 1 ))
3553 positions += center
36- weights = dx * np .ones (N )
54+ directions = np .tile (directions , (N , 1 ))
55+ weights = spacing * np .ones (N )
3756 return positions , directions , weights
3857
3958
@@ -50,7 +69,6 @@ def linear_nested(N, dx1, dx2, center=[0, 0, 0], n0=[1, 0, 0]):
5069 plt.axis('equal')
5170
5271 """
53-
5472 # first segment
5573 x00 , n00 , a00 = linear (N // 3 , dx2 , center = [0 , - N // 6 * (dx1 + dx2 ), 0 ])
5674 positions = x00
@@ -68,7 +86,6 @@ def linear_nested(N, dx1, dx2, center=[0, 0, 0], n0=[1, 0, 0]):
6886 # shift and rotate array
6987 positions , directions = _rotate_array (positions , directions , [1 , 0 , 0 ], n0 )
7088 positions += center
71-
7289 return positions , directions , weights
7390
7491
@@ -93,14 +110,13 @@ def linear_random(N, dy1, dy2, center=[0, 0, 0], n0=[1, 0, 0]):
93110 positions [m , 1 ] = positions [m - 1 , 1 ] + dist [m - 1 ]
94111 # weights of secondary sources
95112 weights = weights_linear (positions )
96- # directions of scondary sources
113+ # directions of secondary sources
97114 directions = np .tile ([1 , 0 , 0 ], (N , 1 ))
98- # shift array to center
115+ # shift array to origin
99116 positions [:, 1 ] -= positions [- 1 , 1 ] / 2
100117 # shift and rotate array
101118 positions , directions = _rotate_array (positions , directions , [1 , 0 , 0 ], n0 )
102119 positions += center
103-
104120 return positions , directions , weights
105121
106122
@@ -113,11 +129,11 @@ def circular(N, R, center=[0, 0, 0]):
113129 :context: close-figs
114130
115131 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 )
117133 plt.axis('equal')
118134
119135 """
120- center = np . squeeze ( np . asarray ( center , dtype = np .float64 ) )
136+ center = util . asarray_1d ( center , dtype = np .float64 )
121137 positions = np .tile (center , (N , 1 ))
122138 alpha = np .linspace (0 , 2 * np .pi , N , endpoint = False )
123139 positions [:, 0 ] += R * np .cos (alpha )
@@ -138,11 +154,10 @@ def rectangular(Nx, dx, Ny, dy, center=[0, 0, 0], n0=None):
138154 :context: close-figs
139155
140156 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 )
142158 plt.axis('equal')
143159
144160 """
145-
146161 # left array
147162 x00 , n00 , a00 = linear (Ny , dy )
148163 positions = x00
@@ -167,15 +182,14 @@ def rectangular(Nx, dx, Ny, dy, center=[0, 0, 0], n0=None):
167182 positions = np .concatenate ((positions , x00 ))
168183 directions = np .concatenate ((directions , n00 ))
169184 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 ]
172187 # rotate array
173188 if n0 is not None :
174189 positions , directions = _rotate_array (positions , directions ,
175190 [1 , 0 , 0 ], n0 )
176191 # shift array to desired position
177- positions += np .asarray (center )
178-
192+ positions += center
179193 return positions , directions , weights
180194
181195
@@ -213,7 +227,6 @@ def rounded_edge(Nxy, Nr, dx, center=[0, 0, 0], n0=None):
213227 plt.axis('equal')
214228
215229 """
216-
217230 # radius of rounded edge
218231 Nr += 1
219232 R = 2 / np .pi * Nr * dx
@@ -253,14 +266,12 @@ def rounded_edge(Nxy, Nr, dx, center=[0, 0, 0], n0=None):
253266 positions , directions = _rotate_array (positions , directions ,
254267 [1 , 0 , 0 ], n0 )
255268 # shift array to desired position
256- positions += np .asarray (center )
257-
269+ positions += center
258270 return positions , directions , weights
259271
260272
261273def planar (Ny , dy , Nz , dz , center = [0 , 0 , 0 ], n0 = None ):
262274 """Planar secondary source distribtion."""
263- center = np .squeeze (np .asarray (center , dtype = np .float64 ))
264275 # initialize vectors for later np.concatenate
265276 positions = np .zeros ((1 , 3 ))
266277 directions = np .zeros ((1 , 3 ))
@@ -277,14 +288,12 @@ def planar(Ny, dy, Nz, dz, center=[0, 0, 0], n0=None):
277288 positions , directions = _rotate_array (positions , directions ,
278289 [1 , 0 , 0 ], n0 )
279290 # shift array to desired position
280- positions += np .asarray (center )
281-
291+ positions += center
282292 return positions , directions , weights
283293
284294
285295def cube (Nx , dx , Ny , dy , Nz , dz , center = [0 , 0 , 0 ], n0 = None ):
286296 """Cube shaped secondary source distribtion."""
287- center = np .squeeze (np .asarray (center , dtype = np .float64 ))
288297 # left array
289298 x00 , n00 , a00 = planar (Ny , dy , Nz , dz )
290299 positions = x00
@@ -323,15 +332,14 @@ def cube(Nx, dx, Ny, dy, Nz, dz, center=[0, 0, 0], n0=None):
323332 positions = np .concatenate ((positions , x00 ))
324333 directions = np .concatenate ((directions , n00 ))
325334 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 ]
328337 # rotate array
329338 if n0 is not None :
330339 positions , directions = _rotate_array (positions , directions ,
331340 [1 , 0 , 0 ], n0 )
332341 # shift array to desired position
333- positions += np .asarray (center )
334-
342+ positions += center
335343 return positions , directions , weights
336344
337345
@@ -344,9 +352,8 @@ def sphere_load(fname, radius, center=[0, 0, 0]):
344352 """
345353 x0 = np .loadtxt (fname )
346354 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 ]
350357 return positions , directions , weights
351358
352359
@@ -366,8 +373,7 @@ def load(fname, center=[0, 0, 0], n0=None):
366373 positions , directions = _rotate_array (positions , directions ,
367374 [1 , 0 , 0 ], n0 )
368375 # shift array to desired position
369- positions += np .asarray (center )
370-
376+ positions += center
371377 return positions , directions , weights
372378
373379
@@ -384,7 +390,6 @@ def weights_linear(positions):
384390 for m in range (1 , N - 1 ):
385391 weights [m ] = 0.5 * (dy [m - 1 ] + dy [m ])
386392 weights [- 1 ] = dy [- 1 ]
387-
388393 return np .abs (weights )
389394
390395
@@ -397,7 +402,7 @@ def weights_closed(positions):
397402 contour.
398403
399404 """
400- positions = np . asarray (positions )
405+ positions = util . asarray_of_rows (positions )
401406 if len (positions ) == 0 :
402407 weights = []
403408 elif len (positions ) == 1 :
@@ -406,7 +411,7 @@ def weights_closed(positions):
406411 successors = np .roll (positions , - 1 , axis = 0 )
407412 d = [np .linalg .norm (b - a ) for a , b in zip (positions , successors )]
408413 weights = [0.5 * (a + b ) for a , b in zip (d , d [- 1 :] + d )]
409- return weights
414+ return np . array ( weights )
410415
411416
412417def _rotate_array (x0 , n0 , n1 , n2 ):
0 commit comments