Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f12d82a

Browse files
committed
Fixed normalization for SPL plotting
1 parent fbea3b3 commit f12d82a

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

sfs/plot.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,7 @@ def soundfield(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip',
126126

127127
# normalize sound field wrt xnorm
128128
if xnorm is not None:
129-
xnorm = util.asarray_1d(xnorm)
130-
r = np.linalg.norm(grid - xnorm)
131-
idx = np.unravel_index(r.argmin(), r.shape)
132-
# p is normally squeezed, therefore we need only 2 dimensions:
133-
idx = idx[:p.ndim]
134-
p = p / abs(p[idx])
129+
p = util.normalize(p, grid, xnorm)
135130

136131
x, y = grid[:2] # ignore z-component
137132

@@ -154,7 +149,13 @@ def soundfield(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip',
154149
def level(p, grid, xnorm=None, colorbar=True, cmap='coolwarm_clip',
155150
ax=None, xlabel='x (m)', ylabel='y (m)', vmax=3.0, vmin=-50,
156151
**kwargs):
157-
"""Two-dimensional plot of level of sound field."""
158-
im = soundfield(20*np.log10(np.abs(p)), grid, xnorm, colorbar, cmap, ax, xlabel, ylabel, vmax, vmin, **kwargs)
152+
"""Two-dimensional plot of level (dB) of sound field."""
153+
# normalize sound field wrt xnorm
154+
if xnorm is not None:
155+
p = util.normalize(p, grid, xnorm)
159156

160-
return im
157+
xnorm = None
158+
im = soundfield(20*np.log10(np.abs(p)), grid, xnorm, colorbar, cmap, ax,
159+
xlabel, ylabel, vmax, vmin, **kwargs)
160+
161+
return im

sfs/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,13 @@ def xyz_grid(x, y, z, spacing, endpoint=True, **kwargs):
165165
args.append(strict_arange(start, stop, spacing[i],
166166
endpoint=endpoint, **kwargs))
167167
return np.meshgrid(*args, sparse=True, copy=False)
168+
169+
170+
def normalize(p, grid, xnorm):
171+
"""Normalize sound field wrt position xnorm."""
172+
xnorm = asarray_1d(xnorm)
173+
r = np.linalg.norm(grid - xnorm)
174+
idx = np.unravel_index(r.argmin(), r.shape)
175+
# p is normally squeezed, therefore we need only 2 dimensions:
176+
idx = idx[:p.ndim]
177+
return p / abs(p[idx])

0 commit comments

Comments
 (0)