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

Skip to content

Commit 1a4620b

Browse files
committed
Implementation of a modal room model for a point source
1 parent 630d6e2 commit 1a4620b

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

examples/modal_room_acoustics.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import sfs
4+
5+
x0 = [1, 3, 1.80] # source position
6+
L = [6, 6, 3] # dimensions of room
7+
N = 20 # number of modal components per dimension
8+
deltan = 0.1 # absorption factor of walls
9+
10+
11+
# compute frequency response
12+
if False is True:
13+
f = np.linspace(20, 200, 180) # frequency
14+
omega = 2 * np.pi * f # angular frequency
15+
grid = sfs.util.xyz_grid(1, 1, 1.80, spacing=1)
16+
17+
p = []
18+
for om in omega:
19+
p.append(sfs.mono.source.point_modal(om, x0, grid, L, N, deltan))
20+
21+
p = np.asarray(p)
22+
23+
plt.plot(f, 20*np.log10(np.abs(p)))
24+
plt.grid()
25+
26+
27+
# compute sound field
28+
if True is True:
29+
f = 500 # frequency
30+
omega = 2 * np.pi * f # angular frequency
31+
grid = sfs.util.xyz_grid([0, 6], [0, 6], 1.80, spacing=.1)
32+
33+
p = sfs.mono.source.point_modal(omega, x0, grid, L, N=[2, 0, 0], deltan=deltan)
34+
35+
sfs.plot.soundfield(p, grid, xnorm=[3, 3, 0], colorbar=False, vmax=1.5, vmin=-1.5)

sfs/mono/source.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,68 @@ def point(omega, x0, n0, grid, c=None):
2323
return np.squeeze(1/(4*np.pi) * np.exp(-1j * k * r) / r)
2424

2525

26+
def point_modal(omega, x0, grid, L, N=None, deltan=0, c=None):
27+
"""Point source in a rectangular room using a modal room model.
28+
29+
Parameters
30+
----------
31+
omega : float
32+
Frequency of source.
33+
x0 : triple of floats
34+
Position of source.
35+
grid : list of numpy.ndarrays
36+
The grid that is used for the sound field calculations.
37+
L : triple of floats
38+
Dimensionons of the rectangular room.
39+
N : triple of integers
40+
Combination of modal orders in the three-spatial dimensions to
41+
calculate the sound field for. If not given, the maximum modal order
42+
is determined and the sound field is computed up to this max.
43+
deltan : float
44+
Absorption coefficient of the walls.
45+
c : float
46+
Speed of sound.
47+
48+
Returns
49+
-------
50+
51+
"""
52+
k = util.wavenumber(omega, c)
53+
x0 = util.asarray_1d(x0)
54+
x, y, z = util.asarray_of_arrays(grid)
55+
56+
if N is None:
57+
# determine maximum modal order per dimension
58+
Nx = int(np.ceil(L[0]/np.pi * k))
59+
Ny = int(np.ceil(L[1]/np.pi * k))
60+
Nz = int(np.ceil(L[2]/np.pi * k))
61+
mm = range(Nx)
62+
nn = range(Ny)
63+
ll = range(Nz)
64+
else:
65+
# compute field for one order combination only
66+
mm = [N[0]]
67+
nn = [N[1]]
68+
ll = [N[2]]
69+
70+
p = 0
71+
for m in mm:
72+
for n in nn:
73+
for l in ll:
74+
kx = m*np.pi/L[0]
75+
ky = n*np.pi/L[1]
76+
kz = l*np.pi/L[2]
77+
78+
km = (kx + 1j*deltan)**2 + (ky + 1j*deltan)**2 + \
79+
(kz + 1j*deltan)**2
80+
81+
p = p + 1 / (k**2 - km) * \
82+
np.cos(kx*x) * np.cos(ky*y) * np.cos(kz*z) * \
83+
np.cos(kx*x0[0]) * np.cos(ky*x0[1]) * np.cos(kz*x0[2])
84+
85+
return np.squeeze(p)
86+
87+
2688
def line(omega, x0, n0, grid, c=None):
2789
"""Line source parallel to the z-axis.
2890

0 commit comments

Comments
 (0)