Skip to content

Commit

Permalink
Fixes a bug in grid where the cartesian coordinate array is not set p…
Browse files Browse the repository at this point in the history
…roperly when creating the object with the number of desired point as the single parameter (issue #380). (#386)
  • Loading branch information
fakufaku authored Dec 7, 2024
1 parent f6d11c9 commit e4d183a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Bugfix
directivity to ``Room.add_microphone_array``, the directivity was dropped
from the object.

- Fixes issue #380: Caused by the attribute ``cartesian`` of ``GridSphere`` not
being set properly when the grid is only initialized with a number of points.

`0.8.2`_ - 2024-11-06
---------------------

Expand Down
5 changes: 2 additions & 3 deletions pyroomacoustics/doa/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,11 @@ def __init__(
# If no list was provided, samples points on the sphere
# as uniformly as possible

self.x, self.y, self.z = fibonacci_spherical_sampling(n_points)
self.x[:], self.y[:], self.z[:] = fibonacci_spherical_sampling(n_points)

# Create convenient arrays
# to access both in cartesian and spherical coordinates
self.azimuth[:] = np.arctan2(self.y, self.x)
self.colatitude[:] = np.arctan2(np.sqrt(self.x**2 + self.y**2), self.z)
self.azimuth[:], self.colatitude[:], _ = cart2spher(self.cartesian)

self._neighbors = None
if precompute_neighbors:
Expand Down
41 changes: 39 additions & 2 deletions pyroomacoustics/doa/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from scipy.spatial import SphericalVoronoi

from pyroomacoustics.doa import fibonacci_spherical_sampling
import pyroomacoustics as pra


@pytest.mark.parametrize("n", [20, 100, 200, 500, 1000, 2000, 5000, 10000])
Expand All @@ -18,7 +18,7 @@ def test_voronoi_area(n, tol):
We observed empirically that the relative max error is
around 6% so we set that as the threshold for the test
"""
points = fibonacci_spherical_sampling(n_points=n)
points = pra.doa.fibonacci_spherical_sampling(n_points=n)
sphere_area = 4.0 * np.pi
area_one_pt = sphere_area / n

Expand All @@ -34,6 +34,43 @@ def test_voronoi_area(n, tol):
assert max_err < tol


def _check_grid_consistency(grid):

cart = pra.doa.spher2cart(grid.azimuth, grid.colatitude)
assert np.allclose(grid.cartesian, np.array([grid.x, grid.y, grid.z]))
assert np.allclose(grid.cartesian, cart)

az, co, _ = pra.doa.cart2spher(grid.cartesian)
assert np.allclose(grid.spherical, np.array([grid.azimuth, grid.colatitude]))
assert np.allclose(grid.spherical, np.array([az, co]))


@pytest.mark.parametrize("n_points", [20, 100, 200, 500, 1000])
def test_grid_sphere_from_spherical(n_points):

x, y, z = pra.doa.fibonacci_spherical_sampling(n_points)
az, co, _ = pra.doa.cart2spher(np.array([x, y, z]))

grid = pra.doa.GridSphere(spherical_points=np.array([az, co]))
_check_grid_consistency(grid)


@pytest.mark.parametrize("n_points", [20, 100, 200, 500, 1000])
def test_grid_sphere_from_cartesian(n_points):

x, y, z = pra.doa.fibonacci_spherical_sampling(n_points)

grid = pra.doa.GridSphere(cartesian_points=np.array([x, y, z]))
_check_grid_consistency(grid)


@pytest.mark.parametrize("n_points", [20, 100, 200, 500, 1000])
def test_grid_sphere_from_fibonacci(n_points):

grid = pra.doa.GridSphere(n_points=n_points)
_check_grid_consistency(grid)


if __name__ == "__main__":
test_voronoi_area(20, 0.01)
test_voronoi_area(100, 0.01)
Expand Down

0 comments on commit e4d183a

Please sign in to comment.