Skip to content

Commit c6d5936

Browse files
committed
add 'db' in 'util.py'
1 parent 2e96971 commit c6d5936

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

examples/modal_beamforming_open_circular_array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
pol_pwd = np.linspace(0, 2*np.pi, 91, endpoint=False) # angles for plane wave decomposition
1313
k = np.linspace(0.1, 20, 100) # wavenumber vector
1414
r = 1 # radius of array
15-
M = 181 # number of microphones
15+
M = 181 # number of microphones
1616

1717
# get uniform grid (microphone positions) of number M
1818
pol, weights = micarray.modal.angular.grid_equal_polar_angle(M)
@@ -26,23 +26,23 @@
2626
D = micarray.modal.radial.circ_diagonal_mode_mat(Dn)
2727

2828
# compute microphone signals for an incident broad-band plane wave
29-
p = np.exp(1j * k[:, np.newaxis]*r * np.cos(pol - pw_angle))
29+
p = np.exp(-1j * k[:, np.newaxis]*r * np.cos(pol - pw_angle))
3030
# compute plane wave decomposition
3131
A_pwd = np.matmul(np.matmul(np.conj(Psi_q.T), D), Psi_p)
3232
q_pwd = np.squeeze(np.matmul(A_pwd, np.expand_dims(p, 2)))
3333
q_pwd_t = np.fft.fftshift(np.fft.irfft(q_pwd, axis=0), axes=0)
3434

3535
# visualize plane wave decomposition (aka beampattern)
3636
plt.figure()
37-
plt.pcolormesh(k, pol_pwd/np.pi, 20*np.log10(np.abs(q_pwd.T)), vmin=-40)
37+
plt.pcolormesh(k, pol_pwd/np.pi, micarray.util.db(q_pwd.T), vmin=-40)
3838
plt.colorbar()
3939
plt.xlabel(r'$kr$')
4040
plt.ylabel(r'$\phi / \pi$')
4141
plt.title('Plane wave docomposition by modal beamformer (frequency domain)')
4242
plt.savefig('modal_open_beamformer_pwd_fd.png')
4343

4444
plt.figure()
45-
plt.pcolormesh(range(2*len(k)-2), pol_pwd/np.pi, 20*np.log10(np.abs(q_pwd_t.T)), vmin=-40)
45+
plt.pcolormesh(range(2*len(k)-2), pol_pwd/np.pi, micarray.util.db(q_pwd_t.T), vmin=-40)
4646
plt.colorbar()
4747
plt.ylabel(r'$\phi / \pi$')
4848
plt.title('Plane wave docomposition by modal beamformer (time domain)')

micarray/util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,19 @@ def matdiagmul(A, b):
9191
for k in range(K):
9292
C[k, :, :] = A * b[k, :]
9393
return C
94+
95+
96+
def db(x, power=False):
97+
"""Convert *x* to decibel.
98+
99+
Parameters
100+
----------
101+
x : array_like
102+
Input data. Values of 0 lead to negative infinity.
103+
power : bool, optional
104+
If ``power=False`` (the default), *x* is squared before
105+
conversion.
106+
107+
"""
108+
with np.errstate(divide='ignore'):
109+
return 10 if power else 20 * np.log10(np.abs(x))

0 commit comments

Comments
 (0)