From 4a10da1026ae0756dc17fbcc984e64498264b3dd Mon Sep 17 00:00:00 2001 From: kuu8902 Date: Mon, 3 Jun 2024 15:59:20 +0900 Subject: [PATCH] =?UTF-8?q?=EF=BC=93=E7=AB=A0=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkoiso/chapter03/q01.py | 12 ++++++++++++ kkoiso/chapter03/q02.py | 11 +++++++++++ kkoiso/chapter03/q03.py | 11 +++++++++++ kkoiso/chapter03/q04.py | 33 +++++++++++++++++++++++++++++++++ kkoiso/chapter03/q05.py | 27 +++++++++++++++++++++++++++ kkoiso/chapter03/q06.py | 22 ++++++++++++++++++++++ kkoiso/chapter03/q07.py | 18 ++++++++++++++++++ kkoiso/chapter03/q08.py | 20 ++++++++++++++++++++ kkoiso/chapter03/q09.py | 32 ++++++++++++++++++++++++++++++++ kkoiso/chapter03/q10.py | 29 +++++++++++++++++++++++++++++ 10 files changed, 215 insertions(+) create mode 100644 kkoiso/chapter03/q01.py create mode 100644 kkoiso/chapter03/q02.py create mode 100644 kkoiso/chapter03/q03.py create mode 100644 kkoiso/chapter03/q04.py create mode 100644 kkoiso/chapter03/q05.py create mode 100644 kkoiso/chapter03/q06.py create mode 100644 kkoiso/chapter03/q07.py create mode 100644 kkoiso/chapter03/q08.py create mode 100644 kkoiso/chapter03/q09.py create mode 100644 kkoiso/chapter03/q10.py diff --git a/kkoiso/chapter03/q01.py b/kkoiso/chapter03/q01.py new file mode 100644 index 0000000..6b27b49 --- /dev/null +++ b/kkoiso/chapter03/q01.py @@ -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 diff --git a/kkoiso/chapter03/q02.py b/kkoiso/chapter03/q02.py new file mode 100644 index 0000000..4252c10 --- /dev/null +++ b/kkoiso/chapter03/q02.py @@ -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 diff --git a/kkoiso/chapter03/q03.py b/kkoiso/chapter03/q03.py new file mode 100644 index 0000000..3f69f84 --- /dev/null +++ b/kkoiso/chapter03/q03.py @@ -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 diff --git a/kkoiso/chapter03/q04.py b/kkoiso/chapter03/q04.py new file mode 100644 index 0000000..8d86818 --- /dev/null +++ b/kkoiso/chapter03/q04.py @@ -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() diff --git a/kkoiso/chapter03/q05.py b/kkoiso/chapter03/q05.py new file mode 100644 index 0000000..12cfd54 --- /dev/null +++ b/kkoiso/chapter03/q05.py @@ -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() diff --git a/kkoiso/chapter03/q06.py b/kkoiso/chapter03/q06.py new file mode 100644 index 0000000..d907763 --- /dev/null +++ b/kkoiso/chapter03/q06.py @@ -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() diff --git a/kkoiso/chapter03/q07.py b/kkoiso/chapter03/q07.py new file mode 100644 index 0000000..47a80f0 --- /dev/null +++ b/kkoiso/chapter03/q07.py @@ -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 diff --git a/kkoiso/chapter03/q08.py b/kkoiso/chapter03/q08.py new file mode 100644 index 0000000..572e363 --- /dev/null +++ b/kkoiso/chapter03/q08.py @@ -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 diff --git a/kkoiso/chapter03/q09.py b/kkoiso/chapter03/q09.py new file mode 100644 index 0000000..3cc1510 --- /dev/null +++ b/kkoiso/chapter03/q09.py @@ -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() diff --git a/kkoiso/chapter03/q10.py b/kkoiso/chapter03/q10.py new file mode 100644 index 0000000..ee01980 --- /dev/null +++ b/kkoiso/chapter03/q10.py @@ -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()