Skip to content

Commit 6141171

Browse files
committed
5章後半を追加
1 parent 1ace6f2 commit 6141171

File tree

5 files changed

+389
-0
lines changed

5 files changed

+389
-0
lines changed

khashimoto/chapter05/q06.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 簡単な多チャネル観測シミュレーション
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
7+
fs = 16000
8+
sec = 1
9+
A = 1
10+
f = 440
11+
12+
t = np.arange(fs * sec) / fs
13+
s = A * np.sin(2 * np.pi * f * t) # 正弦波
14+
15+
snr = 10
16+
noise = np.random.randn(fs * sec)
17+
noise /= np.sqrt(np.sum(noise**2))
18+
noise *= np.sqrt(np.sum(s**2))
19+
a = 10 ** (-snr / 20)
20+
noise = a * noise # ホワイトノイズ
21+
22+
x1 = np.zeros(fs * sec)
23+
x2 = np.zeros(fs * sec)
24+
x3 = np.zeros(fs * sec)
25+
for n in range(fs * sec):
26+
x1[n] = s[n] + noise[n]
27+
x2[n] = s[n - 10] + noise[n]
28+
x3[n] = s[n - 20] + noise[n]
29+
30+
31+
plt.plot(t, x1)
32+
plt.plot(t, x2)
33+
plt.plot(t, x3)
34+
plt.xlim(0, 0.01)
35+
plt.show()

khashimoto/chapter05/q07.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 遅延和ビームフォーマ
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
from scipy import signal
6+
7+
8+
# 6.
9+
fs = 16000
10+
sec = 1
11+
A = 1
12+
f = 440
13+
14+
ts = np.arange(fs * sec) / fs
15+
s = A * np.sin(2 * np.pi * f * ts) # 正弦波
16+
17+
snr = 10
18+
noise = np.random.randn(fs * sec)
19+
noise /= np.sqrt(np.sum(noise**2))
20+
noise *= np.sqrt(np.sum(s**2))
21+
a = 10 ** (-snr / 20)
22+
noise = a * noise # ホワイトノイズ
23+
24+
x1 = np.zeros(fs * sec)
25+
x2 = np.zeros(fs * sec)
26+
x3 = np.zeros(fs * sec)
27+
for n in range(fs * sec):
28+
x1[n] = s[n] + noise[n]
29+
x2[n] = s[n - 10] + noise[n]
30+
x3[n] = s[n - 20] + noise[n]
31+
32+
33+
# 7.
34+
L = 1024
35+
S = 512
36+
window = np.hanning(L)
37+
# a.
38+
f_, t_, X1 = signal.stft(x1, fs, window, L, L - S)
39+
f_, t_, X2 = signal.stft(x2, fs, window, L, L - S)
40+
f_, t_, X3 = signal.stft(x3, fs, window, L, L - S)
41+
42+
43+
F, T = X1.shape
44+
f = np.arange(F) * (fs / 2) / (F - 1)
45+
tau1 = 0
46+
tau2 = 10 / fs
47+
tau3 = 20 / fs
48+
49+
Y = np.zeros((F, T), dtype=complex)
50+
for i in range(F):
51+
# c.
52+
w_f = (
53+
np.array(
54+
[
55+
np.exp(-1j * 2 * np.pi * f[i] * tau1),
56+
np.exp(-1j * 2 * np.pi * f[i] * tau2),
57+
np.exp(-1j * 2 * np.pi * f[i] * tau3),
58+
]
59+
)
60+
/ 3
61+
)
62+
for j in range(T):
63+
# b.
64+
x_ft = np.array([X1[i, j], X2[i, j], X3[i, j]]).T
65+
# d.
66+
Y[i, j] = np.conj(w_f.T) @ x_ft
67+
68+
# e.
69+
t_Y, Y_istft = signal.istft(Y, fs, window, L, L - S)
70+
71+
plt.plot(t_Y, Y_istft)
72+
plt.xlim(0, 0.01)
73+
plt.show()

