In [3]:
import numpy as np
def dft(f):
import ia898.src as ia
f = np.asarray(f).astype(np.float64)
if (len(f.shape) == 1):
m = len(f)
A = ia.dftmatrix(f.shape[0])
F = np.sqrt(m) * A.dot(f)
elif (len(f.shape) == 2):
(m, n) = f.shape
A = ia.dftmatrix(m)
B = ia.dftmatrix(n)
F = np.sqrt(m * n) * (A.dot(f)).dot(B)
else:
(p,m,n) = f.shape
A = ia.dftmatrix(m)
B = ia.dftmatrix(n)
C = ia.dftmatrix(p)
Faux = A.dot(f)
Faux = Faux.dot(B)
F = np.sqrt(p)*np.sqrt(m)*np.sqrt(n)*C.dot(Faux)
return F
In [3]:
testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python dft.ipynb
import numpy as np
import sys,os
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
In [4]:
if testing:
f = np.arange(24).reshape(2,3,4) # original image with generic axis
F = ia.dft(f) # proposed dft
F1 = np.fft.fftn(f) # numpy dft
print('ia.dft:','\n',F.round(2),'\n')
print('fft.fftn:','\n',F1.round(2),'\n')
print('Equal Results? (max error)',abs(F1-F).max())
In [5]:
if testing:
f = ia.circle([256,256], 10, [129,129])
ia.adshow(f)
F = ia.dft(f)
Fv = ia.dftview(F)
ia.adshow(Fv)
In [6]:
if False: #testing:
f = np.zeros((25,30,40))
f[10:15,20:26,21:27] = 1
F = ia.dft(f)
ia.adshow(ia.normalize(ia.mosaic(f,5)),'Original Image')
ia.adshow(ia.mosaic(ia.dftview(F),5),'Fourier Transformation')
In [7]:
if testing:
import matplotlib.image as mpimg
f = mpimg.imread('../data/cameraman.tif')
%time F1 = ia.dft(f)
%time F2 = np.fft.fft2(f)
print('Max difference is:', np.abs(F1-F2).max())
In [ ]: