Skip to content

Commit

Permalink
doc: examples of filter approximations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Apr 2, 2018
1 parent d859915 commit cbb85e1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions pygsp/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Filter.synthesize
Filter.compute_frame
Filter.estimate_frame_bounds
Filter.approximate
Filter.plot
Filter.localize
Expand Down
20 changes: 20 additions & 0 deletions pygsp/filters/approximations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ class Chebyshev(Filter):
order : int
Polynomial order.
Examples
--------
Plot the basis formed by the first K Chebyshev polynomials:
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(1, 1)
>>>
>>> G = graphs.Ring()
>>> G.estimate_lmax()
>>>
>>> K = 5 # Polynomials of order up to K.
>>>
>>> coefficients = np.identity(K)
>>> f = filters.Chebyshev(G, coefficients)
>>> f.plot(sum=False, eigenvalues=False, ax=ax)
>>>
>>> _ = ax.set_title('Chebysev polynomials')
>>> _ = ax.legend(['order {}'.format(order) for order in range(K)])
"""

def __init__(self, G, coefficients):
Expand Down
32 changes: 29 additions & 3 deletions pygsp/filters/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _evaluate(self, x, _):
return y

def approximate(self, method, **kwargs):
r"""Returns a filter which approximates this filter.
r"""Return a filter which approximates this filter.
While approximations might loose accuracy, they allow for much faster
computations.
Expand All @@ -114,7 +114,33 @@ def approximate(self, method, **kwargs):
Examples
--------
TODO: approx plot from notebook (needs new plotting)
Approximate a filter with Chebyshev polynomials of various orders:
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(1, 1)
>>>
>>> G = graphs.Ring()
>>> G.compute_fourier_basis()
>>> f1 = filters.Heat(G)
>>> f1.plot(eigenvalues=True, linewidth=3, label='continuous', ax=ax)
>>>
>>> for order in range(1, 5):
... f2 = f1.approximate('Chebyshev', order=order)
... l = 'Chebyshev order {}'.format(order)
... f2.plot(eigenvalues=False, label=l, linestyle='dashed', ax=ax)
>>>
>>> _ = ax.set_title('Approximation for various polynomial orders')
>>> _ = ax.legend()
Approximate a filterbank with Chebyshev polynomials:
>>> G = graphs.Ring()
>>> G.compute_fourier_basis()
>>> f1 = filters.Itersine(G)
>>> f2 = f1.approximate('Chebyshev', order=20)
>>> f1.plot(title='Continuous filterbank')
>>> f2.plot(title='Approximated filterbank')
"""
from . import approximations
Expand Down Expand Up @@ -173,7 +199,7 @@ def filter(self, s, method=None, order=30):
s : ndarray
Graph signals, a tensor of shape ``(N_NODES, N_SIGNALS,
N_FEATURES)``, where ``N_NODES`` and ``N_SIGNALS`` are the number
of nodes and signals of the signal tensor that pas passed in, and
of nodes and signals of the signal tensor that was passed in, and
``N_FEATURES`` is either 1 (synthesis) or the number of filters in
the filter bank (analysis).
Expand Down

0 comments on commit cbb85e1

Please sign in to comment.