khashimoto/chapter05/q08.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# ビームパターン
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
7+
# 3.
8+
def array_manifold_vector3(p, theta, f):
9+
c = 334
10+
M = p.shape[0]
11+
a = np.zeros(M, dtype=complex)
12+
u = np.array([np.sin(theta), np.cos(theta), 0]).T
13+
for m in range(1, M + 1):
14+
p_m = p[m - 1]
15+
a[m - 1] = np.exp(1j * 2 * np.pi * f / c * (u.T @ p_m))
16+
return a
17+
18+
19+
def beampattern(w_f, p_m, fs):
20+
F = w_f.shape[0]
21+
f = np.arange(F) * (fs / 2) / (F - 1)
22+
psi = np.zeros((F, 361), dtype=complex)
23+
24+
for i in range(F):
25+
for theta in range(361):
26+
theta_rad = theta * np.pi / 180
27+
# a.
28+
a_f = array_manifold_vector3(p_m, theta_rad, f[i])
29+
# b.
30+
psi[i, theta] = np.conj(w_f[i, :].T) @ a_f
31+
32+
Theta = np.arange(361)
33+
# c.
34+
plt.pcolormesh(Theta, f, 20 * np.log10(np.abs(psi)))
35+
plt.xlabel("Angle[°]")
36+
plt.ylabel("Frequency[Hz]")
37+
plt.show()
38+
39+
40+
c = 334
41+
fs = 16000
42+
L = 1024
43+
F = L // 2 + 1
44+
f = np.arange(F) * (fs / 2) / (F - 1)
45+
46+
# 1. の直線状アレイにおける遅延和ビームフォーマのビームパターンをプロット
47+
d = 0.05
48+
M = 3
49+
theta = np.pi / 4
50+
tau1 = 0
51+
tau2 = d * np.sin(theta) / c
52+
tau3 = 2 * d * np.sin(theta) / c
53+
54+
w_f = np.zeros((F, M), dtype=complex)
55+
for i in range(F):
56+
w_f[i] = np.array(
57+
[
58+
np.exp(-1j * 2 * np.pi * f[i] * tau1),
59+
np.exp(-1j * 2 * np.pi * f[i] * tau2),
60+
np.exp(-1j * 2 * np.pi * f[i] * tau3),
61+
]
62+
)
63+
p_m = np.array([[-d, 0, 0], [0, 0, 0], [d, 0, 0]])
64+
beampattern(w_f, p_m, fs)

khashimoto/chapter05/q09.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 空間サンプリング定理
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
7+
# 3.
8+
def array_manifold_vector3(p, theta, f):
9+
c = 334
10+
M = p.shape[0]
11+
a = np.zeros(M, dtype=complex)
12+
u = np.array([np.sin(theta), np.cos(theta), 0]).T
13+
for m in range(1, M + 1):
14+
p_m = p[m - 1]
15+
a[m - 1] = np.exp(1j * 2 * np.pi * f / c * (u.T @ p_m))
16+
return a
17+
18+
19+
# 8.
20+
def beampattern(w_f, p_m, fs):
21+
F = w_f.shape[0]
22+
f = np.arange(F) * (fs / 2) / (F - 1)
23+
psi = np.zeros((F, 361), dtype=complex)
24+
25+
for i in range(F):
26+
for theta in range(361):
27+
theta_rad = theta * np.pi / 180
28+
# a.
29+
a_f = array_manifold_vector3(p_m, theta_rad, f[i])
30+
# b.
31+
psi[i, theta] = np.conj(w_f[i, :].T) @ a_f
32+
33+
return psi
34+
35+
36+
c = 334
37+
fs = 16000
38+
L = 1024
39+
F = L // 2 + 1
40+
f = np.arange(F) * (fs / 2) / (F - 1)
41+
M = 3
42+
theta = np.pi / 4
43+
44+
45+
# マイク間隔2[cm]
46+
d = 0.02
47+
tau1 = 0
48+
tau2 = d * np.sin(theta) / c
49+
tau3 = 2 * d * np.sin(theta) / c
50+
51+
w_f = np.zeros((F, M), dtype=complex)
52+
for i in range(F):
53+
w_f[i] = np.array(
54+
[
55+
np.exp(-1j * 2 * np.pi * f[i] * tau1),
56+
np.exp(-1j * 2 * np.pi * f[i] * tau2),
57+
np.exp(-1j * 2 * np.pi * f[i] * tau3),
58+
]
59+
)
60+
p_m = np.array([[-d, 0, 0], [0, 0, 0], [d, 0, 0]])
61+
psi1 = beampattern(w_f, p_m, fs)
62+
63+
# マイク間隔5[cm]
64+
d = 0.05
65+
tau1 = 0
66+
tau2 = d * np.sin(theta) / c
67+
tau3 = 2 * d * np.sin(theta) / c
68+
69+
w_f = np.zeros((F, M), dtype=complex)
70+
for i in range(F):
71+
w_f[i] = np.array(
72+
[
73+
np.exp(-1j * 2 * np.pi * f[i] * tau1),
74+
np.exp(-1j * 2 * np.pi * f[i] * tau2),
75+
np.exp(-1j * 2 * np.pi * f[i] * tau3),
76+
]
77+
)
78+
p_m = np.array([[-d, 0, 0], [0, 0, 0], [d, 0, 0]])
79+
psi2 = beampattern(w_f, p_m, fs)
80+
81+
# マイク間隔10[cm]
82+
d = 0.1
83+
tau1 = 0
84+
tau2 = d * np.sin(theta) / c
85+
tau3 = 2 * d * np.sin(theta) / c
86+
87+
w_f = np.zeros((F, M), dtype=complex)
88+
for i in range(F):
89+
w_f[i] = np.array(
90+
[
91+
np.exp(-1j * 2 * np.pi * f[i] * tau1),
92+
np.exp(-1j * 2 * np.pi * f[i] * tau2),
93+
np.exp(-1j * 2 * np.pi * f[i] * tau3),
94+
]
95+
)
96+
p_m = np.array([[-d, 0, 0], [0, 0, 0], [d, 0, 0]])
97+
psi3 = beampattern(w_f, p_m, fs)
98+
99+
Theta = np.arange(361)
100+
101+
plt.subplot(1, 3, 1)
102+
plt.pcolormesh(Theta, f, 20 * np.log10(np.abs(psi1)))
103+
plt.xlabel("Angle[°]")
104+
plt.ylabel("Frequency[Hz]")
105+
106+
plt.subplot(1, 3, 2)
107+
plt.pcolormesh(Theta, f, 20 * np.log10(np.abs(psi2)))
108+
plt.xlabel("Angle[°]")
109+
plt.ylabel("Frequency[Hz]")
110+
111+
plt.subplot(1, 3, 3)
112+
plt.pcolormesh(Theta, f, 20 * np.log10(np.abs(psi3)))
113+
plt.xlabel("Angle[°]")
114+
plt.ylabel("Frequency[Hz]")
115+
116+
plt.show()

