Skip to content

Commit 0e720b5

Browse files
committed
part 5: filters
1 parent e2685ac commit 0e720b5

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

notebooks/05_filters.ipynb

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 5 Filters\n",
8+
"\n",
9+
"To filter signals on graphs, we need to define filters. They are represented in the toolbox by the [`pygsp.filters.Filter` class](https://pygsp.readthedocs.io/en/stable/reference/filters.html). Filters are usually defined in the spectral domain. Given the transfer function\n",
10+
"\n",
11+
"**TODO**\n",
12+
"* look at <https://pygsp.readthedocs.io/en/stable/tutorials/intro.html#filters>\n",
13+
"* localization as great tool to visualize filters in the vertex domain"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"import numpy as np\n",
23+
"import matplotlib.pyplot as plt\n",
24+
"from pygsp import graphs, filters"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"## 5.1 Heat diffusion\n",
32+
"\n",
33+
"**TODO**: show that this is heat diffusion"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"G1 = graphs.Sensor(seed=42)\n",
43+
"G1.compute_fourier_basis()\n",
44+
"G2 = graphs.Ring(N=100)\n",
45+
"G2.compute_fourier_basis()\n",
46+
"G2.set_coordinates('line1D')\n",
47+
"\n",
48+
"TAUS = [0, 5, 100]\n",
49+
"DELTA = 10\n",
50+
"\n",
51+
"fig, axes = plt.subplots(len(TAUS), 3, figsize=(15, 6))\n",
52+
"\n",
53+
"for i, tau in enumerate(TAUS):\n",
54+
" g1 = filters.Heat(G1, tau)\n",
55+
" g2 = filters.Heat(G2, tau)\n",
56+
" \n",
57+
" y = g1.localize(DELTA).squeeze()\n",
58+
" G1.plot_signal(y, ax=axes[i, 0])\n",
59+
" axes[i, 0].set_axis_off()\n",
60+
" axes[i, 0].text(0, -0.2, '$y^T L y = {:.2f}$'.format(y.T @ G1.L @ y))\n",
61+
" \n",
62+
" G2.plot_signal(g2.localize(G2.N//2), ax=axes[i, 2])\n",
63+
" \n",
64+
" g1.plot(ax=axes[i, 1])\n",
65+
" axes[i, 1].set_xlabel('')\n",
66+
" axes[i, 1].set_ylabel('')\n",
67+
" text = r'$\\hat{{g}}(\\lambda) = \\exp \\left( \\frac{{-{{{}}} \\lambda}}{{\\lambda_{{max}}}} \\right)$'.format(tau)\n",
68+
" axes[i, 1].text(6, 0.5, text, fontsize=15)\n",
69+
" \n",
70+
"axes[0, 0].set_title('$y = \\hat{{g}}(L) \\delta_{{{}}}$: localized on sensor'.format(DELTA))\n",
71+
"axes[0, 1].set_title('$\\hat{g}(\\lambda)$: filter defined in the spectral domain')\n",
72+
"axes[0, 2].set_title('$y = \\hat{{g}}(L) \\delta_{{{}}}$: localized on ring graph'.format(G2.N//2))\n",
73+
"axes[-1, 1].set_xlabel(\"$\\lambda$: laplacian's eigenvalues / graph frequencies\")"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"## 5.2 Filterbanks\n",
81+
"\n",
82+
"**TODO**:\n",
83+
"* popular filterbanks\n",
84+
"* tight vs non-tight"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"G = graphs.Ring(N=20)\n",
94+
"G.estimate_lmax()\n",
95+
"G.set_coordinates('line1D')\n",
96+
"g = filters.HalfCosine(G)\n",
97+
"s = g.localize(G.N // 2)\n",
98+
"g.plot()\n",
99+
"G.plot_signal(s)"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"## 5.3 Approximations"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"**TODO**\n",
114+
"* Approximation with Chebyshev polynomials.\n",
115+
"* Show computational advantage.\n",
116+
"* Show how it smoothes the original filterbank."
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"## 5.4 Exercise\n",
124+
"\n",
125+
"Solve the following problem using a graph filter:\n",
126+
"$$\\mathbf{x}^* = \\operatorname*{arg\\,min}_{\\mathbf{x} \\in \\mathbb{R}^N} \\|\\mathbf{y} - \\mathbf{x}\\|_2^2 + \\alpha \\mathbf{x}^\\intercal \\mathbf{L} \\mathbf{x},$$\n",
127+
"where $y$ is the observed signal, $\\alpha$ is an hyper-parameter which controls the trade-off between the data fidelity term and the smoothness prior."
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"metadata": {},
134+
"outputs": [],
135+
"source": [
136+
"# Your code here."
137+
]
138+
}
139+
],
140+
"metadata": {
141+
"kernelspec": {
142+
"display_name": "Python 3",
143+
"language": "python",
144+
"name": "python3"
145+
},
146+
"language_info": {
147+
"codemirror_mode": {
148+
"name": "ipython",
149+
"version": 3
150+
},
151+
"file_extension": ".py",
152+
"mimetype": "text/x-python",
153+
"name": "python",
154+
"nbconvert_exporter": "python",
155+
"pygments_lexer": "ipython3",
156+
"version": "3.7.0"
157+
}
158+
},
159+
"nbformat": 4,
160+
"nbformat_minor": 2
161+
}

0 commit comments

Comments
 (0)