Skip to content

Commit 44acbc5

Browse files
authored
Merge pull request #21 from Yuto-Yamamoto01/yyamamoto/chapter03
3章を追加
2 parents 67b6ce2 + 2e12a6b commit 44acbc5

File tree

15 files changed

+194
-0
lines changed

15 files changed

+194
-0
lines changed

yyamamoto/chapter03/q01.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
3+
def linear_conv(x, h):
4+
N = x.size
5+
z = np.zeros(2*N-1, dtype=complex)
6+
k = np.arange(N)
7+
h_addzeros = np.hstack([h, np.zeros(N)])
8+
for n in range(2*N-1):
9+
z[n] = np.sum(x[k] * h_addzeros[n-k])
10+
return z

yyamamoto/chapter03/q02.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy as np
2+
3+
def circular_conv(x, h):
4+
N = x.size
5+
z = np.zeros(N, dtype=complex)
6+
k = np.arange(N)
7+
for n in range(N):
8+
z[n] = np.sum(x[k] * h[(n-k) % N])
9+
return z

yyamamoto/chapter03/q03.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
3+
def linear_conv2(x, h):
4+
N = x.size
5+
z = np.zeros(N, dtype=complex)
6+
k = np.arange(N)
7+
h_addzeros = np.hstack([h, np.zeros(N)])
8+
for n in range(N):
9+
z[n] = np.sum(x[k] * h_addzeros[n-k])
10+
return z

yyamamoto/chapter03/q04.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q01 import linear_conv
4+
from q02 import circular_conv
5+
from q03 import linear_conv2
6+
7+
x = np.array([4,3,2,1])
8+
y = np.array([1,0,-1,0])
9+
10+
z1 = linear_conv(x, y)
11+
z2 = circular_conv(x, y)
12+
z3 = linear_conv2(x, y)
13+
14+
fig = plt.figure()
15+
fig.add_subplot(1,3,1)
16+
plt.stem(z1)
17+
plt.title('q01')
18+
fig.add_subplot(1,3,2)
19+
plt.stem(z2)
20+
plt.title('q02')
21+
fig.add_subplot(1,3,3)
22+
plt.stem(z3)
23+
plt.title('q03')
24+
25+
fig.savefig('q04_graph')

yyamamoto/chapter03/q04_graph.png

13.7 KB
Loading

yyamamoto/chapter03/q05.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
x = np.zeros(14)
5+
x[8] = 1
6+
y = np.zeros(10)
7+
n = np.arange(4, 14) # ファンシーインデックス
8+
y = 0.2 * (x[n] + x[n-1] + x[n-2] + x[n-3] + x[n-4])
9+
10+
fig = plt.figure()
11+
plt.stem(y)
12+
fig.savefig('q05_graph')

yyamamoto/chapter03/q05_graph.png

12.8 KB
Loading

yyamamoto/chapter03/q06.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def difference_equation(x, y, n):
5+
if n == 0:
6+
return 0.4 * x[n]
7+
else:
8+
return 0.3 * y[n-1] + 0.4 * x[n]
9+
10+
x = np.zeros(10)
11+
x[5] = 1
12+
y = np.zeros(10)
13+
14+
for n in range(10):
15+
y[n] = difference_equation(x, y, n)
16+
17+
fig = plt.figure()
18+
plt.stem(y)
19+
fig.savefig('q06_graph')

yyamamoto/chapter03/q06_graph.png

11.9 KB
Loading

yyamamoto/chapter03/q07.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
3+
def difference_equation(a, b, x):
4+
def calc_y(n):
5+
N = a.size
6+
M = b.size
7+
sum_a, sum_b = 0
8+
if n == 0:
9+
return np.sum(b[k_b] * x[n-k_b])
10+
else:
11+
for k_a in range(N):
12+
sum_a += a[k_a] * calc_y(n-k_a)
13+
for k_b in range(M):
14+
if n-k_b < 0:
15+
continue
16+
else:
17+
sum_b += b[k_b] * x[n-k_b]
18+
19+
return (-sum_a + sum_b) / a[0]
20+
21+
y = np.zeros(x.size)
22+
for n in range(x.size):
23+
y[n] = calc_y(n)
24+
25+
return y

yyamamoto/chapter03/q08.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
3+
def calc_H(a, b, omega):
4+
N = a.size
5+
M = b.size
6+
k_a = np.arange(1, N)
7+
k_b = np.arange(M)
8+
sum_a = np.sum(a[k_a] * np.exp(-1j*omega*k_a))
9+
sum_b = np.sum(b[k_b] * np.exp(-1j*omega*k_b))
10+
11+
return sum_b / (1 + sum_a)

yyamamoto/chapter03/q09.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q08 import calc_H
4+
5+
fs = 16000
6+
a = np.array([0, 0])
7+
b = np.full(3, 0.33)
8+
N = 10000
9+
10+
# グラフ用の配列
11+
omegas = np.zeros(N)
12+
Hs = np.zeros(N, dtype=complex)
13+
for i in range(N):
14+
f = i / N * fs
15+
omega = 2 * np.pi * f / fs
16+
omegas[i] = omega
17+
Hs[i] = calc_H(a, b, omega)
18+
19+
20+
A = np.abs(Hs)
21+
P = np.rad2deg(np.angle(Hs))
22+
# グラフの中心を調整
23+
omegas = omegas - 3
24+
A = np.roll(A, 2000)
25+
P = np.roll(P, 2000)
26+
27+
fig = plt.figure()
28+
fig.add_subplot(1, 2, 1)
29+
plt.stem(omegas, A)
30+
plt.title('Amplitude Characteristics')
31+
plt.xlabel('omega')
32+
fig.add_subplot(1, 2, 2)
33+
plt.stem(omegas, P)
34+
plt.title('Phase Characteristics')
35+
plt.xlabel('omega')
36+
fig.savefig('q09_graph')

yyamamoto/chapter03/q09_graph.png

29.2 KB
Loading

yyamamoto/chapter03/q10.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q08 import calc_H
4+
5+
fs = 16000
6+
a = np.array([0, 0.3])
7+
b = np.array([0.4, 0])
8+
N = 4000
9+
10+
# グラフ用の配列
11+
omegas = np.zeros(N)
12+
Hs = np.zeros(N, dtype=complex)
13+
for i in range(N):
14+
f = i / N * fs
15+
omega = 2 * np.pi * f / fs
16+
omegas[i] = omega
17+
Hs[i] = calc_H(a, b, omega)
18+
19+
20+
A = np.abs(Hs)
21+
P = np.rad2deg(np.angle(Hs))
22+
# グラフの中心を調整
23+
omegas = omegas - 3
24+
A = np.roll(A, 2000)
25+
P = np.roll(P, 2000)
26+
27+
fig = plt.figure()
28+
fig.add_subplot(1, 2, 1)
29+
plt.stem(omegas, A)
30+
plt.title('Amplitude Characteristics')
31+
plt.xlabel('omega')
32+
fig.add_subplot(1, 2, 2)
33+
plt.stem(omegas, P)
34+
plt.title('Phase Characteristics')
35+
plt.xlabel('omega')
36+
fig.savefig('q10_graph')
37+

yyamamoto/chapter03/q10_graph.png

23.9 KB
Loading

0 commit comments

Comments
 (0)