Skip to content

Commit 231f233

Browse files
authored
Merge pull request #28 from 153hashimoto/khashimoto/chapter05
5章を追加 橋本
2 parents 4ca392e + 6141171 commit 231f233

File tree

10 files changed

+568
-0
lines changed

10 files changed

+568
-0
lines changed

khashimoto/chapter05/q01.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# アレイマニフォールドベクトル(直線状アレイ)
2+
3+
import numpy as np
4+
5+
6+
def array_manifold_vector1(d, M, theta, f):
7+
c = 334
8+
a = np.zeros(M, dtype=complex)
9+
u = np.array([np.sin(theta), np.cos(theta), 0]).T
10+
for m in range(1, M + 1):
11+
p_m = np.array([((m - 1) - (M - 1) / 2) * d, 0, 0]).T
12+
a[m - 1] = np.exp(1j * 2 * np.pi * f / c * np.dot(u, p_m))
13+
return a
14+
15+
16+
d = 0.05
17+
M = 3
18+
theta = np.pi / 4
19+
f = 1000
20+
print(array_manifold_vector1(d, M, theta, f))
21+
22+
# [0.7868537-0.61713958j 1. +0.j 0.7868537+0.61713958j]

khashimoto/chapter05/q02.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# アレイマニフォールドベクトル(円状アレイ)
2+
3+
import numpy as np
4+
5+
6+
def array_manifold_vector2(r, M, theta, f):
7+
c = 334
8+
a = np.zeros(M, dtype=complex)
9+
u = np.array([np.sin(theta), np.cos(theta), 0]).T
10+
for m in range(1, M + 1):
11+
p_m = np.array(
12+
[
13+
r * np.sin(2 * np.pi / M * (m - 1)),
14+
r * np.cos(2 * np.pi / M * (m - 1)),
15+
0,
16+
]
17+
).T
18+
a[m - 1] = np.exp(1j * 2 * np.pi * f / c * np.dot(u, p_m))
19+
return a
20+
21+
22+
r = 0.05
23+
M = 3
24+
theta = np.pi / 4
25+
f = 1000
26+
print(array_manifold_vector2(r, M, theta, f))
27+
28+
# [0.7868537 +0.61713958j 0.97051349+0.2410468j 0.6148926 -0.78861086j]

khashimoto/chapter05/q03.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# アレイマニフォールドベクトル(一般)
2+
3+
import numpy as np
4+
5+
6+
def array_manifold_vector3(p, theta, f):
7+
c = 334
8+
M = len(p)
9+
a = np.zeros(M, dtype=complex)
10+
u = np.array([np.sin(theta), np.cos(theta), 0]).T
11+
for m in range(1, M + 1):
12+
p_m = p[m - 1].T
13+
a[m - 1] = np.exp(1j * 2 * np.pi * f / c * np.dot(u, p_m))
14+
return a
15+
16+
17+
# 1.
18+
p = np.array([[-0.05, 0, 0], [0, 0, 0], [0.05, 0, 0]])
19+
theta = np.pi / 4
20+
f = 1000
21+
print(array_manifold_vector3(p, theta, f))
22+
23+
# [0.7868537-0.61713958j 1. +0.j 0.7868537+0.61713958j]
24+
25+
# 2.
26+
p = np.array(
27+
[
28+
[0, 0.05, 0],
29+
[0.05 * (np.sqrt(3) / 2), 0.05 * (-1 / 2), 0],
30+
[0.05 * (-np.sqrt(3) / 2), 0.05 * (-1 / 2), 0],
31+
]
32+
)
33+
theta = np.pi / 4
34+
f = 1000
35+
print(array_manifold_vector3(p, theta, f))
36+
37+
# [0.7868537 +0.61713958j 0.97051349+0.2410468j 0.6148926 -0.78861086j]

khashimoto/chapter05/q04.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 空間相関行列の計算
2+
3+
import numpy as np
4+
5+
6+
def spatial_correlation_matrix(*X):
7+
M = len(X)
8+
F = X[0].shape[0]
9+
T = X[0].shape[1]
10+
R = np.zeros((F, M, M), dtype=complex)
11+
for f in range(F):
12+
sum = np.zeros((M, M), dtype=complex)
13+
for t in range(T):
14+
# a.
15+
x_ft = np.zeros(M, dtype=complex)
16+
for m in range(M):
17+
x_ft[m] = X[m][f][t]
18+
x_ft = np.array([x_ft]).T
19+
20+
# b.
21+
sum = np.add(sum, x_ft @ np.conj(x_ft.T))
22+
R[f] = sum / T
23+
return R
24+
25+
26+
X_1 = np.array([[1, -1j, -1, 1j], [2, -2j, -2, 2j], [3, -3j, -3, 3j]])
27+
X_2 = np.array([[4, -2j, 1, 0], [2, -1j, 0, 0], [1, -1j, 1, 0]])
28+
R = spatial_correlation_matrix(X_1, X_2)
29+
print(R[0])
30+
print(R[1])
31+
print(R[2])
32+
33+
# # [[1. +0.j 1.25+0.j]
34+
# [1.25+0.j 5.25+0.j]]
35+
# [[4. +0.j 1.5 +0.j]
36+
# [1.5 +0.j 1.25+0.j]]
37+
# [[9. +0.j 0.75+0.j]
38+
# [0.75+0.j 0.75+0.j]]

khashimoto/chapter05/q05.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 空間相関行列の確認
2+
3+
import numpy as np
4+
from q04 import spatial_correlation_matrix
5+
6+
7+
def zeropad(L, S, x):
8+
zero1 = np.zeros(L - S)
9+
x_pad = np.concatenate([zero1, x])
10+
zero2 = x_pad.size % S
11+
if zero2 != 0:
12+
x_pad = np.concatenate([x_pad, np.zeros(S - zero2)]) # 2.
13+
x_pad = np.concatenate([x_pad, np.zeros(L - S)])
14+
return x_pad
15+
16+
17+
def frame(L, S, x):
18+
x_pad = zeropad(L, S, x)
19+
T = (x_pad.size - L) // S + 1 # フレーム数
20+
x_t = np.zeros((T, L))
21+
for t in range(T):
22+
for l in range(L):
23+
x_t[t][l] = x_pad[t * S + l]
24+
return x_t
25+
26+
27+
def stft(L, S, w, x):
28+
x_t = frame(L, S, x)
29+
T = len(x_t)
30+
x_stft = np.zeros((T, L // 2 + 1), dtype=complex)
31+
for t in range(T):
32+
x_stft[t] = np.fft.rfft(x_t[t] * w)
33+
return x_stft.T
34+
35+
36+
fs = 16000
37+
ts = 5
38+
t = np.arange(ts * fs) / fs
39+
x = np.zeros((ts * fs, 2))
40+
x[:, 0] = np.random.normal(size=ts * fs)
41+
x[:, 1] = np.random.normal(size=ts * fs)
42+
43+
L = 512
44+
S = 256
45+
w = np.hanning(L)
46+
47+
x0_stft = stft(L, S, w, x[:, 0])
48+
x1_stft = stft(L, S, w, x[:, 1])
49+
50+
R_f = spatial_correlation_matrix(x0_stft, x1_stft)
51+
print(R_f[100].real)
52+
53+
# [[191.38231688 5.05200591]
54+
# [ 5.05200591 201.98183266]]

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)

0 commit comments

Comments
 (0)