1.调用keikna库
keikna库中有fft,所以为了减小任务量我就调用keikan的fft的库来完成。
http://blog.sina.com.cn/s/blog_4513dde60102vstu.html
这个帖子里详细的介绍了keikna的一些东西。
2.介绍FFT和IFFT的实现
对于二维的傅里叶变换的实现,冈萨雷斯《数字图像处理》有详细的介绍。
通过对这两个图片的学习,可以开始写代码了。
3.代码实现
下面我就把代码贴上:
输出的结果为:
import numpy
from reikna.fft import FFT
import reikna.cluda as cluda
api = cluda.cuda_api()
thr = api.Thread.create()
x = numpy.array((
[[1,1,1,0],
[0,1,1,1],
[0,0,1,1],
[0,0,1,1]
]), dtype=numpy.complex128)
R = x.shape[0]
L = x.shape[-1]
#print (x.real)
x=x.flatten()
NX = numpy.full((16,),0).astype(numpy.complex128)
for i in range(0,16):
NX[i]=x[i]
NX = NX.reshape(4,4)
x = thr.to_device(NX)
X = thr.array((R,L), dtype=numpy.complex128)
fft = FFT(x)
fftc = fft.compile(thr)
fftc(X, x, 0)
xfft = X.get()
print (xfft)
aa = xfft.conjugate()
#print (aa)
xx = thr.to_device(aa)
fft = FFT(xx)
fftc = fft.compile(thr)
fftc(X, xx, 0)
xifft = X.get()
xifft = xifft/(R*L)
thr.release()
print (xifft.real)