# Determining Type and Bandwidth of a filter

Given a filter, if it is given as an equation such as:

$$f(x,y) = \left(\frac{\partial^2}{\partial x^2} + \frac{\partial^2}{\partial y^2}\right) \exp\left(-\frac{x^2 + y^2}{\sigma^2}\right)$$

Or in a kernel such as: $$\left[\begin{array}{rrr} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{array}\right]$$ How much can I find out about the filter. Specifically can I find out if it is a highpass/ lowpass/ bandpass filter (if so how?), and can I find out the bandwidth of the filter?

If it helps, the context is image processing.

Thanks

-

## migrated from math.stackexchange.comFeb 11 '13 at 16:40

This question came from our site for people studying math at any level and professionals in related fields.

To filter an image using a kernel, you use convolution, so the kernel is a sort of impulse response function. To see the frequency response of a filter, you perform an FFT of the impulse response. In this case, your kernel is only 3x3, so I think you would pad it with zeros first to see a higher-resolution response spectrum. Then you can look at/measure the spectrum to see the type and bandwidth, etc. –  endolith Feb 11 '13 at 18:20

As others have mentioned, performing a 2D FFT on the kernel will give you the frequency response of the filter. However, it's worth mentioning that 2D filters can be analyzed using the Z-transform, which may or may not provide deeper insight, depending on the filter (and what you want to know).

For example, given the kernel you specified, the corresponding difference equation would be

$$y(n_1,n_2) = x(n_1+1,n_2) + x(n_1,n_2 + 1) + x(n_1-1,n_2) + x(n_1,n_2-1) - 4x(n_1,n_2).$$

Its Z-transform is

$$Y(z_1,z_2) = z_1X(z_1,z_2) + z_2X(z_1,z_2) + z^{-1}_1X(z_1,z_2) + z_2^{-1}X(z_1,z_2) - 4X(z_1,z_2),$$

which, after rearranging, yields the following transfer function for the filter:

$$H(z_1,z_2) = \frac{Y(z_1,z_2)}{X(z_1,z_2)} = z_1 + z_2 + z_1^{-1} + z_2^{-1} - 4.$$

To determine the magnitude response, just plug in a pair of complex exponentials and simplify, as follows:

\begin{aligned} H(e^{iw_1},e^{iw_2}) & = e^{iw_1} + e^{iw_2} + e^{-iw_1} + e^{-iw_2} - 4 \\ & = (e^{iw_1} + e^{-iw_1}) + (e^{iw_2} + e^{-iw_2}) - 4 \\ & = 2\cos{w_1} + 2\cos{w_2} - 4 \\ |H(e^{iw_1},e^{iw_2})| & = 2\sqrt{ (\cos{w_1} + \cos{w_2} - 2)^2 } . \end{aligned}

Evaluating the magnitude response at extreme frequencies will give you a sense of highpass vs. lowpass for the filter. For example,

$$|H(e^{i0},e^{i0})| = 2\sqrt{(\cos{0} + \cos{0} - 2)^2} = 2\sqrt{(1+1-2)^2} = 0,$$ and $$|H(e^{i\pi},e^{i\pi})| = 2\sqrt{(\cos{\pi} + \cos{\pi} - 2)^2} = 2\sqrt{(-1-1-2)^2} = 8.$$

Of course, the transfer function can be evaluated directly to plot the magnitude response of the filter as well. Here's an example using numpy:

import numpy as np
import pylab as py
from mpl_toolkits.mplot3d import axes3d

def H(z1,z2):
return z1 + z2 + 1./z1 + 1./z2 - 4.0

n = 100
w1 = w2 = np.linspace(-np.pi,np.pi, n)
mag = np.zeros((n,n))

for i1 in xrange(0,n):
for i2 in xrange(0,n):
z1 = np.exp(1j*w1[i1])
z2 = np.exp(1j*w2[i2])
mag[i1,i2] = np.abs(H(z1,z2))

fig = py.figure()
X, Y = np.meshgrid(w1,w2)
ax.plot_surface(X, Y, mag, cmap='bone', alpha=.5)
py.show()

Keep in mind that if you use the kernel 2D FFT technique, the resulting magnitudes won't necessarily be centered at zero like the above plot.

-

The negative of the second derivative of a gaussian kernel as you have described it turns out to be what is called the 'Mexican Hat'. kernel. You can see some of its uses in the wiki.

As it stands, such a filter can be used to detect edges. The kernel you have provided can also be used in the same capacity.

The best way to be able to tell if a filter is low/band/high pass is to observe it in the frequency domain, as that will tell you what bands are being suppressed, or left alone. Performing the 2-dimensional DFT, and taking the magnitude of the result should give you this information.

-

If you have the MATLAB Image Processing Toolbox, you could use freqz2: http://www.mathworks.com/help/images/ref/freqz2.html

Scipy code for a 2-d DFT/FFT as noted by @endolith and @Mohammad

h = array([[0,1,0],[1,-4,1],[0,1,0]])
N=32;
figure();
imshow(abs(fft2(h,s=(N,N))),interpolation='nearest');
colorbar()
-