Skip to content

Commit a2cf075

Browse files
authored
Merge pull request #20 from Yuto-Yamamoto01/yyamamoto/chapter02
2章を追加
2 parents 43e5cd2 + 555514b commit a2cf075

File tree

16 files changed

+287
-0
lines changed

16 files changed

+287
-0
lines changed

yyamamoto/chapter02/q01.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import math
2+
3+
def dft(x):
4+
N = len(x)
5+
X = [0] * N
6+
for k in range(N):
7+
for n in range(N):
8+
X[k] += x[n] * math.exp(-1j*2*math.pi*k*n/N)
9+
return X
10+
11+
def idft(X):
12+
N = len(X)
13+
x = [0] * N
14+
for n in range(N):
15+
for k in range(N):
16+
x[n] += X[k] * math.exp(1j*2*math.pi*k*n/N) / N
17+
return x

yyamamoto/chapter02/q02.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import math
2+
import cmath
3+
4+
def dft(x):
5+
N = len(x)
6+
X = [0] * N
7+
for k in range(N):
8+
for n in range(N):
9+
X[k] += x[n] * cmath.exp(-1j*2*math.pi*k*n/N)
10+
return X
11+
12+
def idft(X):
13+
N = len(X)
14+
x = [0] * N
15+
for n in range(N):
16+
for k in range(N):
17+
x[n] += X[k] * cmath.exp(1j*2*math.pi*k*n/N) / N
18+
return x
19+
20+
21+
# ここからq02
22+
impulse = [1, 0, 0, 0, 0, 0, 0, 0]
23+
print(dft(impulse))

yyamamoto/chapter02/q03.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import math
2+
import cmath
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
def dft(x):
7+
N = len(x)
8+
X = [0] * N
9+
for k in range(N):
10+
for n in range(N):
11+
X[k] += x[n] * cmath.exp(-1j*2*math.pi*k*n/N)
12+
return X
13+
14+
def idft(X):
15+
N = len(X)
16+
x = [0] * N
17+
for n in range(N):
18+
for k in range(N):
19+
x[n] += X[k] * cmath.exp(1j*2*math.pi*k*n/N) / N
20+
return x
21+
22+
impulse = [1, 0, 0, 0, 0, 0, 0, 0]
23+
dft_value = dft(impulse)
24+
25+
# ここからq03
26+
idft_value = idft(dft_value)
27+
t = np.arange(0, 8, 1)
28+
fig = plt.figure()
29+
plt.plot(idft_value)
30+
fig.savefig('q03_graph')

yyamamoto/chapter02/q03_graph.png

11.3 KB
Loading

yyamamoto/chapter02/q04.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import math
2+
import cmath
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
def dft(x):
7+
N = len(x)
8+
X = [0] * N
9+
for k in range(N):
10+
for n in range(N):
11+
X[k] += x[n] * cmath.exp(-1j*2*math.pi*k*n/N)
12+
return X
13+
14+
def idft(X):
15+
N = len(X)
16+
x = [0] * N
17+
for n in range(N):
18+
for k in range(N):
19+
x[n] += X[k] * cmath.exp(1j*2*math.pi*k*n/N) / N
20+
return x
21+
22+
23+
# ここからq02
24+
impulse = [1, 0, 0, 0, 0, 0, 0, 0]
25+
dft_value = dft(impulse)
26+
27+
A = 20 * np.log10(np.abs(dft_value))
28+
P = np.rad2deg(np.angle(dft_value))
29+
30+
fig = plt.figure()
31+
fig.add_subplot(1, 2, 1)
32+
plt.plot(A)
33+
plt.title("Amplitude Spectrum")
34+
fig.add_subplot(1, 2, 2)
35+
plt.plot(P)
36+
plt.title("Phase Spectrum")
37+
fig.savefig('q04_graph')

yyamamoto/chapter02/q04_graph.png

16.3 KB
Loading

yyamamoto/chapter02/q05.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
import math
3+
import cmath
4+
5+
def dft(x):
6+
N = len(x)
7+
X = [0] * N
8+
for k in range(N):
9+
for n in range(N):
10+
X[k] += x[n] * cmath.exp(-1j*2*math.pi*k*n/N)
11+
return X
12+
13+
def idft(X):
14+
N = len(X)
15+
x = [0] * N
16+
for n in range(N):
17+
for k in range(N):
18+
x[n] += X[k] * cmath.exp(1j*2*math.pi*k*n/N) / N
19+
return x
20+
21+
22+
impulse = [1, 0, 0, 0, 0, 0, 0, 0]
23+
print(dft(impulse))
24+
25+
26+
# ここからq05
27+
print(np.fft.fft(impulse))
28+
29+
# 同じ結果になった!!!!!

