Skip to content

Commit 09c2051

Browse files
authored
Merge pull request #16 from 153hashimoto/khashimoto/chapter03
Add chapter03
2 parents 1a6bf6d + 9128f78 commit 09c2051

File tree

10 files changed

+155
-0
lines changed

10 files changed

+155
-0
lines changed

khashimoto/chapter03/q01.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 線形畳み込み
2+
3+
import numpy as np
4+
5+
6+
def LinearConv(x, h):
7+
N = x.size
8+
z = np.zeros(2 * N - 1)
9+
for n in range(2 * N - 1):
10+
for k in range(N):
11+
if (n - k) < 0 or (n - k) > (N - 1):
12+
z[n] += 0
13+
else:
14+
z[n] += x[k] * h[n - k]
15+
return z

khashimoto/chapter03/q02.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 巡回畳み込み
2+
3+
import numpy as np
4+
5+
6+
def CircularConv(x, h):
7+
N = x.size
8+
z = np.zeros(N)
9+
for k in range(N):
10+
z += x[k] * h[(np.arange(N) - k) % N]
11+
return z

khashimoto/chapter03/q03.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 零詰め+巡回畳み込み
2+
3+
import numpy as np
4+
5+
6+
def ZeroPaddingCircularConv(x, h):
7+
N1 = x.size
8+
x = np.hstack((x, np.zeros(N1 - 1)))
9+
h = np.hstack((h, np.zeros(N1 - 1)))
10+
N = 2 * N1 - 1
11+
z = np.zeros(N)
12+
for k in range(N):
13+
z += x[k] * h[(np.arange(N) - k) % N]
14+
return z

khashimoto/chapter03/q04.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 各種畳み込みの関係
2+
3+
import numpy as np
4+
from q01 import LinearConv
5+
from q02 import CircularConv
6+
from q03 import ZeroPaddingCircularConv
7+
8+
x = np.array([4, 3, 2, 1])
9+
h = np.array([1, 0, -1, 0])
10+
print("線形畳み込み:")
11+
print(LinearConv(x, h))
12+
print("巡回畳み込み:")
13+
print(CircularConv(x, h))
14+
print("零詰めを行った巡回畳み込み:")
15+
print(ZeroPaddingCircularConv(x, h))

khashimoto/chapter03/q05.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 差分方程式(再帰なし)
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
x = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
7+
y = np.zeros(10)
8+
n = np.arange(10)
9+
y[n] = 0.2 * x[n] + 0.2 * x[n - 1] + 0.2 * x[n - 2] + 0.2 * x[n - 3] + 0.2 * x[n - 4]
10+
11+
plt.stem(y)
12+
plt.show()

khashimoto/chapter03/q06.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 差分方程式(再帰あり)
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
x = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
7+
y = np.zeros(10)
8+
for n in range(10):
9+
y[n] = 0.3 * y[n - 1] + 0.4 * x[n]
10+
11+
plt.stem(y)
12+
plt.show()

khashimoto/chapter03/q07.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 差分方程式(一般系)
2+
3+
import numpy as np
4+
5+
6+
def DifferenceEquation(a, b, x):
7+
N = a.size - 1
8+
M = b.size - 1
9+
L = x.size
10+
11+
y = np.zeros(L)
12+
ka = np.arange(1, N + 1)
13+
kb = np.arange(M + 1)
14+
for n in range(L):
15+
y[n] = (-sum(a[ka] * y[n - ka]) + sum(b[kb] * x[n - kb])) / a[0]
16+
17+
return y

khashimoto/chapter03/q08.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 周波数応答
2+
3+
import numpy as np
4+
5+
6+
def FreqRes(a, b, fs, f):
7+
N = a.size - 1
8+
M = b.size - 1
9+
10+
ka = np.arange(1, N + 1)
11+
kb = np.arange(M + 1)
12+
H = sum(b[kb] * np.exp(-1j * 2 * np.pi * f / fs * kb)) / (
13+
1 + sum(a[ka] * np.exp(-1j * 2 * np.pi * f / fs * ka))
14+
)
15+
return H

khashimoto/chapter03/q09.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+
import matplotlib.pyplot as plt
5+
from q08 import FreqRes
6+
7+
fs = 16000
8+
N = 10
9+
f = np.arange(0, fs, 1 / N)
10+
11+
a = np.array([0])
12+
b = np.array([0.2, 0.2, 0.2, 0.2, 0.2])
13+
H = np.zeros(f.size, dtype=complex)
14+
for i in range(f.size):
15+
H[i] = FreqRes(a, b, fs, f[i])
16+
17+
plt.subplot(2, 1, 1)
18+
plt.plot(abs(H))
19+
20+
plt.subplot(2, 1, 2)
21+
plt.plot(np.angle(H))
22+
plt.show()

khashimoto/chapter03/q10.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+
import matplotlib.pyplot as plt
5+
from q08 import FreqRes
6+
7+
fs = 16000
8+
N = 10
9+
f = np.arange(0, fs, 1 / N)
10+
11+
a = np.array([0.3])
12+
b = np.array([0.4])
13+
H = np.zeros(f.size, dtype=complex)
14+
for i in range(f.size):
15+
H[i] = FreqRes(a, b, fs, f[i])
16+
17+
plt.subplot(2, 1, 1)
18+
plt.plot(abs(H))
19+
20+
plt.subplot(2, 1, 2)
21+
plt.plot(np.angle(H))
22+
plt.show()

0 commit comments

Comments
 (0)