In [24]:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from skimage import io,color,data,filters,exposure,util
In [23]:
#复指数函数
M=8 # total number of point
u=1 # current frequency
r=8 # current "time"
c=(np.exp(-2j*np.pi*u/M*np.array(range(r))))
plt.plot(np.real(c),np.imag(c),'.r')
#plt.plot(np.imag(c),'.r')
r=3 # current "time"
c1=(np.exp(-2j*np.pi*u/M*np.array(range(r))))
plt.plot(np.real(c1),np.imag(c1),'dg')
#plt.plot(np.imag(c1),'dg')
Out[23]:
In [75]:
%matplotlib notebook
fig, ax = plt.subplots()
def init():
return
def update(frame):
ax.clear()
M=32 # total number of point
u=1 # current frequency
r=32 # current "time"
c=(np.exp(-2j*np.pi*u/M*np.array(range(r))))
ax.plot(np.real(c),np.imag(c),'.r')
#plt.plot(np.imag(c),'.r')
u=1
r=frame # current "time"
#c1=(np.exp(-2j*np.pi*u/M*np.array(range(r+1))))
c1=(np.exp(-2j*np.pi*u/M*np.array(r)))
ax.plot(np.real(c1),np.imag(c1),'dg')
#ax.plot(range(frame),np.real(c1),'-dg')
ani = FuncAnimation(fig, update, frames=range(32),
init_func=init, blit=True)
In [13]:
# 离散Fourier变换
t=np.array([1,1,1,1,0,0,0,0])
f=np.fft.fft(t)
#plt.bar(range(len(t)),t)
#plt.bar(range(len(t)),np.abs(f))
#plt.bar(range(len(t)),np.angle(f))
#plt.stem(range(len(t)),np.angle(f))
plt.stem(range(len(t)),np.abs(f))
Out[13]:
In [27]:
# F(u)=F*(-u)
f1=f.copy()
f1[1:]=f[1:][::-1]
x=np.conj(f1)
np.abs(x-f).sum()
Out[27]:
In [4]:
# f(t-tau)---> F(u)exp(-j2 pi u tau / M)
t=np.array([1,1,1,1,0,0,0,0])
t1=np.roll(t,-2)
f=np.fft.fft(t)
f1=np.fft.fft(t1)
f2=np.exp(1j*2*np.pi*np.array(range(8))/8*2)*f
np.abs(f1-f2).sum()
Out[4]:
In [18]:
# F(f(t)*g(t))=F(u)G(u)
t1=np.float64(np.array(range(8))<4)
t2=np.array([1,1,1])
np.abs(np.fft.ifft(np.fft.fft(t1)*np.fft.fft(t2,8)))
np.convolve(t1,t2)
Out[18]:
In [34]:
#t=np.random.rand(32)*2-1
t=np.sin(range(32))
p=np.abs(np.array([1,-1,1,-1,1,-1,1,-1]))
t[6:6+len(p)]=p.copy()
q1=np.convolve(t,p[::-1])
#plt.plot(np.abs(q1),'.b')
f1=np.fft.fft(t)
f2=np.fft.fft(p,32)
q2=np.fft.ifft(f1/(0.001+np.abs(f1))*f2/(0.001+np.abs(f2)))
#q2=np.fft.ifft(f1/(0.00001+f2))
plt.plot(np.abs(q2),'.r')
Out[34]:
In [30]:
t=np.random.rand(256)-0.5
p=np.random.rand(128)-0.5
t[16:16+len(p)]=p.copy()
q1=np.convolve(t,p[::-1])
f1=np.fft.fft(t)
f2=np.fft.fft(p,len(t))
#q2=np.fft.ifft(f1/(0.001+np.abs(f1))*f2/(0.001+np.abs(f2)))
q2=np.fft.ifft(f1/(0.00001+f2))
plt.plot(np.abs(q2),'.')
np.uint8(q2[1:20]>0.7)
Out[30]:
In [18]:
im=data.coins()
imf=np.fft.fft2(im)
a=50 # noise frequency
m=50 # noise amplitude
imf[0,a]=imf[0,a]*m
imf[a,0]=imf[a,0]*m
imf[a,a]=imf[a,a]*m
imf[imf.shape[0]-a,imf.shape[1]-a]=imf[imf.shape[0]-a,imf.shape[1]-a]*m
im1=np.uint8(np.abs(np.fft.ifft2(imf)))
#plt.plot((im1[:,100]))
im2=im.copy()
im2[:,100]=255;
plt.imshow(im2)
print()
In [23]:
m=0
imf[0,a]=imf[0,a]*m
imf[a,0]=imf[a,0]*m
imf[a,a]=imf[a,a]*m
imf[imf.shape[0]-a,imf.shape[1]-a]=imf[imf.shape[0]-a,imf.shape[1]-a]*m
im2=np.uint8(np.abs(np.fft.ifft2(imf)))
#plt.imshow(im2)
plt.plot(im1[:,100])
plt.plot(im2[:,100])
Out[23]:
In [26]:
im=data.coins()
imf=np.fft.fft2(im)
amp=np.abs(imf)
phase=np.angle(imf)
onlyample=np.uint8(np.abs(np.fft.ifft2(amp)))
onlyphase=np.uint8(np.mean(amp)*np.abs(np.fft.ifft2(np.exp(1j*phase))))
#plt.imshow(onlyample)
plt.imshow(onlyphase)
Out[26]:
In [31]:
w,h=imf.shape
w2=np.uint8(w/2)
h2=np.uint8(h/2)
imf1=np.vstack((imf[w2:,:],imf[:w2,:]))
imf2=np.hstack((imf1[:,h2:],imf1[:,:h2]))
plt.imshow(np.log(np.abs(imf2)))
Out[31]:
In [37]:
a=np.zeros((500,500),dtype=np.uint8)
a[245:255,245:255]=255
f=np.fft.ifft2(a)
f[10:490,:]=0
f[:,10:490]=0
a1=np.abs(np.fft.ifft2(f))
a1=np.uint8(a1*255/np.max(a1))
plt.imshow(a1)
#plt.plot(a[:,250])
Out[37]: