Shifts zero-frequency component to center of spectrum.
The origin (0,0) of the DFT is normally at top-left corner of the image. For visualization
purposes, it is common to periodically translate the origin to the image center. This is
particularlly interesting because of the complex conjugate simmetry of the DFT of a real function.
Note that as the image can have even or odd sizes, to translate back the DFT from the center to
the corner, there is another correspondent function: idftshift
.
In [1]:
import numpy as np
def dftshift(f):
import ia898.src as ia
return ia.ptrans(f, np.array(f.shape)//2)
In [1]:
testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python dftshift.ipynb
import numpy as np
import sys,os
import matplotlib.image as mpimg
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
In [6]:
if testing:
f = ia.circle([120,150],6,[60,75])
F = ia.dft(f)
Fs = ia.dftshift(F)
ia.adshow(ia.dftview(F))
ia.adshow(ia.dftview(Fs))
In [7]:
if testing:
F = np.array([[10+6j,20+5j,30+4j],
[40+3j,50+2j,60+1j]])
Fs = ia.dftshift(F)
print('Fs=\n',Fs)
In [8]:
if testing:
print('testing dftshift')
print(repr(ia.dftshift(np.array([[10+6j,20+5j,30+4j],
[40+3j,50+2j,60+1j]]))) ==
repr(np.array([[ 60.+1.j, 40.+3.j, 50.+2.j],
[ 30.+4.j, 10.+6.j, 20.+5.j]])))
In [ ]: