Skip to content

Commit eb2837e

Browse files
committed
2章を追加
1 parent ae1572c commit eb2837e

File tree

10 files changed

+239
-0
lines changed

10 files changed

+239
-0
lines changed

khashimoto/chapter02/q01.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# DFT/IDFT の実装: N 点の信号の離散フーリエ変換 (DFT)とその逆変換(IDFT)を計算する関数を実装せよ.
2+
3+
import numpy as np
4+
5+
def dft(x):
6+
N = len(x)
7+
dft_X = np.zeros(N, dtype=complex)
8+
for k in range(N):
9+
for n in range(N):
10+
dft_X[k] += x[n] * np.exp(-1j * 2 * np.pi * k * n / N)
11+
return dft_X
12+
13+
def idft(X):
14+
N = len(X)
15+
idft_x = np.zeros(N, dtype=complex)
16+
for n in range(N):
17+
for k in range(N):
18+
idft_x[n] += X[k] * np.exp(1j * 2 * np.pi * k * n / N)
19+
idft_x[n] /= N
20+
return idft_x
21+
22+
23+
# --------------- 確認----------------------
24+
x_1 = np.array([1, 0, 0, 0, 0])
25+
X_1 = dft(x_1)
26+
print(X_1)
27+
x_2 = idft(x_1)
28+
print(x_2)

khashimoto/chapter02/q02.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# DFT の確認: 1.で実装した関数を用いて 8 点の単位インパルス信号 の DFT を計算せよ.
2+
3+
import numpy as np
4+
5+
def dft(x):
6+
N = len(x)
7+
dft_X = np.zeros(N, dtype=complex)
8+
for k in range(N):
9+
for n in range(N):
10+
dft_X[k] += x[n] * np.exp(-1j * 2 * np.pi * k * n / N)
11+
return dft_X
12+
13+
delta = np.array([1, 0, 0, 0, 0, 0, 0, 0])
14+
X = dft(delta)
15+
print(X)

khashimoto/chapter02/q03.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# IDFT の確認: 2.の結果の IDFT を計算しプロットせよ.
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
def dft(x):
7+
N = len(x)
8+
dft_X = np.zeros(N, dtype=complex)
9+
for k in range(N):
10+
for n in range(N):
11+
dft_X[k] += x[n] * np.exp(-1j * 2 * np.pi * k * n / N)
12+
return dft_X
13+
14+
def idft(X):
15+
N = len(X)
16+
idft_x = np.zeros(N, dtype=complex)
17+
for n in range(N):
18+
for k in range(N):
19+
idft_x[n] += X[k] * np.exp(1j * 2 * np.pi * k * n / N)
20+
idft_x[n] /= N
21+
return idft_x
22+
23+
24+
25+
delta = np.array([1, 0, 0, 0, 0, 0, 0, 0])
26+
X = dft(delta) # 2. の結果
27+
idft_x = idft(X)
28+
plt.stem(idft_x.real)
29+
plt.show()
30+
31+
32+
# ---------------- 確認 ----------------------
33+
'''
34+
x = np.arange(0, 2 * np.pi, 0.1)
35+
36+
fig = plt.figure()
37+
plt.subplot(5,1,1)
38+
y1 = np.sin(x)
39+
plt.plot(y1)
40+
41+
plt.subplot(5,1,2)
42+
Y = dft(y1)
43+
plt.plot(Y.real)
44+
45+
plt.subplot(5,1,3)
46+
plt.plot(Y.imag)
47+
48+
y2 = idft(Y)
49+
plt.subplot(5,1,4)
50+
plt.plot(y2.real)
51+
52+
plt.subplot(5,1,5)
53+
plt.plot(y2.imag)
54+
55+
plt.show()
56+
'''

khashimoto/chapter02/q04.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# スペクトルの確認: 2.の結果の振幅スペクトルおよび位相スペクトル をプロットせよ.
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
def dft(x):
7+
N = len(x)
8+
dft_X = np.zeros(N, dtype=complex)
9+
for k in range(N):
10+
for n in range(N):
11+
dft_X[k] += x[n] * np.exp(-1j * 2 * np.pi * k * n / N)
12+
return dft_X
13+
14+
delta = np.array([1, 0, 0, 0, 0, 0, 0, 0])
15+
X = dft(delta) # 2. の結果
16+
17+
plt.subplot(2,1,1)
18+
plt.stem(abs(X))
19+
20+
plt.subplot(2,1,2)
21+
plt.stem(np.angle(X))
22+
plt.show()