yyamamoto/chapter02/q06.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
A = 1 # 振幅
5+
f = 440 # 周波数 Hz
6+
sec = 3 # 信号長 s
7+
fs = 16000 # サンプリング周波数 Hz
8+
9+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
10+
x = A * np.sin(2*np.pi*f*t) # 正弦波の値
11+
12+
13+
# ここからq06
14+
dft_value = np.fft.fft(x)
15+
A = 20 * np.log10(np.abs(dft_value))
16+
P = np.rad2deg(np.angle(dft_value))
17+
18+
freq = fs * np.arange(len(A)) / len(A)
19+
# グラフの描画
20+
fig = plt.figure()
21+
fig.add_subplot(1,2,1)
22+
plt.plot(freq, A)
23+
plt.title('Amplitude spectrum')
24+
25+
fig.add_subplot(1,2,2)
26+
plt.plot(freq, P)
27+
plt.title('Phase Spectrum')
28+
fig.savefig('q06_graph')
29+
30+

yyamamoto/chapter02/q06_graph.png

27.4 KB
Loading

yyamamoto/chapter02/q07.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import numpy as np
2+
3+
def Hamming(N):
4+
n = np.arange(N)
5+
w = 0.54 - 0.46 * np.cos(2*np.pi*n/(N-1))
6+
return w

yyamamoto/chapter02/q08.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import math
4+
5+
def Hamming(N):
6+
n = np.arange(N)
7+
w = 0.54 - 0.46 * np.cos(2*np.pi*n/(N-1))
8+
return w
9+
10+
A = 1 # 振幅
11+
f = 440 # 周波数 Hz
12+
sec = 3 # 信号長 s
13+
fs = 16000 # サンプリング周波数 Hz
14+
15+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
16+
x = A * np.sin(2*np.pi*f*t) # 正弦波の値
17+
18+
# ここからq08
19+
N = len(x)
20+
y = np.zeros(N)
21+
H = Hamming(N)
22+
y = x * H
23+
24+
fig = plt.figure()
25+
plt.plot(t, y)
26+
fig.savefig('q08_graph')
27+

yyamamoto/chapter02/q08_graph.png

18.2 KB
Loading

yyamamoto/chapter02/q09.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import math
4+
5+
def Hamming(N):
6+
n = np.arange(N)
7+
w = 0.54 - 0.46 * np.cos(2*np.pi*n/(N-1))
8+
return w
9+
10+
# ここからq08
11+
N = 48000
12+
H = Hamming(N)
13+
dft_value = np.fft.fft(H)
14+
15+
A = 20 * np.log10(np.abs(dft_value))
16+
P = np.rad2deg(np.angle(dft_value))
17+
18+
fs = 16000
19+
freq = fs * np.arange(len(A)) / len(A)
20+
21+
# グラフの描画
22+
fig = plt.figure()
23+
fig.add_subplot(1,2,1)
24+
plt.plot(freq, A)
25+
plt.title('Amplitude spectrum')
26+
fig.add_subplot(1,2,2)
27+
plt.plot(freq, P)
28+
plt.title('Phase Spectrum')
29+
fig.savefig('q09_graph')

yyamamoto/chapter02/q09_graph.png

28.8 KB
Loading

yyamamoto/chapter02/q10.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import math
4+
import tqdm
5+
6+
A = 1 # 振幅
7+
f = 440 # 周波数 Hz
8+
sec = 0.5 # 信号長 s
9+
fs = 16000 # サンプリング周波数 Hz
10+
11+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
12+
x = A * np.sin(2*np.pi*f*t) # 正弦波の値
13+
14+
X = np.fft.fft(x)
15+
16+
def conv(x, h):
17+
N = len(x)
18+
n = np.arange(N)
19+
z = np.zeros(N, dtype=complex)
20+
for k in tqdm.tqdm(range(N)):
21+
z[k] = np.sum(x[n] * h[k-n])
22+
return z
23+
24+
25+
def Hamming(N):
26+
n = np.arange(N)
27+
w = np.zeros(N, dtype=complex)
28+
w = 0.54 - 0.46 * np.cos(2*np.pi*n/(N-1))
29+
return w
30+
31+
N = len(x)
32+
y = np.zeros(N)
33+
H = Hamming(N)
34+
y = x * H
35+
36+
# ここまでq08の内容
37+
38+
39+
N = 48000
40+
H = Hamming(N)
41+
Y = np.fft.fft(H)
42+
43+
Z = np.zeros(N)
44+
Z = conv(X, Y) # np.convolve は巡回畳み込みをしないので使えませんでした...
45+
z = np.fft.ifft(Z)
46+
print(z)
47+
print(y)
48+
49+
50+
51+
fig = plt.figure()
52+
fig.add_subplot(1,2,1)
53+
plt.plot(t, y)
54+
plt.title('q08')
55+
fig.add_subplot(1,2,2)
56+
plt.plot(t, z)
57+
plt.title('q10')
58+
fig.savefig('q10_graph')
59+

yyamamoto/chapter02/q10_graph.png

26.8 KB
Loading

0 commit comments

Comments
 (0)