Demo iaconvteo

Illustrate the discrete convolution theorem.

Description

In this demonstration, the convolution theorem is illustrated by comparing the result of the convolution with its equivalent filtering in frequency domain. The convolution used is the periodic discrete convolution with kernel origin at the array origin.

Script

Reading the image

The image is read and displayed. It is important that the image is in the float format.

1 import numpy as np
2 import ia636 as ia
3 
4 f = adreadgray('lenina.pgm').astype(np.float)
5 adshow(f,'f')

f

Convolution with the Laplacian kernel

The image is convolved (periodicaly) with the 3x3 diagonal gradient:

1 h = array([[-2,-1,0],
2            [-1, 0,1],
3            [ 0, 1,2]])
4 g = ia.iapconv2(f,h)
5 adshow(ia.ianormalize(g),'g: filtered image (spatial domain)')

g: filtered image (spatial domain)

Equivalent filter in frequency domain

The 3x3 kernel is zero padded to the size of the input image. This is important as both image and kernel are periodic with same period of the image.

1 hx = np.zeros_like(f)
2 hx[:h.shape[0],:h.shape[1]] = h
3 print 'hx:\n',hx
hx:
[[-2. -1.  0. ...,  0.  0.  0.]
 [-1.  0.  1. ...,  0.  0.  0.]
 [ 0.  1.  2. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]

The spectrum of the kernel is computed and visualized.

1 HX = ia.iadft(hx);
2 adshow(ia.iadftview(HX),'HX: Spectrum of kernel hx')

HX: Spectrum of kernel hx

Filtering in the frequency domain

The image is filtered by multiplying its DFT by the frequency mask computed in the previous step.

1 F = ia.iadft(f)
2 G = F * HX
3 print "Is symmetrical:", ia.iaisdftsym(G)
4 adshow(ia.iadftview(G),'Spectrum of filtered image')
5 g_aux = ia.iaidft(G).real
6 adshow(ia.ianormalize(g_aux),'g_aux: Filtered image (frequency domain)')
Is symmetrical: True

Spectrum of filtered image

g_aux: Filtered image (frequency domain)

Comparing the results

Both images, filtered by the convolution and filtered in the frequency domain are compared to see that they are the same. The small differencies are due to numerical precision errors.

1 e = np.abs(g - g_aux)
2 print "Max error:", e.max()
Max error: 1.40062184073e-09

Applications of the convolution theorem

This theorem is very important as many conclusions are possible:

  • It is possible to interpret and visualize the frequency response of any convolution kernel (low pass, high pass, band pass)
  • Any convolution filter can be implemented in the frequency domain
  • Any frequency filter can be implemented in the spatial domain

See Also