Skip to content

Commit 68ee6b5

Browse files
committed
Add Lab05 resampling visualization
1 parent dff7358 commit 68ee6b5

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

tools/generate_lab05_resampling.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
from pathlib import Path
5+
6+
ROOT = Path(__file__).resolve().parents[1]
7+
OUT = ROOT / "docs" / "assets" / "lab05_resampling_polyphase.png"
8+
OUT.parent.mkdir(parents=True, exist_ok=True)
9+
10+
fs = 8000
11+
N = 1024
12+
t = np.arange(N)/fs
13+
14+
# input signal
15+
x = np.sin(2*np.pi*700*t) + 0.5*np.sin(2*np.pi*1800*t)
16+
17+
L = 3
18+
M = 2
19+
20+
# naive upsample
21+
xu = np.zeros(len(x)*L)
22+
xu[::L] = x
23+
24+
# simple FIR (low-pass)
25+
num_taps = 63
26+
n = np.arange(num_taps) - (num_taps-1)/2
27+
h = np.sinc(n/L) * np.hanning(num_taps)
28+
h /= np.sum(h)
29+
30+
xf = np.convolve(xu, h, mode='same')
31+
32+
# decimate
33+
y = xf[::M]
34+
35+
plt.figure(figsize=(10,6))
36+
37+
plt.subplot(3,1,1)
38+
plt.plot(x[:200])
39+
plt.title("Input signal")
40+
41+
plt.subplot(3,1,2)
42+
plt.plot(xu[:400])
43+
plt.title("Upsampled (zero insertion)")
44+
45+
plt.subplot(3,1,3)
46+
plt.plot(y[:200])
47+
plt.title("Resampled output (L/M)")
48+
49+
plt.tight_layout()
50+
plt.savefig(OUT, dpi=150)
51+
print("Saved:", OUT)

0 commit comments

Comments
 (0)