Skip to content

Commit 58fae63

Browse files
committed
4章後半を追加
1 parent 32852b4 commit 58fae63

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed

khashimoto/chapter04/q06.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ISTFTの実装
2+
3+
import numpy as np
4+
from q05 import window
5+
6+
7+
def istft(S, X):
8+
F = len(X)
9+
T = X.shape[1]
10+
11+
# 1.
12+
N = 2 * (F - 1)
13+
M = S * (T - 1) + N
14+
15+
# 2.
16+
x = np.zeros(M)
17+
18+
# 3.
19+
z = np.zeros((N, T))
20+
for t in range(T):
21+
z[:, t] = np.fft.irfft(X[:, t])
22+
23+
# 4.
24+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(N) / (N - 1)) # Hamming窓
25+
w_s = window(S, w)
26+
n = np.arange(N)
27+
for t in range(T):
28+
x[t * S + n] = x[t * S + n] + w_s[n] * z[n, t]
29+
30+
return x

khashimoto/chapter04/q07.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ISTFTの確認
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
from q06 import istft
6+
from q03 import stft
7+
8+
A = 1
9+
f = 440
10+
fs = 16000
11+
time = np.arange(fs * 0.1) / fs
12+
x = A * np.sin(2 * np.pi * f * time) # 正弦波
13+
14+
L = 1000
15+
S = 500
16+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(L) / (L - 1)) # Hamming窓
17+
x_stft = stft(L, S, w, x) # 4. の結果
18+
19+
x_istft = istft(S, x_stft.T)
20+
21+
plt.plot(x_istft)
22+
plt.show()

khashimoto/chapter04/q08.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 合成窓の確認
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
from q03 import stft
6+
7+
8+
def istft_2(S, X):
9+
F = len(X)
10+
T = X.shape[1]
11+
12+
# 1.
13+
N = 2 * (F - 1)
14+
M = S * (T - 1) + N
15+
16+
# 2.
17+
x = np.zeros(M)
18+
19+
# 3.
20+
z = np.zeros((N, T))
21+
for t in range(T):
22+
z[:, t] = np.fft.irfft(X[:, t])
23+
24+
# 4.
25+
n = np.arange(N)
26+
for t in range(T):
27+
x[t * S + n] = x[t * S + n] + 1 * z[n, t] # w_s[n] = 1
28+
29+
return x
30+
31+
32+
A = 1
33+
f = 440
34+
fs = 16000
35+
time = np.arange(fs * 0.1) / fs
36+
x = A * np.sin(2 * np.pi * f * time) # 正弦波
37+
38+
L = 1000
39+
S = 500
40+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(L) / (L - 1)) # Hamming窓
41+
x_stft = stft(L, S, w, x) # 4. の結果
42+
43+
x_istft = istft_2(S, x_stft.T)
44+
45+
plt.plot(x_istft)
46+
plt.show()

khashimoto/chapter04/q09.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 不確定性原理の確認
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
from scipy.signal import chirp
6+
from q03 import stft
7+
8+
fs = 16000
9+
t = np.arange(0, fs) / fs
10+
sig = chirp(t, f0=100, f1=16000, t1=1)
11+
12+
# スペクトログラムのプロット
13+
14+
# 100/50
15+
L = 100
16+
S = 50
17+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(L) / (L - 1)) # Hamming窓
18+
sig_stft = stft(L, S, w, sig)
19+
20+
plt.subplot(4, 2, 1)
21+
plt.pcolormesh(abs(sig_stft).T)
22+
plt.title("Amplitude spectrum (100/50)")
23+
plt.subplot(4, 2, 2)
24+
plt.pcolormesh(np.angle(sig_stft).T)
25+
plt.title("Phase spectrum (100/50)")
26+
27+
# 200/100
28+
L = 200
29+
S = 100
30+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(L) / (L - 1)) # Hamming窓
31+
sig_stft = stft(L, S, w, sig)
32+
33+
plt.subplot(4, 2, 3)
34+
plt.pcolormesh(abs(sig_stft).T)
35+
plt.title("Amplitude spectrum (200/100)")
36+
plt.subplot(4, 2, 4)
37+
plt.pcolormesh(np.angle(sig_stft).T)
38+
plt.title("Phase spectrum (200/100)")
39+
40+
# 400/200
41+
L = 400
42+
S = 200
43+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(L) / (L - 1)) # Hamming窓
44+
sig_stft = stft(L, S, w, sig)
45+
46+
plt.subplot(4, 2, 5)
47+
plt.pcolormesh(abs(sig_stft).T)
48+
plt.title("Amplitude spectrum (400/200)")
49+
plt.subplot(4, 2, 6)
50+
plt.pcolormesh(np.angle(sig_stft).T)
51+
plt.title("Phase spectrum (400/200)")
52+
53+
# 800/400
54+
L = 800
55+
S = 400
56+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(L) / (L - 1)) # Hamming窓
57+
sig_stft = stft(L, S, w, sig)
58+
59+
plt.subplot(4, 2, 7)
60+
plt.pcolormesh(abs(sig_stft).T)
61+
plt.title("Amplitude spectrum (800/400)")
62+
plt.subplot(4, 2, 8)
63+
plt.pcolormesh(np.angle(sig_stft).T)
64+
plt.title("Phase spectrum (800/400)")
65+
plt.show()

khashimoto/chapter04/q10.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 時間周波数のインデクスと物理量との対応
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
from q03 import stft
6+
7+
8+
def fr_convert(x, fs, S):
9+
L = (len(x) - 1) * 2
10+
f = np.arange(len(x)) * fs / L
11+
return f
12+
13+
14+
def t_convert(x, fs, S):
15+
t = np.arange(x.shape[1]) * S / fs
16+
return t
17+
18+
19+
# 4. の結果を再度プロット
20+
A = 1
21+
f = 440
22+
fs = 16000
23+
time = np.arange(fs * 0.1) / fs
24+
x = A * np.sin(2 * np.pi * f * time) # 正弦波
25+
26+
L = 1000
27+
S = 500
28+
w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(L) / (L - 1)) # Hamming窓
29+
x_stft = stft(L, S, w, x)
30+
31+
32+
# プロット
33+
fr = fr_convert(x_stft.T, fs, S)
34+
t = t_convert(x_stft.T, fs, S)
35+
36+
plt.subplot(1, 2, 1)
37+
plt.pcolormesh(t, fr, abs(x_stft).T)
38+
plt.xlabel("Time[s]")
39+
plt.ylabel("Frequency[Hz]")
40+
plt.title("Amplitude spectrum")
41+
plt.subplot(1, 2, 2)
42+
plt.pcolormesh(t, fr, np.angle(x_stft).T)
43+
plt.xlabel("Time[s]")
44+
plt.ylabel("Frequency[Hz]")
45+
plt.title("Phase spectrum")
46+
plt.show()

0 commit comments

Comments
 (0)