From 4f83604344d792ea436750f713d47926456dbb2b Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Fri, 15 Mar 2019 11:30:58 +0100 Subject: [PATCH] Split "plot" module into "plot2d" and "plot3d" See #26. --- doc/examples/animations_pulsating_sphere.py | 6 +-- doc/examples/horizontal_plane_arrays.py | 8 ++-- doc/examples/mirror-image-source-model.ipynb | 6 +-- doc/examples/modal-room-acoustics.ipynb | 2 +- doc/examples/plot_particle_density.py | 2 +- doc/examples/sound_field_synthesis.py | 6 +-- doc/examples/soundfigures.py | 8 ++-- doc/examples/time_domain.py | 18 +++---- doc/examples/time_domain_nfchoa.py | 12 ++--- sfs/__init__.py | 9 +++- sfs/array.py | 26 +++++------ sfs/fd/nfchoa.py | 4 +- sfs/fd/sdm.py | 4 +- sfs/fd/source.py | 34 +++++++------- sfs/fd/wfs.py | 4 +- sfs/{plot.py => plot2d.py} | 49 +++++++------------- sfs/plot3d.py | 15 ++++++ sfs/td/nfchoa.py | 4 +- sfs/td/source.py | 4 +- sfs/td/wfs.py | 4 +- 20 files changed, 116 insertions(+), 109 deletions(-) rename sfs/{plot.py => plot2d.py} (90%) create mode 100644 sfs/plot3d.py diff --git a/doc/examples/animations_pulsating_sphere.py b/doc/examples/animations_pulsating_sphere.py index 2257c48..9fb741d 100644 --- a/doc/examples/animations_pulsating_sphere.py +++ b/doc/examples/animations_pulsating_sphere.py @@ -15,7 +15,7 @@ def particle_displacement(omega, center, radius, amplitude, grid, frames, fig, ax = plt.subplots(figsize=figsize) ax.axis([grid[0].min(), grid[0].max(), grid[1].min(), grid[1].max()]) - scat = sfs.plot.particles(grid + displacement, **kwargs) + scat = sfs.plot2d.particles(grid + displacement, **kwargs) def update_frame_displacement(i): position = (grid + displacement * phasor**i).apply(np.real) @@ -38,7 +38,7 @@ def particle_velocity(omega, center, radius, amplitude, grid, frames, fig, ax = plt.subplots(figsize=figsize) ax.axis([grid[0].min(), grid[0].max(), grid[1].min(), grid[1].max()]) - quiv = sfs.plot.vectors( + quiv = sfs.plot2d.vectors( velocity, grid, clim=[-omega * amplitude, omega * amplitude], **kwargs) @@ -59,7 +59,7 @@ def sound_pressure(omega, center, radius, amplitude, grid, frames, phasor = np.exp(1j * 2 * np.pi / frames) fig, ax = plt.subplots(figsize=figsize) - im = sfs.plot.soundfield(np.real(pressure), grid, **kwargs) + im = sfs.plot2d.amplitude(np.real(pressure), grid, **kwargs) ax.axis([grid[0].min(), grid[0].max(), grid[1].min(), grid[1].max()]) def update_frame_pressure(i): diff --git a/doc/examples/horizontal_plane_arrays.py b/doc/examples/horizontal_plane_arrays.py index 3cb677a..8fa9580 100644 --- a/doc/examples/horizontal_plane_arrays.py +++ b/doc/examples/horizontal_plane_arrays.py @@ -34,10 +34,10 @@ def compute_and_plot_soundfield(title): plt.figure(figsize=(15, 15)) plt.cla() - sfs.plot.soundfield(p, grid, xnorm) - sfs.plot.loudspeaker_2d(array.x, array.n, twin) - sfs.plot.virtualsource_2d(xs) - sfs.plot.virtualsource_2d([0, 0], npw, type='plane') + sfs.plot2d.amplitude(p, grid, xnorm) + sfs.plot2d.loudspeakers(array.x, array.n, twin) + sfs.plot2d.virtualsource(xs) + sfs.plot2d.virtualsource([0, 0], npw, type='plane') plt.title(title) plt.grid() plt.savefig(title + '.png') diff --git a/doc/examples/mirror-image-source-model.ipynb b/doc/examples/mirror-image-source-model.ipynb index 7b9afa0..b99c133 100644 --- a/doc/examples/mirror-image-source-model.ipynb +++ b/doc/examples/mirror-image-source-model.ipynb @@ -103,7 +103,7 @@ "metadata": {}, "outputs": [], "source": [ - "sfs.plot.soundfield(P, grid, xnorm=[L[0]/2, L[1]/2, L[2]/2]);" + "sfs.plot2d.amplitude(P, grid, xnorm=[L[0]/2, L[1]/2, L[2]/2]);" ] }, { @@ -140,8 +140,8 @@ "metadata": {}, "outputs": [], "source": [ - "sfs.plot.level(p, grid)\n", - "sfs.plot.virtualsource_2d(x0)" + "sfs.plot2d.level(p, grid)\n", + "sfs.plot2d.virtualsource(x0)" ] } ], diff --git a/doc/examples/modal-room-acoustics.ipynb b/doc/examples/modal-room-acoustics.ipynb index ef7adf9..26164bd 100644 --- a/doc/examples/modal-room-acoustics.ipynb +++ b/doc/examples/modal-room-acoustics.ipynb @@ -114,7 +114,7 @@ "metadata": {}, "outputs": [], "source": [ - "sfs.plot.soundfield(p, grid);" + "sfs.plot2d.amplitude(p, grid);" ] }, { diff --git a/doc/examples/plot_particle_density.py b/doc/examples/plot_particle_density.py index 6f93f74..9de8845 100644 --- a/doc/examples/plot_particle_density.py +++ b/doc/examples/plot_particle_density.py @@ -23,7 +23,7 @@ def plot_particle_displacement(title): # plot displacement plt.figure(figsize=(15, 15)) plt.cla() - sfs.plot.particles(X, facecolor='black', s=3, trim=[-3, 3, -3, 3]) + sfs.plot2d.particles(X, facecolor='black', s=3, trim=[-3, 3, -3, 3]) plt.axis('off') plt.title(title) plt.grid() diff --git a/doc/examples/sound_field_synthesis.py b/doc/examples/sound_field_synthesis.py index 55d2cf8..8c5f723 100644 --- a/doc/examples/sound_field_synthesis.py +++ b/doc/examples/sound_field_synthesis.py @@ -73,11 +73,11 @@ # === plot synthesized sound field === plt.figure(figsize=(10, 10)) -sfs.plot.soundfield(p, grid, [0, 0, 0]) -sfs.plot.loudspeaker_2d(array.x, array.n, twin) +sfs.plot2d.amplitude(p, grid, [0, 0, 0]) +sfs.plot2d.loudspeakers(array.x, array.n, twin) plt.grid() plt.savefig('soundfield.png') -#sfs.plot.loudspeaker_3d(array.x, array.n, twin) +#sfs.plot3d.secondary_sources(array.x, array.n, twin) #plt.savefig('loudspeakers.png') diff --git a/doc/examples/soundfigures.py b/doc/examples/soundfigures.py index 7260db6..6bb763b 100644 --- a/doc/examples/soundfigures.py +++ b/doc/examples/soundfigures.py @@ -37,14 +37,14 @@ # plot and save synthesized sound field plt.figure(figsize=(10, 10)) -sfs.plot.soundfield(p, grid, xnorm=[0, -2.2, 0], cmap='BrBG', colorbar=False, - vmin=-1, vmax=1) +sfs.plot2d.amplitude(p, grid, xnorm=[0, -2.2, 0], cmap='BrBG', colorbar=False, + vmin=-1, vmax=1) plt.title('Synthesized Sound Field') plt.savefig('soundfigure.png') # plot and save level of synthesized sound field plt.figure(figsize=(12.5, 12.5)) -im = sfs.plot.level(p, grid, xnorm=[0, -2.2, 0], vmin=-50, vmax=0, - colorbar_kwargs=dict(label='dB')) +im = sfs.plot2d.level(p, grid, xnorm=[0, -2.2, 0], vmin=-50, vmax=0, + colorbar_kwargs=dict(label='dB')) plt.title('Level of Synthesized Sound Field') plt.savefig('soundfigure_level.png') diff --git a/doc/examples/time_domain.py b/doc/examples/time_domain.py index 79d965b..858e2a7 100644 --- a/doc/examples/time_domain.py +++ b/doc/examples/time_domain.py @@ -36,10 +36,10 @@ p = p * 100 # scale absolute amplitude plt.figure(figsize=(10, 10)) -sfs.plot.level(p, grid, cmap=my_cmap) -sfs.plot.loudspeaker_2d(array.x, array.n, twin) +sfs.plot2d.level(p, grid, cmap=my_cmap) +sfs.plot2d.loudspeakers(array.x, array.n, twin) plt.grid() -sfs.plot.virtualsource_2d(xs) +sfs.plot2d.virtualsource(xs) plt.title('impulse_ps_wfs_25d') plt.savefig('impulse_ps_wfs_25d.png') @@ -59,10 +59,10 @@ secondary_source, observation_time=t, grid=grid) plt.figure(figsize=(10, 10)) -sfs.plot.level(p, grid, cmap=my_cmap) -sfs.plot.loudspeaker_2d(array.x, array.n, twin) +sfs.plot2d.level(p, grid, cmap=my_cmap) +sfs.plot2d.loudspeakers(array.x, array.n, twin) plt.grid() -sfs.plot.virtualsource_2d([0, 0], npw, type='plane') +sfs.plot2d.virtualsource([0, 0], npw, type='plane') plt.title('impulse_pw_wfs_25d') plt.savefig('impulse_pw_wfs_25d.png') @@ -82,9 +82,9 @@ p = p * 100 # scale absolute amplitude plt.figure(figsize=(10, 10)) -sfs.plot.level(p, grid, cmap=my_cmap) -sfs.plot.loudspeaker_2d(array.x, array.n, twin) +sfs.plot2d.level(p, grid, cmap=my_cmap) +sfs.plot2d.loudspeakers(array.x, array.n, twin) plt.grid() -sfs.plot.virtualsource_2d(xs) +sfs.plot2d.virtualsource(xs) plt.title('impulse_fs_wfs_25d') plt.savefig('impulse_fs_wfs_25d.png') diff --git a/doc/examples/time_domain_nfchoa.py b/doc/examples/time_domain_nfchoa.py index c884832..929c1ef 100644 --- a/doc/examples/time_domain_nfchoa.py +++ b/doc/examples/time_domain_nfchoa.py @@ -27,9 +27,9 @@ observation_time=t, grid=grid) plt.figure() -sfs.plot.level(p, grid) -sfs.plot.loudspeaker_2d(array.x, array.n) -sfs.plot.virtualsource_2d([0, 0], ns=npw, type='plane') +sfs.plot2d.level(p, grid) +sfs.plot2d.loudspeakers(array.x, array.n) +sfs.plot2d.virtualsource([0, 0], ns=npw, type='plane') plt.savefig('impulse_pw_nfchoa_25d.png') # Point source @@ -44,7 +44,7 @@ observation_time=t, grid=grid) plt.figure() -sfs.plot.level(p, grid) -sfs.plot.loudspeaker_2d(array.x, array.n) -sfs.plot.virtualsource_2d(xs, type='point') +sfs.plot2d.level(p, grid) +sfs.plot2d.loudspeakers(array.x, array.n) +sfs.plot2d.virtualsource(xs, type='point') plt.savefig('impulse_ps_nfchoa_25d.png') diff --git a/sfs/__init__.py b/sfs/__init__.py index 5ea1dcf..a1c356f 100644 --- a/sfs/__init__.py +++ b/sfs/__init__.py @@ -11,7 +11,8 @@ td array tapering - plot + plot2d + plot3d util """ @@ -59,7 +60,11 @@ def reset(self): from . import array from . import util try: - from . import plot + from . import plot2d +except ImportError: + pass +try: + from . import plot3d except ImportError: pass diff --git a/sfs/array.py b/sfs/array.py index d48b584..f3ba7d6 100644 --- a/sfs/array.py +++ b/sfs/array.py @@ -110,7 +110,7 @@ def linear(N, spacing, center=[0, 0, 0], orientation=[1, 0, 0]): :context: close-figs x0, n0, a0 = sfs.array.linear(16, 0.2, orientation=[0, -1, 0]) - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -141,7 +141,7 @@ def linear_diff(distances, center=[0, 0, 0], orientation=[1, 0, 0]): x0, n0, a0 = sfs.array.linear_diff(4 * [0.3] + 6 * [0.15] + 4 * [0.3], orientation=[0, -1, 0]) - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -182,7 +182,7 @@ def linear_random(N, min_spacing, max_spacing, center=[0, 0, 0], N=12, min_spacing=0.15, max_spacing=0.4, orientation=[0, -1, 0]) - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -216,7 +216,7 @@ def circular(N, R, center=[0, 0, 0]): :context: close-figs x0, n0, a0 = sfs.array.circular(16, 1) - sfs.plot.loudspeaker_2d(x0, n0, a0, size=0.2, show_numbers=True) + sfs.plot2d.loudspeakers(x0, n0, a0, size=0.2, show_numbers=True) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -262,7 +262,7 @@ def rectangular(N, spacing, center=[0, 0, 0], orientation=[1, 0, 0]): :context: close-figs x0, n0, a0 = sfs.array.rectangular((4, 8), 0.2) - sfs.plot.loudspeaker_2d(x0, n0, a0, show_numbers=True) + sfs.plot2d.loudspeakers(x0, n0, a0, show_numbers=True) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -309,7 +309,7 @@ def rounded_edge(Nxy, Nr, dx, center=[0, 0, 0], orientation=[1, 0, 0]): :context: close-figs x0, n0, a0 = sfs.array.rounded_edge(8, 5, 0.2) - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -380,7 +380,7 @@ def edge(Nxy, dx, center=[0, 0, 0], orientation=[1, 0, 0]): :context: close-figs x0, n0, a0 = sfs.array.edge(8, 0.2) - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -441,7 +441,7 @@ def planar(N, spacing, center=[0, 0, 0], orientation=[1, 0, 0]): x0, n0, a0 = sfs.array.planar( (4,3), 0.5, orientation=[0, 1, 0]) # 4 sources along x, 3 sources along z - sfs.plot.loudspeaker_2d(x0, n0, a0) # plot the last ssd in 2D + sfs.plot2d.loudspeakers(x0, n0, a0) # plot the last ssd in 2D plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -488,7 +488,7 @@ def cube(N, spacing, center=[0, 0, 0], orientation=[1, 0, 0]): x0, n0, a0 = sfs.array.cube( N=2, spacing=0.5, center=[0, 0, 0], orientation=[1, 0, 0]) - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -547,7 +547,7 @@ def sphere_load(file, radius, center=[0, 0, 0]): '../data/arrays/example_array_6LS_3D.txt', radius=2, center=[0, 0, 0]) - sfs.plot.loudspeaker_2d(x0, n0, a0, size=0.25) + sfs.plot2d.loudspeakers(x0, n0, a0, size=0.25) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -590,7 +590,7 @@ def load(file, center=[0, 0, 0], orientation=[1, 0, 0]): :context: close-figs x0, n0, a0 = sfs.array.load('../data/arrays/example_array_4LS_2D.csv') - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -600,7 +600,7 @@ def load(file, center=[0, 0, 0], orientation=[1, 0, 0]): x0, n0, a0 = sfs.array.load( '../data/arrays/wfs_university_rostock_2018.csv') - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') @@ -695,7 +695,7 @@ def concatenate(*arrays): ssd1 = sfs.array.edge(10, 0.2) ssd2 = sfs.array.edge(20, 0.1, center=[2, 2, 0], orientation=[-1, 0, 0]) x0, n0, a0 = sfs.array.concatenate(ssd1, ssd2) - sfs.plot.loudspeaker_2d(x0, n0, a0) + sfs.plot2d.loudspeakers(x0, n0, a0) plt.axis('equal') plt.xlabel('x / m') plt.ylabel('y / m') diff --git a/sfs/fd/nfchoa.py b/sfs/fd/nfchoa.py index 2a8312b..55322a8 100644 --- a/sfs/fd/nfchoa.py +++ b/sfs/fd/nfchoa.py @@ -24,8 +24,8 @@ def plot(d, selection, secondary_source): p = sfs.fd.synthesize(d, selection, array, secondary_source, grid=grid) - sfs.plot.soundfield(p, grid) - sfs.plot.loudspeaker_2d(array.x, array.n, selection * array.a, size=0.15) + sfs.plot2d.amplitude(p, grid) + sfs.plot2d.loudspeakers(array.x, array.n, selection * array.a, size=0.15) """ diff --git a/sfs/fd/sdm.py b/sfs/fd/sdm.py index b3af25f..a14d7ed 100644 --- a/sfs/fd/sdm.py +++ b/sfs/fd/sdm.py @@ -23,8 +23,8 @@ def plot(d, selection, secondary_source): p = sfs.fd.synthesize(d, selection, array, secondary_source, grid=grid) - sfs.plot.soundfield(p, grid) - sfs.plot.loudspeaker_2d(array.x, array.n, selection * array.a, size=0.15) + sfs.plot2d.amplitude(p, grid) + sfs.plot2d.loudspeakers(array.x, array.n, selection * array.a, size=0.15) """ diff --git a/sfs/fd/source.py b/sfs/fd/source.py index c7581a9..568317f 100644 --- a/sfs/fd/source.py +++ b/sfs/fd/source.py @@ -64,7 +64,7 @@ def point(omega, x0, grid, c=None): :context: close-figs p = sfs.fd.source.point(omega, x0, grid) - sfs.plot.soundfield(p, grid) + sfs.plot2d.amplitude(p, grid) plt.title("Point Source at {} m".format(x0)) Normalization ... @@ -72,8 +72,8 @@ def point(omega, x0, grid, c=None): .. plot:: :context: close-figs - sfs.plot.soundfield(p * normalization_point, grid, - colorbar_kwargs=dict(label="p / Pa")) + sfs.plot2d.amplitude(p * normalization_point, grid, + colorbar_kwargs=dict(label="p / Pa")) plt.title("Point Source at {} m (normalized)".format(x0)) """ @@ -115,8 +115,8 @@ def point_velocity(omega, x0, grid, c=None, rho0=None): :context: close-figs v = sfs.fd.source.point_velocity(omega, x0, vgrid) - sfs.plot.soundfield(p * normalization_point, grid) - sfs.plot.vectors(v * normalization_point, vgrid) + sfs.plot2d.amplitude(p * normalization_point, grid) + sfs.plot2d.vectors(v * normalization_point, vgrid) plt.title("Sound Pressure and Particle Velocity") """ @@ -206,7 +206,7 @@ def point_dipole(omega, x0, n0, grid, c=None): n0 = 0, 1, 0 p = sfs.fd.source.point_dipole(omega, x0, n0, grid) - sfs.plot.soundfield(p, grid) + sfs.plot2d.amplitude(p, grid) plt.title("Dipole Point Source at {} m".format(x0)) """ @@ -417,7 +417,7 @@ def line(omega, x0, grid, c=None): :context: close-figs p = sfs.fd.source.line(omega, x0, grid) - sfs.plot.soundfield(p, grid) + sfs.plot2d.amplitude(p, grid) plt.title("Line Source at {} m".format(x0[:2])) Normalization ... @@ -425,8 +425,8 @@ def line(omega, x0, grid, c=None): .. plot:: :context: close-figs - sfs.plot.soundfield(p * normalization_line, grid, - colorbar_kwargs=dict(label="p / Pa")) + sfs.plot2d.amplitude(p * normalization_line, grid, + colorbar_kwargs=dict(label="p / Pa")) plt.title("Line Source at {} m (normalized)".format(x0[:2])) """ @@ -455,8 +455,8 @@ def line_velocity(omega, x0, grid, c=None, rho0=None): :context: close-figs v = sfs.fd.source.line_velocity(omega, x0, vgrid) - sfs.plot.soundfield(p * normalization_line, grid) - sfs.plot.vectors(v * normalization_line, vgrid) + sfs.plot2d.amplitude(p * normalization_line, grid) + sfs.plot2d.vectors(v * normalization_line, vgrid) plt.title("Sound Pressure and Particle Velocity") """ @@ -606,7 +606,7 @@ def plane(omega, x0, n0, grid, c=None): direction = 45 # degree n0 = sfs.util.direction_vector(np.radians(direction)) p = sfs.fd.source.plane(omega, x0, n0, grid) - sfs.plot.soundfield(p, grid, colorbar_kwargs=dict(label="p / Pa")) + sfs.plot2d.amplitude(p, grid, colorbar_kwargs=dict(label="p / Pa")) plt.title("Plane wave with direction {} degree".format(direction)) """ @@ -655,8 +655,8 @@ def plane_velocity(omega, x0, n0, grid, c=None, rho0=None): :context: close-figs v = sfs.fd.source.plane_velocity(omega, x0, n0, vgrid) - sfs.plot.soundfield(p, grid) - sfs.plot.vectors(v, vgrid) + sfs.plot2d.amplitude(p, grid) + sfs.plot2d.vectors(v, vgrid) plt.title("Sound Pressure and Particle Velocity") """ @@ -745,7 +745,7 @@ def pulsating_sphere(omega, center, radius, amplitude, grid, inside=False, radius = 0.25 amplitude = 1 / (radius * omega * sfs.default.rho0 * sfs.default.c) p = sfs.fd.source.pulsating_sphere(omega, x0, radius, amplitude, grid) - sfs.plot.soundfield(p, grid) + sfs.plot2d.amplitude(p, grid) plt.title("Sound Pressure of a Pulsating Sphere") """ @@ -797,8 +797,8 @@ def pulsating_sphere_velocity(omega, center, radius, amplitude, grid, c=None): :context: close-figs v = sfs.fd.source.pulsating_sphere_velocity(omega, x0, radius, amplitude, vgrid) - sfs.plot.soundfield(p, grid) - sfs.plot.vectors(v, vgrid) + sfs.plot2d.amplitude(p, grid) + sfs.plot2d.vectors(v, vgrid) plt.title("Sound Pressure and Particle Velocity of a Pulsating Sphere") """ diff --git a/sfs/fd/wfs.py b/sfs/fd/wfs.py index 32e805d..66a5fea 100644 --- a/sfs/fd/wfs.py +++ b/sfs/fd/wfs.py @@ -27,8 +27,8 @@ def plot(d, selection, secondary_source): p = sfs.fd.synthesize(d, selection, array, secondary_source, grid=grid) - sfs.plot.soundfield(p, grid) - sfs.plot.loudspeaker_2d(array.x, array.n, selection * array.a, size=0.15) + sfs.plot2d.amplitude(p, grid) + sfs.plot2d.loudspeakers(array.x, array.n, selection * array.a, size=0.15) """ diff --git a/sfs/plot.py b/sfs/plot2d.py similarity index 90% rename from sfs/plot.py rename to sfs/plot2d.py index a8b4d59..fd76a36 100644 --- a/sfs/plot.py +++ b/sfs/plot2d.py @@ -1,11 +1,10 @@ -"""Plot sound fields etc.""" +"""2D plots of sound fields etc.""" import matplotlib.pyplot as plt from matplotlib import __version__ as matplotlib_version from matplotlib.patches import PathPatch from matplotlib.path import Path from matplotlib.collections import PatchCollection from mpl_toolkits import axes_grid1 -from mpl_toolkits.mplot3d import Axes3D import numpy as np from . import util from . import default @@ -42,7 +41,7 @@ def _register_cmap_transparent(name, color): _register_cmap_transparent('blacktransparent', 'black') -def virtualsource_2d(xs, ns=None, type='point', ax=None): +def virtualsource(xs, ns=None, type='point', ax=None): """Draw position/orientation of virtual source.""" xs = np.asarray(xs) ns = np.asarray(ns) @@ -62,7 +61,7 @@ def virtualsource_2d(xs, ns=None, type='point', ax=None): head_length=0.1, fc='k', ec='k') -def reference_2d(xref, size=0.1, ax=None): +def reference(xref, size=0.1, ax=None): """Draw reference/normalization point.""" xref = np.asarray(xref) if ax is None: @@ -72,7 +71,7 @@ def reference_2d(xref, size=0.1, ax=None): ax.plot((xref[0]-size, xref[0]+size), (xref[1]+size, xref[1]-size), 'k-') -def secondarysource_2d(x0, n0, grid=None): +def secondary_sources(x0, n0, grid=None): """Simple plot of secondary source locations.""" x0 = np.asarray(x0) n0 = np.asarray(n0) @@ -80,7 +79,7 @@ def secondarysource_2d(x0, n0, grid=None): # plot only secondary sources inside simulated area if grid is not None: - x0, n0 = _visible_secondarysources_2d(x0, n0, grid) + x0, n0 = _visible_secondarysources(x0, n0, grid) # plot symbols for x00 in x0: @@ -88,8 +87,8 @@ def secondarysource_2d(x0, n0, grid=None): ax.add_artist(ss) -def loudspeaker_2d(x0, n0, a0=0.5, size=0.08, show_numbers=False, grid=None, - ax=None): +def loudspeakers(x0, n0, a0=0.5, size=0.08, show_numbers=False, grid=None, + ax=None): """Draw loudspeaker symbols at given locations and angles. Parameters @@ -117,7 +116,7 @@ def loudspeaker_2d(x0, n0, a0=0.5, size=0.08, show_numbers=False, grid=None, # plot only secondary sources inside simulated area if grid is not None: - x0, n0 = _visible_secondarysources_2d(x0, n0, grid) + x0, n0 = _visible_secondarysources(x0, n0, grid) # normalized coordinates of loudspeaker symbol (see IEC 60617-9) codes, coordinates = zip(*( @@ -155,7 +154,7 @@ def loudspeaker_2d(x0, n0, a0=0.5, size=0.08, show_numbers=False, grid=None, verticalalignment='center', clip_on=True) -def _visible_secondarysources_2d(x0, n0, grid): +def _visible_secondarysources(x0, n0, grid): """Determine secondary sources which lie within *grid*.""" x, y = util.as_xyz_components(grid[:2]) idx = np.where((x0[:, 0] > x.min()) & (x0[:, 0] < x.max()) & @@ -165,22 +164,10 @@ def _visible_secondarysources_2d(x0, n0, grid): return x0[idx, :], n0[idx, :] -def loudspeaker_3d(x0, n0, a0=None, w=0.08, h=0.08): - """Plot positions and normals of a 3D secondary source distribution.""" - fig = plt.figure(figsize=(15, 15)) - ax = fig.add_subplot(111, projection='3d') - ax.quiver(x0[:, 0], x0[:, 1], x0[:, 2], n0[:, 0], - n0[:, 1], n0[:, 2], length=0.1) - plt.xlabel('x (m)') - plt.ylabel('y (m)') - plt.title('Secondary Sources') - fig.show() - - -def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', vmin=-2.0, vmax=2.0, - xlabel=None, ylabel=None, colorbar=True, colorbar_kwargs={}, - ax=None, **kwargs): - """Two-dimensional plot of sound field. +def amplitude(p, grid, xnorm=None, cmap='coolwarm_clip', vmin=-2.0, vmax=2.0, + xlabel=None, ylabel=None, colorbar=True, colorbar_kwargs={}, + ax=None, **kwargs): + """Two-dimensional plot of sound field (real part). Parameters ---------- @@ -240,7 +227,7 @@ def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', vmin=-2.0, vmax=2.0, See Also -------- - sfs.plot.level + sfs.plot2d.level """ p = np.asarray(p) @@ -311,7 +298,7 @@ def level(p, grid, xnorm=None, power=False, cmap=None, vmax=3, vmin=-50, **kwargs): """Two-dimensional plot of level (dB) of sound field. - Takes the same parameters as `sfs.plot.soundfield()`. + Takes the same parameters as `sfs.plot2d.amplitude()`. Other Parameters ---------------- @@ -323,8 +310,8 @@ def level(p, grid, xnorm=None, power=False, cmap=None, vmax=3, vmin=-50, if xnorm is not None: p = util.normalize(p, grid, xnorm) L = util.db(p, power=power) - return soundfield(L, grid=grid, xnorm=None, cmap=cmap, - vmax=vmax, vmin=vmin, **kwargs) + return amplitude(L, grid=grid, xnorm=None, cmap=cmap, + vmax=vmax, vmin=vmin, **kwargs) def particles(x, trim=None, ax=None, xlabel='x (m)', ylabel='y (m)', @@ -403,7 +390,7 @@ def add_colorbar(im, aspect=20, pad=0.5, **kwargs): Parameters ---------- im : ScalarMappable - The output of `sfs.plot.soundfield()`, `sfs.plot.level()` or any + The output of `sfs.plot2d.amplitude()`, `sfs.plot2d.level()` or any other `matplotlib.cm.ScalarMappable`. aspect : float, optional Aspect ratio of the colorbar. Strictly speaking, since the diff --git a/sfs/plot3d.py b/sfs/plot3d.py new file mode 100644 index 0000000..9cf4cb0 --- /dev/null +++ b/sfs/plot3d.py @@ -0,0 +1,15 @@ +"""3D plots of sound fields etc.""" +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + + +def secondary_sources(x0, n0, a0=None, w=0.08, h=0.08): + """Plot positions and normals of a 3D secondary source distribution.""" + fig = plt.figure(figsize=(15, 15)) + ax = fig.add_subplot(111, projection='3d') + q = ax.quiver(x0[:, 0], x0[:, 1], x0[:, 2], n0[:, 0], + n0[:, 1], n0[:, 2], length=0.1) + plt.xlabel('x (m)') + plt.ylabel('y (m)') + plt.title('Secondary Sources') + return q diff --git a/sfs/td/nfchoa.py b/sfs/td/nfchoa.py index 74981b0..2e65450 100644 --- a/sfs/td/nfchoa.py +++ b/sfs/td/nfchoa.py @@ -32,8 +32,8 @@ def plot(d, selection, secondary_source, t=0): p = sfs.td.synthesize(d, selection, array, secondary_source, grid=grid, observation_time=t) - sfs.plot.level(p, grid) - sfs.plot.loudspeaker_2d(array.x, array.n, selection * array.a, size=0.15) + sfs.plot2d.level(p, grid) + sfs.plot2d.loudspeakers(array.x, array.n, selection * array.a, size=0.15) """ import numpy as np diff --git a/sfs/td/source.py b/sfs/td/source.py index 7d915c1..bf9e9aa 100644 --- a/sfs/td/source.py +++ b/sfs/td/source.py @@ -69,7 +69,7 @@ def point(xs, signal, observation_time, grid, c=None): :context: close-figs p = sfs.td.source.point(xs, signal, ts, grid) - sfs.plot.level(p, grid) + sfs.plot2d.level(p, grid) """ xs = util.asarray_1d(xs) @@ -131,7 +131,7 @@ def point_image_sources(x0, signal, observation_time, grid, L, max_order, grid = sfs.util.xyz_grid([0, room[0]], [0, room[1]], 0, spacing=0.01) p = sfs.td.source.point_image_sources( xs, signal, 1.5 * ts, grid, room, order, coeffs) - sfs.plot.level(p, grid) + sfs.plot2d.level(p, grid) """ if coeffs is None: diff --git a/sfs/td/wfs.py b/sfs/td/wfs.py index 276361f..03fa985 100644 --- a/sfs/td/wfs.py +++ b/sfs/td/wfs.py @@ -38,8 +38,8 @@ def plot(d, selection, secondary_source, t=0): p = sfs.td.synthesize(d, selection, array, secondary_source, grid=grid, observation_time=t) - sfs.plot.level(p, grid) - sfs.plot.loudspeaker_2d(array.x, array.n, selection * array.a, size=0.15) + sfs.plot2d.level(p, grid) + sfs.plot2d.loudspeakers(array.x, array.n, selection * array.a, size=0.15) """ import numpy as np