Skip to content

Commit 151187a

Browse files
authored
Merge pull request #17 from woodnx/skotsugi/chapter03
Add skotsugi/chapter03
2 parents 0f4e0dc + 610e67b commit 151187a

File tree

16 files changed

+229
-0
lines changed

16 files changed

+229
-0
lines changed

skotsugi/chapter03/q01.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
3+
def linear_conv(x, h):
4+
N = len(x)
5+
M = 2*N - 1
6+
z = np.zeros(M, dtype = 'complex_')
7+
8+
for i in range(M):
9+
for k in range(N):
10+
j = i - k
11+
12+
if j < 0 or j > N - 1:
13+
continue
14+
else:
15+
z[i] += x[k] * h[j]
16+
17+
return z

skotsugi/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+
3+
def circular_conv(x, h):
4+
N = len(x)
5+
z = np.zeros(N, dtype = 'complex_')
6+
7+
for i in range(N):
8+
for k in range(N):
9+
z[i] += x[k] * h[(i - k) % N]
10+
11+
return z

skotsugi/chapter03/q03.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+
3+
def circular_zero_conv(x, h):
4+
N = len(x)
5+
M = N * 2
6+
7+
x_zero = np.zeros(M, dtype = 'complex_')
8+
x_zero[:N] = x
9+
10+
h_zero = np.zeros(M, dtype = 'complex_')
11+
h_zero[:N] = h
12+
13+
z = np.zeros(M, dtype = 'complex_')
14+
15+
for i in range(M):
16+
for k in range(M):
17+
j = (i - k) % M
18+
z[i] += x_zero[k] * h_zero[j]
19+
20+
return z

skotsugi/chapter03/q04.png

12.8 KB
Loading

skotsugi/chapter03/q04.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import matplotlib.pyplot as plt
2+
from q01 import linear_conv
3+
from q02 import circular_conv
4+
from q03 import circular_zero_conv
5+
6+
x = [4, 3, 2, 1]
7+
y = [1, 0, -1, 0]
8+
9+
z_l = linear_conv(x, y)
10+
z_c = circular_conv(x, y)
11+
z_z = circular_zero_conv(x, y)
12+
13+
fig, ax = plt.subplots(3, 1, sharex=True, sharey=True)
14+
15+
ax[0].grid()
16+
ax[1].grid()
17+
ax[2].grid()
18+
19+
ax[0].stem(z_l.real)
20+
ax[1].stem(z_c.real)
21+
ax[2].stem(z_z.real)
22+
23+
plt.savefig('./skotsugi/chapter03/q04.png')

skotsugi/chapter03/q05.png

14.5 KB
Loading

skotsugi/chapter03/q05.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+
N = 10
5+
x = np.zeros(N)
6+
x[0] = 1
7+
8+
i = np.arange(N)
9+
y = 0.2*x[i] + 0.2*x[i-1] + 0.2*x[i-2] + 0.2*x[i-3] + 0.2*x[i-4]
10+
11+
fig, ax = plt.subplots(2, 1, sharex=True)
12+
13+
ax[0].grid()
14+
ax[1].grid()
15+
16+
ax[0].stem(x)
17+
ax[1].stem(y)
18+
19+
plt.savefig('./skotsugi/chapter03/q05.png')

skotsugi/chapter03/q06.png

14.2 KB
Loading

skotsugi/chapter03/q06.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
N = 10
5+
x = np.zeros(N)
6+
x[0] = 1 # impulse
7+
8+
def y(i: int):
9+
if i < 0: return 0
10+
return 0.3*y(i-1) + 0.4*x[i]
11+
12+
i = np.arange(N)
13+
14+
result = [y(i) for i in range(N)]
15+
16+
fig, ax = plt.subplots(2, 1, sharex=True)
17+
18+
ax[0].grid()
19+
ax[1].grid()
20+
21+
ax[0].stem(x)
22+
ax[1].stem(result)
23+
24+
plt.savefig('./skotsugi/chapter03/q06.png')

