Skip to content

Commit 555514b

Browse files
2 parents 234d6bf + 43e5cd2 commit 555514b

37 files changed

+599
-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()

kkoiso/chapter03/q01.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+
5+
def linear_conv(x, h):
6+
N = len(x)
7+
z = np.zeros(2 * N - 1)
8+
for n in range(2 * N - 1):
9+
for k in range(N):
10+
if 0 <= n - k <= N - 1:
11+
z[n] = z[n] + x[k] * h[n - k]
12+
return z

kkoiso/chapter03/q02.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+
import matplotlib.pyplot as plt
3+
4+
5+
def circular_conv(x, h):
6+
N = len(x)
7+
z = np.zeros(N)
8+
for n in range(N):
9+
for k in range(N):
10+
z[n] = z[n] + x[k] * h[(n - k) % N]
11+
return z

kkoiso/chapter03/q03.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+
import matplotlib.pyplot as plt
3+
from q02 import circular_conv
4+
5+
6+
def zero_padded_circular_conv(x, h):
7+
N = len(x)
8+
x_padded = np.concatenate([x, np.zeros(N)])
9+
h_padded = np.concatenate([h, np.zeros(N)])
10+
z = circular_conv(x_padded, h_padded)
11+
return z

kkoiso/chapter03/q04.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 zero_padded_circular_conv
6+
7+
x = [4, 3, 2, 1]
8+
h = [1, 0, -1, 0]
9+
x_padded = np.concatenate([x, np.zeros(4)])
10+
11+
z_linear = linear_conv(x, h)
12+
z_circular = circular_conv(x, h)
13+
z_zero_padded = zero_padded_circular_conv(x, h)
14+
15+
plt.figure(figsize=(12, 8))
16+
17+
plt.subplot(3, 1, 1)
18+
plt.stem(z_linear)
19+
plt.title("Linear Convolution")
20+
plt.grid(True)
21+
22+
plt.subplot(3, 1, 2)
23+
plt.stem(z_circular)
24+
plt.title("Circular Convolution")
25+
plt.grid(True)
26+
27+
plt.subplot(3, 1, 3)
28+
plt.stem(z_zero_padded)
29+
plt.title("Zero-padded Circular Convolution")
30+
plt.grid(True)
31+
32+
plt.tight_layout()
33+
plt.show()

kkoiso/chapter03/q05.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+
4+
5+
def difference_equations_no_recursion(x):
6+
y = np.zeros(len(x))
7+
for n in range(len(x)):
8+
y[n] = 0.2 * x[n]
9+
if n >= 1:
10+
y[n] = y[n] + 0.2 * x[n - 1]
11+
if n >= 2:
12+
y[n] = y[n] + 0.2 * x[n - 2]
13+
if n >= 3:
14+
y[n] = y[n] + 0.2 * x[n - 3]
15+
if n >= 4:
16+
y[n] = y[n] + 0.2 * x[n - 4]
17+
return y
18+
19+
20+
x = np.zeros(10)
21+
x[0] = 1
22+
y = difference_equations_no_recursion(x)
23+
24+
plt.stem(y)
25+
plt.title("Difference Equation (Non-recursive)")
26+
plt.grid(True)
27+
plt.show()

kkoiso/chapter03/q06.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
5+
def difference_equations_recursion(x):
6+
y = np.zeros(len(x))
7+
for n in range(len(x)):
8+
if n == 0:
9+
y[n] = 0.4 * x[n]
10+
else:
11+
y[n] = 0.3 * y[n - 1] + 0.4 * x[n]
12+
return y
13+
14+
15+
x = np.zeros(10)
16+
x[0] = 1
17+
y = difference_equations_recursion(x)
18+
19+
plt.stem(y)
20+
plt.title("Difference Equation (Recursive)")
21+
plt.grid(True)
22+
plt.show()

kkoiso/chapter03/q07.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
5+
def general_difference_equations(a, b, x):
6+
N = len(a)
7+
M = len(b)
8+
L = len(x)
9+
y = np.zeros(L)
10+
for n in range(1, L + 1):
11+
for k in range(1, N + 1):
12+
if n >= k:
13+
y[n] = y[n] - a[k] * y[n - k]
14+
for k in range(M + 1):
15+
if n >= k:
16+
y[n] = y[n] + b[k] * x[n - k]
17+
y[n] = y[n] / a[0]
18+
return y

kkoiso/chapter03/q08.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
5+
def frequency_response(a, b, fs):
6+
omega = np.linspace(0, 2 * np.pi, fs, endpoint=False)
7+
8+
num = np.zeros(len(omega), dtype=np.complex128)
9+
den = np.zeros(len(omega), dtype=np.complex128)
10+
11+
for k in range(len(b)):
12+
num = num + b[k] * np.exp(-1j * (omega) * k)
13+
14+
den = den + 1
15+
for k in range(1, len(a)):
16+
den = den + a[k] * np.exp(-1j * (omega) * k)
17+
18+
H = num / den
19+
20+
return omega, H

kkoiso/chapter03/q09.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q08 import frequency_response
4+
5+
a = [1]
6+
b = [0.2, 0.2, 0.2, 0.2, 0.2]
7+
# b = [0.33, 0.33, 0.33]
8+
fs = 16000
9+
N = 16000
10+
w, H = frequency_response(a, b, fs)
11+
w = w[:N]
12+
H = H[:N]
13+
14+
15+
plt.figure(figsize=(12, 6))
16+
17+
plt.subplot(2, 1, 1)
18+
plt.plot(w / (2 * np.pi) * fs, np.abs(H))
19+
plt.title("Frequency Response - No-recursion (Amplitude)")
20+
plt.xlabel("Frequency (Hz)")
21+
plt.ylabel("Amplitude (dB)")
22+
plt.grid(True)
23+
24+
plt.subplot(2, 1, 2)
25+
plt.plot(w / (2 * np.pi) * fs, np.angle(H))
26+
plt.title("Frequency Response - No-recursion (Phase)")
27+
plt.xlabel("Frequency (Hz)")
28+
plt.ylabel("Phase (radians)")
29+
plt.grid(True)
30+
31+
plt.tight_layout()
32+
plt.show()

kkoiso/chapter03/q10.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+
from q08 import frequency_response
4+
5+
a = [1, -0.3]
6+
b = [0.4]
7+
fs = 16000
8+
N = 16000
9+
w, H = frequency_response(a, b, fs)
10+
w = w[:N]
11+
H = H[:N]
12+
plt.figure(figsize=(12, 6))
13+
14+
plt.subplot(2, 1, 1)
15+
plt.plot(w / (2 * np.pi) * fs, np.abs(H))
16+
plt.title("Frequency Response - Recursive (Ampliitude)")
17+
plt.xlabel("Frequency (Hz)")
18+
plt.ylabel("Amplitude (dB)")
19+
plt.grid(True)
20+
21+
plt.subplot(2, 1, 2)
22+
plt.plot(w / (2 * np.pi) * fs, np.angle(H))
23+
plt.title("Frequency Response - Recursive (Phase)")
24+
plt.xlabel("Frequency (Hz)")
25+
plt.ylabel("Phase (radians)")
26+
plt.grid(True)
27+
28+
plt.tight_layout()
29+
plt.show()

progress.png

194 Bytes
Loading

0 commit comments

Comments
 (0)