-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Convolution.py
More file actions
64 lines (51 loc) · 1.57 KB
/
Copy pathCircular_Convolution.py
File metadata and controls
64 lines (51 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import matplotlib.pyplot as plt
# --- Input Sequences ---
x = np.array([1, 2, 3, 4])
h = np.array([1, 0, 1, 0])
# --- Padding to Equal Length ---
N = max(len(x), len(h))
x = np.pad(x, (0, N - len(x)))
h = np.pad(h, (0, N - len(h)))
# --- Manual Circular Convolution ---
def circular_convolution_manual(x, h):
N = len(x)
y = np.zeros(N)
for n in range(N):
for k in range(N):
y[n] += x[k] * h[(n - k) % N]
return y
y_manual = circular_convolution_manual(x, h)
# --- FFT-based Circular Convolution ---
X = np.fft.fft(x)
H = np.fft.fft(h)
Y = X * H
y_fft = np.fft.ifft(Y).real # Remove small imaginary errors
# --- Plotting ---
fig, axs = plt.subplots(4, 1, figsize=(10, 10))
# 1. Input signal x[n]
axs[0].stem(range(N), x, basefmt=" ")
axs[0].set_title("Input Signal x[n]")
axs[0].set_xlabel("n")
axs[0].set_ylabel("x[n]")
axs[0].grid(True)
# 2. Impulse response h[n]
axs[1].stem(range(N), h, basefmt=" ", linefmt="orange", markerfmt="ro")
axs[1].set_title("Impulse Response h[n]")
axs[1].set_xlabel("n")
axs[1].set_ylabel("h[n]")
axs[1].grid(True)
# 3. Circular Convolution (Manual)
axs[2].stem(range(N), y_manual, basefmt=" ", linefmt="green", markerfmt="go")
axs[2].set_title("Circular Convolution (Manual)")
axs[2].set_xlabel("n")
axs[2].set_ylabel("y[n]")
axs[2].grid(True)
# 4. Circular Convolution (FFT-based)
axs[3].stem(range(N), y_fft, basefmt=" ", linefmt="red", markerfmt="mo")
axs[3].set_title("Circular Convolution (FFT-based)")
axs[3].set_xlabel("n")
axs[3].set_ylabel("y[n]")
axs[3].grid(True)
plt.tight_layout()
plt.show()