-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from kuu8902/kkoiso/chapter03
Add chapter03 koiso
- Loading branch information
Showing
10 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |