Skip to content

Commit

Permalink
update expwin to be band-pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Jun 26, 2018
1 parent b63acc8 commit 3904030
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions doc/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Additional features:
* print(graph) and print(filters) now show valuable information.
* Building a graph object is much faster.
* New rectangular filter (low-pass and band-pass).
* The exponential window has been updated from low-pass only to band-pass.

0.5.1 (2017-12-15)
------------------
Expand Down
1 change: 1 addition & 0 deletions pygsp/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
.. autosummary::
Expwin
Rectangular
Approximations
Expand Down
43 changes: 30 additions & 13 deletions pygsp/filters/expwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class Expwin(Filter):
Parameters
----------
G : graph
band_min : float
Minimum relative band. The filter take the value 0.5 at this relative
frequency (default = None).
band_max : float
Maximum relative band (default = 0.2)
Maximum relative band. The filter take the value 0.5 at this relative
frequency (default = 0.2).
slope : float
Slope parameter (default = 1)
The slope at cut-off (default = 1).
Examples
--------
Expand All @@ -27,36 +31,49 @@ class Expwin(Filter):
>>> G = graphs.Ring(N=20)
>>> G.estimate_lmax()
>>> G.set_coordinates('line1D')
>>> g = filters.Expwin(G)
>>> g = filters.Expwin(G, band_min=0.1, band_max=0.5)
>>> s = g.localize(G.N // 2)
>>> fig, axes = plt.subplots(1, 2)
>>> g.plot(ax=axes[0])
>>> G.plot_signal(s, ax=axes[1])
"""

def __init__(self, G, band_max=0.2, slope=1):
def __init__(self, G, band_min=None, band_max=0.2, slope=1):

self.band_max = band_max
self.band_min = band_min
self.slope = slope

def fx(x):
def f(x):
# Canary to avoid division by zero and overflow.
y = np.where(x <= 0, -1, x)
y = np.exp(-slope / y)
return np.where(x <= 0, 0, y)

def gx(x):
y = fx(x)
return y / (y + fx(1 - x))
def h(x):
y = f(x)
z = f(1 - x)
return y / (y + z)

def ffin(x):
return gx(1 - x)
def g_highpass(x):
return h(0.5 - x/G.lmax + band_max)
def g_lowpass(x):
return h(0.5 + x/G.lmax - band_min)

g = [lambda x: ffin(x/band_max/G.lmax)]
if band_min is None:
g = [g_highpass]
elif band_max is None:
g = [g_lowpass]
else:
g = [lambda x: g_lowpass(x) * g_highpass(x)]

super(Expwin, self).__init__(G, g)

def _get_extra_repr(self):
return dict(band_max='{:.2f}'.format(self.band_max),
slope='{:.2f}'.format(self.slope))
attrs = dict(slope='{:.0f}'.format(self.slope))
if self.band_min is not None:
attrs.update(band_min='{:.2f}'.format(self.band_min))
if self.band_max is not None:
attrs.update(band_max='{:.2f}'.format(self.band_max))
return attrs
6 changes: 6 additions & 0 deletions pygsp/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ def test_heat(self):
def test_expwin(self):
f = filters.Expwin(self._G)
self._test_methods(f, tight=False)
f = filters.Expwin(self._G, band_min=None, band_max=0.8)
self._test_methods(f, tight=False)
f = filters.Expwin(self._G, band_min=0.1, band_max=None)
self._test_methods(f, tight=False)
f = filters.Expwin(self._G, band_min=0.1, band_max=0.7)
self._test_methods(f, tight=False)

def test_rectangular(self):
f = filters.Rectangular(self._G)
Expand Down

0 comments on commit 3904030

Please sign in to comment.