diff --git a/examples/time_domain.py b/examples/time_domain.py index d790372..d9a4ab9 100644 --- a/examples/time_domain.py +++ b/examples/time_domain.py @@ -31,7 +31,7 @@ a = sfs.mono.drivingfunction.source_selection_point(n0, x0, xs) twin = sfs.tapering.tukey(a, .3) p = sfs.time.soundfield.p_array(x0, d, twin * a0, t, grid) -p = p * 100 # scale absolute amplitude +p = p * 30 # scale absolute amplitude plt.figure(figsize=(10, 10)) sfs.plot.level(p, grid, cmap=my_cmap) diff --git a/sfs/plot.py b/sfs/plot.py index 05a9af6..2f265fe 100644 --- a/sfs/plot.py +++ b/sfs/plot.py @@ -3,7 +3,8 @@ import matplotlib.pyplot as plt from matplotlib.patches import PathPatch from matplotlib.path import Path -from matplotlib.collections import PatchCollection +from matplotlib.collections import PatchCollection, LineCollection +from matplotlib.colors import ListedColormap from mpl_toolkits import axes_grid1 from mpl_toolkits.mplot3d import Axes3D import numpy as np @@ -86,6 +87,43 @@ def secondarysource_2d(x0, n0, grid=None): ax.add_artist(ss) +def secondarysourcecontour_2d(x0, a0=True, ax=None): + """Draw contour of secondary source distribution as polygon. + + Parameters + ---------- + x0 : (N, 3) array_like + Secondary source positions. + a0 : bool or (N,) array_like, optional + Active secondary sources. + ax : Axes object, optional + The loudspeakers are plotted into this `matplotlib.axes.Axes` + object or -- if not specified -- into the current axes. + """ + x0 = util.asarray_of_rows(x0) + a0 = util.asarray_1d(a0) + + # colormap with black and gray to indicate active parts + cmap = ListedColormap(np.stack((.5*np.ones(3), np.zeros(3)))) + + # setup line collection with secondary source contour + x0 = np.append(x0, x0[0:1, :], axis=0) + points = np.array([x0[:, 0], x0[:, 1]]).T.reshape(-1, 1, 2) + segments = np.concatenate([points[:-1], points[1:]], axis=1) + lc = LineCollection(segments, cmap=cmap) + lc.set_linewidth(2) + + # set color of line to indicate active parts + a0 = np.append(a0, a0[0]) + active = np.logical_and(a0[0:-1], a0[1:]) + lc.set_array(active) + + # add collection of lines to current axis + if ax is None: + ax = plt.gca() + plt.gca().add_collection(lc) + + def loudspeaker_2d(x0, n0, a0=0.5, size=0.08, show_numbers=False, grid=None, ax=None): """Draw loudspeaker symbols at given locations and angles.