khashimoto/chapter05/q10.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 空間スペクトル
2+
3+
import numpy as np
4+
from scipy import signal
5+
import matplotlib.pyplot as plt
6+
7+
8+
# 4.
9+
def spatial_correlation_matrix(X):
10+
M, F, T = X.shape
11+
R = np.zeros((F, M, M), dtype=complex)
12+
for f in range(F):
13+
sum = np.zeros((M, M), dtype=complex)
14+
for t in range(T):
15+
# a.
16+
x_ft = np.zeros(M, dtype=complex)
17+
for m in range(M):
18+
x_ft[m] = X[m, f, t]
19+
x_ft = np.array([x_ft]).T
20+
21+
# b.
22+
sum = np.add(sum, x_ft @ np.conj(x_ft.T))
23+
R[f] = sum / T
24+
return R
25+
26+
27+
def spatialspec(z_m, M, d, f):
28+
c = 334
29+
fs = 16000
30+
L = 1024
31+
S = 512
32+
window = np.hanning(L)
33+
34+
F = L // 2 + 1
35+
T = (fs + L - S) // S + 1
36+
# a.
37+
Z_m = np.zeros((M, F, T), dtype=complex)
38+
for m in range(M):
39+
f_, t_, Z_m[m] = signal.stft(z_m[m], fs, window, L, L - S)
40+
41+
# b.
42+
R_z = spatial_correlation_matrix(Z_m)
43+
44+
# c. d.
45+
P = np.zeros(361, dtype=complex)
46+
for theta in range(361):
47+
theta_rad = theta * np.pi / 180
48+
tau1 = 0
49+
tau2 = d * np.sin(theta_rad) / c
50+
tau3 = 2 * d * np.sin(theta_rad) / c
51+
w = np.array(
52+
[
53+
np.exp(-1j * 2 * np.pi * f_[f] * tau1),
54+
np.exp(-1j * 2 * np.pi * f_[f] * tau2),
55+
np.exp(-1j * 2 * np.pi * f_[f] * tau3),
56+
]
57+
)
58+
59+
P[theta] = np.conj(w.T) @ R_z[f] @ w
60+
return P
61+
62+
63+
# 6.
64+
fs = 16000
65+
sec = 1
66+
A = 1
67+
f = 440
68+
69+
ts = np.arange(fs * sec) / fs
70+
s = A * np.sin(2 * np.pi * f * ts) # 正弦波
71+
72+
snr = 10
73+
np.random.seed(0)
74+
noise = np.random.randn(fs * sec)
75+
noise /= np.sqrt(np.sum(noise**2))
76+
noise *= np.sqrt(np.sum(s**2))
77+
a = 10 ** (-snr / 20)
78+
noise = a * noise # ホワイトノイズ
79+
80+
x1 = np.zeros(fs * sec)
81+
x2 = np.zeros(fs * sec)
82+
x3 = np.zeros(fs * sec)
83+
for n in range(fs * sec):
84+
x1[n] = s[n] + noise[n]
85+
x2[n] = s[n - 10] + noise[n]
86+
x3[n] = s[n - 20] + noise[n]
87+
88+
89+
# 10.
90+
z_m = np.array([x1, x2, x3])
91+
M = 3
92+
d = 0.05
93+
Theta = np.arange(361)
94+
95+
freq = np.arange(20, 31)
96+
for i in range(11):
97+
P = spatialspec(z_m, M, d, freq[i])
98+
plt.subplot(6, 2, i + 1)
99+
plt.plot(20 * np.log10(np.abs(P)))
100+
101+
plt.show()

0 commit comments

Comments
 (0)