Skip to content

Commit cbb85e1

Browse files
committed
doc: examples of filter approximations
1 parent d859915 commit cbb85e1

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

pygsp/filters/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Filter.synthesize
2121
Filter.compute_frame
2222
Filter.estimate_frame_bounds
23+
Filter.approximate
2324
Filter.plot
2425
Filter.localize
2526

pygsp/filters/approximations.py

+20
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ class Chebyshev(Filter):
3838
order : int
3939
Polynomial order.
4040
41+
Examples
42+
--------
43+
44+
Plot the basis formed by the first K Chebyshev polynomials:
45+
46+
>>> import matplotlib.pyplot as plt
47+
>>> fig, ax = plt.subplots(1, 1)
48+
>>>
49+
>>> G = graphs.Ring()
50+
>>> G.estimate_lmax()
51+
>>>
52+
>>> K = 5 # Polynomials of order up to K.
53+
>>>
54+
>>> coefficients = np.identity(K)
55+
>>> f = filters.Chebyshev(G, coefficients)
56+
>>> f.plot(sum=False, eigenvalues=False, ax=ax)
57+
>>>
58+
>>> _ = ax.set_title('Chebysev polynomials')
59+
>>> _ = ax.legend(['order {}'.format(order) for order in range(K)])
60+
4161
"""
4262

4363
def __init__(self, G, coefficients):

pygsp/filters/filter.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _evaluate(self, x, _):
100100
return y
101101

102102
def approximate(self, method, **kwargs):
103-
r"""Returns a filter which approximates this filter.
103+
r"""Return a filter which approximates this filter.
104104
105105
While approximations might loose accuracy, they allow for much faster
106106
computations.
@@ -114,7 +114,33 @@ def approximate(self, method, **kwargs):
114114
115115
Examples
116116
--------
117-
TODO: approx plot from notebook (needs new plotting)
117+
118+
Approximate a filter with Chebyshev polynomials of various orders:
119+
120+
>>> import matplotlib.pyplot as plt
121+
>>> fig, ax = plt.subplots(1, 1)
122+
>>>
123+
>>> G = graphs.Ring()
124+
>>> G.compute_fourier_basis()
125+
>>> f1 = filters.Heat(G)
126+
>>> f1.plot(eigenvalues=True, linewidth=3, label='continuous', ax=ax)
127+
>>>
128+
>>> for order in range(1, 5):
129+
... f2 = f1.approximate('Chebyshev', order=order)
130+
... l = 'Chebyshev order {}'.format(order)
131+
... f2.plot(eigenvalues=False, label=l, linestyle='dashed', ax=ax)
132+
>>>
133+
>>> _ = ax.set_title('Approximation for various polynomial orders')
134+
>>> _ = ax.legend()
135+
136+
Approximate a filterbank with Chebyshev polynomials:
137+
138+
>>> G = graphs.Ring()
139+
>>> G.compute_fourier_basis()
140+
>>> f1 = filters.Itersine(G)
141+
>>> f2 = f1.approximate('Chebyshev', order=20)
142+
>>> f1.plot(title='Continuous filterbank')
143+
>>> f2.plot(title='Approximated filterbank')
118144
119145
"""
120146
from . import approximations
@@ -173,7 +199,7 @@ def filter(self, s, method=None, order=30):
173199
s : ndarray
174200
Graph signals, a tensor of shape ``(N_NODES, N_SIGNALS,
175201
N_FEATURES)``, where ``N_NODES`` and ``N_SIGNALS`` are the number
176-
of nodes and signals of the signal tensor that pas passed in, and
202+
of nodes and signals of the signal tensor that was passed in, and
177203
``N_FEATURES`` is either 1 (synthesis) or the number of filters in
178204
the filter bank (analysis).
179205

0 commit comments

Comments
 (0)