Skip to content

Commit

Permalink
Merge pull request #19 from kuu8902/kkoiso/chapter03
Browse files Browse the repository at this point in the history
Add chapter03  koiso
  • Loading branch information
taishi-n authored Jun 3, 2024
2 parents 75bea75 + 4a10da1 commit 4f501aa
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kkoiso/chapter03/q01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np
import matplotlib.pyplot as plt


def linear_conv(x, h):
N = len(x)
z = np.zeros(2 * N - 1)
for n in range(2 * N - 1):
for k in range(N):
if 0 <= n - k <= N - 1:
z[n] = z[n] + x[k] * h[n - k]
return z
11 changes: 11 additions & 0 deletions kkoiso/chapter03/q02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np
import matplotlib.pyplot as plt


def circular_conv(x, h):
N = len(x)
z = np.zeros(N)
for n in range(N):
for k in range(N):
z[n] = z[n] + x[k] * h[(n - k) % N]
return z
11 changes: 11 additions & 0 deletions kkoiso/chapter03/q03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np
import matplotlib.pyplot as plt
from q02 import circular_conv


def zero_padded_circular_conv(x, h):
N = len(x)
x_padded = np.concatenate([x, np.zeros(N)])
h_padded = np.concatenate([h, np.zeros(N)])
z = circular_conv(x_padded, h_padded)
return z
33 changes: 33 additions & 0 deletions kkoiso/chapter03/q04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
import matplotlib.pyplot as plt
from q01 import linear_conv
from q02 import circular_conv
from q03 import zero_padded_circular_conv

x = [4, 3, 2, 1]
h = [1, 0, -1, 0]
x_padded = np.concatenate([x, np.zeros(4)])

z_linear = linear_conv(x, h)
z_circular = circular_conv(x, h)
z_zero_padded = zero_padded_circular_conv(x, h)

plt.figure(figsize=(12, 8))

plt.subplot(3, 1, 1)
plt.stem(z_linear)
plt.title("Linear Convolution")
plt.grid(True)

plt.subplot(3, 1, 2)
plt.stem(z_circular)
plt.title("Circular Convolution")
plt.grid(True)

plt.subplot(3, 1, 3)
plt.stem(z_zero_padded)
plt.title("Zero-padded Circular Convolution")
plt.grid(True)

plt.tight_layout()
plt.show()
27 changes: 27 additions & 0 deletions kkoiso/chapter03/q05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import numpy as np
import matplotlib.pyplot as plt


def difference_equations_no_recursion(x):
y = np.zeros(len(x))
for n in range(len(x)):
y[n] = 0.2 * x[n]
if n >= 1:
y[n] = y[n] + 0.2 * x[n - 1]
if n >= 2:
y[n] = y[n] + 0.2 * x[n - 2]
if n >= 3:
y[n] = y[n] + 0.2 * x[n - 3]
if n >= 4:
y[n] = y[n] + 0.2 * x[n - 4]
return y


x = np.zeros(10)
x[0] = 1
y = difference_equations_no_recursion(x)

plt.stem(y)
plt.title("Difference Equation (Non-recursive)")
plt.grid(True)
plt.show()
22 changes: 22 additions & 0 deletions kkoiso/chapter03/q06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import numpy as np
import matplotlib.pyplot as plt


def difference_equations_recursion(x):
y = np.zeros(len(x))
for n in range(len(x)):
if n == 0:
y[n] = 0.4 * x[n]
else:
y[n] = 0.3 * y[n - 1] + 0.4 * x[n]
return y


x = np.zeros(10)
x[0] = 1
y = difference_equations_recursion(x)

plt.stem(y)
plt.title("Difference Equation (Recursive)")
plt.grid(True)
plt.show()
18 changes: 18 additions & 0 deletions kkoiso/chapter03/q07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
import matplotlib.pyplot as plt


def general_difference_equations(a, b, x):
N = len(a)
M = len(b)
L = len(x)
y = np.zeros(L)
for n in range(1, L + 1):
for k in range(1, N + 1):
if n >= k:
y[n] = y[n] - a[k] * y[n - k]
for k in range(M + 1):
if n >= k:
y[n] = y[n] + b[k] * x[n - k]
y[n] = y[n] / a[0]
return y
20 changes: 20 additions & 0 deletions kkoiso/chapter03/q08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import matplotlib.pyplot as plt


def frequency_response(a, b, fs):
omega = np.linspace(0, 2 * np.pi, fs, endpoint=False)

num = np.zeros(len(omega), dtype=np.complex128)
den = np.zeros(len(omega), dtype=np.complex128)

for k in range(len(b)):
num = num + b[k] * np.exp(-1j * (omega) * k)

den = den + 1
for k in range(1, len(a)):
den = den + a[k] * np.exp(-1j * (omega) * k)

H = num / den

return omega, H
32 changes: 32 additions & 0 deletions kkoiso/chapter03/q09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
import matplotlib.pyplot as plt
from q08 import frequency_response

a = [1]
b = [0.2, 0.2, 0.2, 0.2, 0.2]
# b = [0.33, 0.33, 0.33]
fs = 16000
N = 16000
w, H = frequency_response(a, b, fs)
w = w[:N]
H = H[:N]


plt.figure(figsize=(12, 6))

plt.subplot(2, 1, 1)
plt.plot(w / (2 * np.pi) * fs, np.abs(H))
plt.title("Frequency Response - No-recursion (Amplitude)")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude (dB)")
plt.grid(True)

plt.subplot(2, 1, 2)
plt.plot(w / (2 * np.pi) * fs, np.angle(H))
plt.title("Frequency Response - No-recursion (Phase)")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Phase (radians)")
plt.grid(True)

plt.tight_layout()
plt.show()
29 changes: 29 additions & 0 deletions kkoiso/chapter03/q10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
import matplotlib.pyplot as plt
from q08 import frequency_response

a = [1, -0.3]
b = [0.4]
fs = 16000
N = 16000
w, H = frequency_response(a, b, fs)
w = w[:N]
H = H[:N]
plt.figure(figsize=(12, 6))

plt.subplot(2, 1, 1)
plt.plot(w / (2 * np.pi) * fs, np.abs(H))
plt.title("Frequency Response - Recursive (Ampliitude)")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude (dB)")
plt.grid(True)

plt.subplot(2, 1, 2)
plt.plot(w / (2 * np.pi) * fs, np.angle(H))
plt.title("Frequency Response - Recursive (Phase)")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Phase (radians)")
plt.grid(True)

plt.tight_layout()
plt.show()

0 comments on commit 4f501aa

Please sign in to comment.