iadft

Synopse

Discrete 1D/2D/3D Fourier Transform.

  • F = iadft(f)
    • F: Output Image.
    • f: Original Image.

Function Code

01. from numpy import *
02. 
03. def iadft(f):
04.     from ia636 import iadftmatrix
05.     f = asarray(f).astype(float64)
06.     if (len(f.shape) == 1):
07.         m = len(f)
08.         A = iadftmatrix(f.shape[0])
09.         F = sqrt(m) * dot(A, f)
10.     elif (len(f.shape) == 2):
11.         (m, n) = f.shape
12.         A = iadftmatrix(m)
13.         B = iadftmatrix(n)
14.         F = sqrt(m * n) * dot(dot(A, f), B)
15.     else:
16.         (p,m,n) = f.shape
17.         A = iadftmatrix(m)
18.         B = iadftmatrix(n)
19.         C = iadftmatrix(p)
20.         Faux = dot(A,f)
21.         Faux = dot(Faux,B)
22.         F = sqrt(p)*sqrt(m)*sqrt(n)*dot(C,Faux)
23.     return F

Examples

Numeric Example: comparing proposed with numpy function

01. from ia636 import *
02. from numpy import *
03. 
04. f = arange(24).reshape(2,3,4) # original image with generic axis
05. F = iadft(f)   # proposed dft
06. F1 = fft.fftn(f) # numpy dft
07. 
08. print 'ia636.iadft:','\n',F.round(2),'\n'
09. print 'fft.fftn:','\n',F1.round(2),'\n'
10. print 'Equal Results? (max error)',abs(F1-F).max()
ia636.iadft: 
[[[ 276. +0.j    -12.+12.j    -12. -0.j    -12.-12.j  ]
  [ -48.+27.71j    0. +0.j      0. +0.j     -0. +0.j  ]
  [ -48.-27.71j    0. +0.j     -0. +0.j     -0. +0.j  ]]

 [[-144. -0.j      0. +0.j     -0. +0.j     -0. +0.j  ]
  [   0. +0.j     -0. +0.j     -0. -0.j     -0. +0.j  ]
  [  -0. +0.j      0. -0.j     -0. -0.j     -0. -0.j  ]]] 

fft.fftn: 
[[[ 276. +0.j    -12.+12.j    -12. +0.j    -12.-12.j  ]
  [ -48.+27.71j    0. +0.j      0. +0.j      0. +0.j  ]
  [ -48.-27.71j    0. +0.j      0. +0.j      0. +0.j  ]]

 [[-144. +0.j      0. +0.j      0. +0.j      0. +0.j  ]
  [   0. +0.j      0. +0.j      0. +0.j      0. +0.j  ]
  [   0. +0.j      0. +0.j      0. +0.j      0. +0.j  ]]] 

Equal Results? (max error) 8.10143365796e-14

Image example: 2d circle

1. from ia636 import *
2. from numpy import *
3. 
4. f = 255 * iacircle([256,256], 10, [129,129])
5. adshow(f)
6. F = iadft(f)
7. Fv = iadftview(F)
8. adshow(Fv)

Image example: 3d square

1. from ia636 import *
2. 
3. f = zeros((25,30,40))
4. f[10:15,20:26,21:27] = 1
5. F = iadft(f)
6. adshow(ianormalize(iamosaic(f,5)),'Original Image')
7. adshow(iamosaic(iadftview(F),5),'Fourier Transformation')

Original Image

Fourier Transformation

Comparison with other implementations

01. import numpy as np
02. import time
03. import ia636 as ia
04. 
05. f = adread('cameraman.tif')
06. t = time.time()
07. F1 = ia.iadft(f)
08. t1 = time.time() - t
09. t = time.time()
10. F2 = np.fft.fft2(f)
11. t2 = time.time() - t
12. print 'Max difference is:', np.abs(F1-F2).max()
13. print 'iadft: %d ms' % (int(t1*1000),)
14. print 'fft2: %d ms' % (int(t2*1000),)
Max difference is: 1.80847080852e-07
iadft: 341 ms
fft2: 5 ms

Equation

1D transformation

2D transformation

3D transformation

See Also

  • iadftmatrix -- Kernel matrix for the DFT Transform.
  • iaidft -- Inverse Discrete Fourier Transform.
  • iadftview -- Discrete Fourier Transform Visualization.

Contributions

  • Mariana Pinheiro, 1st semester 2011