---
title: "Modul teori — Filter frekuensi, restorasi & kompresi JPEG"
subtitle: "PCDv2 — pertemuan ke-7"
lang: id
---

## Tujuan pembelajaran

1. Menjelaskan **DFT/FFT 2D** dan filter **Ideal/Butterworth** (LP, HP, BP, BR).
2. Mengaitkan filter frekuensi dengan **restorasi** (denoise LP, notch).
3. Menggambarkan alur **kompresi JPEG** (DCT, kuantisasi) dan cuplikan **wavelet**.

# Transformasi Fourier 2D

Konvolusi spasial ↔ perkalian spektrum: $f * h \Leftrightarrow F \cdot H$. **Low-pass** → blur; **high-pass** → tepi; **band-pass/reject** → seleksi band.

Filter **Ideal** → **ringing** Gibbs; **Butterworth** order $n$, cutoff $D_0$ — transisi lebih halus.

# Restorasi frekuensi

- **Denoise LP:** Butterworth LP pada citra ber-noise Gaussian ([P3](pcd03_histogram_noise.qmd)).
- **Notch / band-reject:** menghapus komponen frekansi periodik (moiré, stripling).
- **Wiener (konsep):** filter adaptif spektrum signal/noise — tanpa derivasi penuh di modul ini.

# Kompresi citra

**RLE** efektif pada citra biner dengan run panjang.

**JPEG (lossy):**

1. Blok $8\times8$ → **DCT** (basis frekansi diskret).
2. **Kuantisasi** koefisien (buang detail frekansi tinggi).
3. Entropy coding (Huffman) — gambaran.

**Wavelet (cuplikan):** dekomposisi multi-resolusi alternatif DCT; dipakai JPEG2000 — tidak dibahas implementasi penuh.

```{python}
#| echo: true
#| label: fig-pcd07-fft-lp
#| fig-cap: "Spektrum magnitude log + efek low-pass Butterworth (sintetis)."
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage

rng = np.random.default_rng(2)
f = rng.random((128, 128))
f = ndimage.gaussian_filter(f, 2) + 0.05 * rng.standard_normal((128, 128))
F = np.fft.fftshift(np.fft.fft2(f))
mag = np.log1p(np.abs(F))
# Butterworth LP sederhana di domain freq (edukatif)
h, w = f.shape
cy, cx = h // 2, w // 2
yy, xx = np.mgrid[:h, :w]
D = np.sqrt((yy - cy) ** 2 + (xx - cx) ** 2)
D0, n = 20, 2
H = 1 / (1 + (D / (D0 + 1e-9)) ** (2 * n))
G = np.real(np.fft.ifft2(np.fft.ifftshift(np.fft.fftshift(np.fft.fft2(f)) * H)))
fig, axes = plt.subplots(1, 3, figsize=(9, 3))
axes[0].imshow(f, cmap="gray")
axes[0].set_title("Asli + noise")
axes[1].imshow(mag, cmap="magma")
axes[1].set_title("|FFT| log")
axes[2].imshow(G, cmap="gray")
axes[2].set_title("Butterworth LP")
for ax in axes:
    ax.axis("off")
plt.tight_layout()
fig
```

# Latihan mandiri

1. Mengapa filter Ideal jarang dipakai pada citra nyata?
2. Pada JPEG, peran **DCT** dalam memusatkan energi blok $8\times8$?
3. Band-reject cocok untuk noise jenis apa?

# Referensi

- Gonzalez & Woods, bab filtering in frequency domain & image compression.