skotsugi/chapter03/q07.png

14.2 KB
Loading

skotsugi/chapter03/q07.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
3+
def diff_equation_n(i, a, b, x):
4+
N = len(a)
5+
M = len(b)
6+
7+
if i < 0:
8+
return 0
9+
10+
y_sum = 0
11+
x_sum = 0
12+
for k in range(1, N):
13+
j = i - k
14+
y_sum += -a[k] * diff_equation_n(j, a, b, x)
15+
16+
for k in range(M):
17+
j = i - k
18+
x_sum += (b[k] * x[j])
19+
return y_sum + x_sum
20+
21+
def diff_equation(a, b, x):
22+
L = len(x)
23+
return [diff_equation_n(i, a, b, x) for i in range(L)]
24+
25+
26+
if __name__ == "__main__":
27+
import matplotlib.pyplot as plt
28+
29+
N = 10
30+
x = np.zeros(N)
31+
x[0] = 1 # impulse
32+
33+
a = [ 1, -0.3 ]
34+
b = [ 0.4 ]
35+
y = diff_equation(a, b, x)
36+
37+
fig, ax = plt.subplots(2, 1, sharex=True)
38+
39+
ax[0].grid()
40+
ax[1].grid()
41+
42+
ax[0].stem(x)
43+
ax[1].stem(y)
44+
45+
plt.savefig('./skotsugi/chapter03/q07.png')

skotsugi/chapter03/q08.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
3+
def freqz_w(w, a, b):
4+
N = len(a)
5+
M = len(b)
6+
7+
exp_a = np.array([ np.exp(-1j * w * i) for i in range(N)])
8+
exp_b = np.array([ np.exp(-1j * w * i) for i in range(M)])
9+
10+
return np.sum(exp_b * b) / (np.sum(exp_a[1:N] * a[1:N]) + 1)
11+
12+
def freqz(w, a, b):
13+
return [freqz_w(i, a, b) for i in w]

skotsugi/chapter03/q09.png

20.1 KB
Loading

skotsugi/chapter03/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+
from q07 import diff_equation
4+
from q08 import freqz
5+
6+
# impulse
7+
L = 10
8+
x = np.zeros(L)
9+
x[0] = 1
10+
a = [0]
11+
b = [0.2, 0.2, 0.2, 0.2, 0.2]
12+
13+
y = diff_equation(a, b, x)
14+
15+
fs = 16000
16+
N = 400
17+
f = np.arange(0, 1, 1 / N) * fs
18+
w = 2 * np.pi * f / fs
19+
20+
result = freqz(w, a, b)
21+
22+
A = np.abs(result)
23+
theta = np.angle(result)
24+
theta[A < 10**-5] = 0
25+
26+
fig, ax = plt.subplots(1, 2)
27+
ax[0].stem(A)
28+
ax[1].stem(theta)
29+
plt.savefig('./skotsugi/chapter03/q09.png')

skotsugi/chapter03/q10.png

28.8 KB
Loading

skotsugi/chapter03/q10.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q07 import diff_equation
4+
from q08 import freqz
5+
6+
N = 10
7+
x = np.zeros(N)
8+
x[0] = 1 # impulse
9+
10+
a = [ 1, -0.3 ]
11+
b = [ 0.4 ]
12+
y = diff_equation(a, b, x)
13+
14+
fs = 16000
15+
N = 400
16+
f = np.arange(0, 1, 1 / N) * fs
17+
w = 2 * np.pi * f / fs
18+
19+
result = freqz(w, a, b)
20+
21+
A = np.abs(result)
22+
theta = np.angle(result)
23+
theta[A < 10**-5] = 0
24+
25+
fig, ax = plt.subplots(1, 2)
26+
ax[0].plot(A)
27+
ax[1].plot(theta)
28+
plt.savefig('./skotsugi/chapter03/q10.png')

0 commit comments

Comments
 (0)