khashimoto/chapter02/q05.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 既存の実装との比較: 8 点の単位インパルス信号の DFT を numpy.fft.fft 関数を用いて計算し,2.の結果と比較せよ.なお,これ以降 DFT を計算する場合は numpy.fft.fft 関数を使用してよい.
2+
3+
import numpy as np
4+
5+
delta = np.array([1, 0, 0, 0, 0, 0, 0, 0])
6+
X = np.fft.fft(delta)
7+
print(X)
8+
# 2. と同じ結果となった

khashimoto/chapter02/q06.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 正弦波のスペクトル: 第 1 章 1.で作成した信号の DFT を計算し,振幅スペクトルと位相スペクトルをプロットせよ.(プロットする際はデシベル表記にするために 20 * log10(np.abs(X)) などのようにしてデシベル表記になおしてから計算すると見やすいです.)
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
A = 1 # 振幅
7+
f = 440 # 周波数
8+
fs = 16000 # サンプリング周波数
9+
T = 3
10+
t = np.arange(fs * T) / fs
11+
x = A * np.sin(2 * np.pi * f * t) # 第1章1. で作成した信号
12+
13+
X = np.fft.fft(x)
14+
15+
16+
# 振幅スペクトルと位相スペクトルをプロット
17+
plt.subplot(2,1,1)
18+
plt.plot(20 * np.log10(abs(X)))
19+
#plt.stem(abs(X))
20+
21+
plt.subplot(2,1,2)
22+
plt.plot(np.angle(X))
23+
plt.show()

khashimoto/chapter02/q07.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 窓関数: 次式で定義される 点の Hamming 窓を作成する関数を実装せよ.
2+
3+
import numpy as np
4+
5+
def hamming(N):
6+
w = np.zeros(N)
7+
for n in range(N):
8+
w[n] = 0.54-0.46 * np.cos(2 * np.pi * n / (N-1))
9+
return w
10+
11+
12+
# ------------- 確認 ----------------
13+
w = hamming(5)
14+
print(w)

khashimoto/chapter02/q08.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 窓関数の確認: 第 1 章 1.で作成した信号に対して 6.の窓関数を適用した信号をプロットせよ.
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
def hamming(N):
7+
w = np.zeros(N)
8+
for n in range(N):
9+
w[n] = 0.54-0.46 * np.cos(2 * np.pi * n / (N-1))
10+
return w
11+
12+
13+
A = 1 # 振幅
14+
f = 440 # 周波数
15+
fs = 16000 # サンプリング周波数
16+
T = 3
17+
t = np.arange(fs * T) / fs
18+
x = A * np.sin(2 * np.pi * f * t) # 第1章1. で作成した信号
19+
20+
w = hamming(fs*T)
21+
x2 = x * w
22+
23+
plt.plot(t, x2)
24+
plt.show()

khashimoto/chapter02/q09.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 窓関数の特性: 第 1 章 1.で作成した信号と同じ長さの Hamming 窓を作成し,DFT を計算せよ.
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
def hamming(N):
7+
w = np.zeros(N)
8+
for n in range(N):
9+
w[n] = 0.54-0.46 * np.cos(2 * np.pi * n / (N-1))
10+
return w
11+
12+
fs = 16000 # サンプリング周波数
13+
T = 3 # 時間
14+
w = hamming(fs*T)
15+
w = np.fft.fft(w)
16+
print(w)

khashimoto/chapter02/q10.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 窓関数とスペクトルの関係:
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
def hamming(N):
7+
w = np.zeros(N)
8+
for n in range(N):
9+
w[n] = 0.54-0.46 * np.cos(2 * np.pi * n / (N-1))
10+
return w
11+
12+
13+
A = 1 # 振幅
14+
f = 440 # 周波数
15+
fs = 16000 # サンプリング周波数
16+
T = 3
17+
t = np.arange(fs * T) / fs
18+
x = A * np.sin(2 * np.pi * f * t) # 第1章1. で作成した信号
19+
X = np.fft.fft(x)
20+
21+
w = hamming(fs*T)
22+
Y = np.fft.fft(w) # 9. の結果
23+
24+
# X[k]とY[k]の巡回畳み込み
25+
Z = np.zeros(fs*T, dtype=complex)
26+
for k in range(fs*T):
27+
for n in range(fs*T):
28+
Z[k] += X[n] * Y[k-n]
29+
30+
idft_z = np.fft.ifft(Z) / (fs * T)
31+
32+
plt.plot(t, idft_z.real)
33+
plt.show()

0 commit comments

Comments
 (0)