epfl-lts2 / pygsp

Graph Signal Processing in Python
https://pygsp.rtfd.io
BSD 3-Clause "New" or "Revised" License
483 stars 93 forks source link

Could you maybe explain how to create a high-pass filter? #84

Closed StefanBloemheuvel closed 4 years ago

StefanBloemheuvel commented 4 years ago

Hi,

I am not a Signal Processing expert, I am coming from a more Graph Theory background. I have the code to create a low-pass filter:

def g(x):
    return 1. / (1. + tau * x)

But I cannot find a way (or example) to create a high-pass version. How could I achieve that?

Thank you for making this amazing package, and kind regards,

Stefan

bricaud commented 4 years ago

Hi @StefanBloemheuvel , A filter as we define it is a function on the spectral domain. This spectral domain is a subset of the positive real numbers (the graph Laplacian eigenvalues). Usually a low-pass filter is a function with non-zero values on the region of low frequency and a decrease to zero for high frequency values. For the graph the "frequencies" are the eigenvalues of the Laplacian. For a high pass filter, the function should be zero for small values of the spectrum and increase with it. So g1(x)=x would give you a first high pass filter. Maybe a better one would be here g2(x) = tau x / (1 + tau x) as if we add your g with g2, it sums to one, so that it is perfectly complementary.

mdeff commented 4 years ago

Thanks for the kind words @StefanBloemheuvel. I'll add to @bricaud's answer that the latest dev version of the PyGSP can automatically create a complementary filter as g2 = g.complement().

Example:

import pygsp as pg
from matplotlib import pyplot as plt
graph = pg.graphs.Sensor(20)
graph.compute_fourier_basis()
tau = 2
g = pg.filters.Filter(graph, lambda x: 1 / (1 + tau * x))
g = g + g.complement()
fig, ax = plt.subplots(figsize=(10, 5))
g.plot(sum=False, ax=ax)
ax.legend(['low-pass', 'high-pass'], loc='center right')

filters

StefanBloemheuvel commented 4 years ago

Thank you very much for the explanation!