2
sys.path.insert(0,'../build/lib')
3
sys.path.insert(0,'../build/lib.linux-x86_64-2.6')
9
from numpy.fft import fft,ifft,fftshift
15
import fftw3l.planning
16
import fftw3f.planning
23
libs = [fftw3, fftw3f, fftw3l]
25
_complex = ['complex','singlecomplex','longcomplex']
26
_float = ['double','single','longdouble']
29
def fftw_propagation_aligned(N,repeats, lib, dtype):
30
t = np.linspace(-5,5,N)
32
f = np.linspace(-1/dt/2.,1/dt/2.,N)
35
farray = lib.create_aligned_array(f.shape,dtype=dtype)
36
tarray = lib.create_aligned_array(t.shape,dtype=dtype)
37
fftplan = lib.Plan(tarray,farray,'forward')
38
ifftplan = lib.Plan(farray,tarray,'backward')
41
tarray += np.exp(-t**2/0.5)
42
dispersion = np.exp(-1.j*h*beta*f)
44
for i in xrange(repeats):
46
farray *= dispersion/N
49
return fftshift(t),fftshift(tarray),fftshift(f),fftshift(farray), to
51
def fftw_propagation(N,repeats,lib, dtype):
52
t = np.linspace(-5,5,N)
54
f = np.linspace(-1/dt/2.,1/dt/2.,N)
57
farray = np.zeros(f.shape,dtype=dtype)
58
tarray = np.zeros(t.shape,dtype=dtype)
59
fftplan = lib.Plan(tarray,farray,'forward')
60
ifftplan = lib.Plan(farray,tarray,'backward')
63
tarray += np.exp(-t**2/0.5)
64
dispersion = np.exp(-1.j*h*beta*f)
66
for i in xrange(repeats):
68
farray *= dispersion/N
71
return fftshift(t),fftshift(tarray),fftshift(f),fftshift(farray), to
74
def np_propagation(N,repeats,dtype):
75
t = np.linspace(-5,5,N)
77
f = np.linspace(-1/dt/2.,1/dt/2.,N)
80
tarray = np.zeros(t.shape,dtype)
81
tarray += np.exp(-t**2/0.5)
82
farray = np.zeros(tarray.shape,dtype)
83
dispersion = np.zeros(tarray.shape,dtype)
84
dispersion += np.exp(-1.j*h*beta*f)
86
for i in xrange(repeats):
91
return fftshift(t),fftshift(tarray),fftshift(f),fftshift(farray),to
94
def scipy_propagation(N,repeats, dtype):
96
from scipy.fftpack import fft as sfft, ifft as sifft
98
return None, None, None, None, np.NaN
99
t = np.linspace(-5,5,N)
101
f = np.linspace(-1/dt/2.,1/dt/2.,N)
104
tarray = np.zeros(t.shape,dtype)
105
tarray += np.exp(-t**2/0.5)
106
farray = np.zeros(tarray.shape,dtype)
107
dispersion = np.zeros(tarray.shape,dtype)
108
dispersion += np.exp(-1.j*h*beta*f)
110
for i in xrange(repeats):
111
farray = sfft(tarray)
113
tarray = sifft(tarray)
115
return fftshift(t),fftshift(tarray),fftshift(f),fftshift(farray),to
118
class ProductTestCase(unittest.TestCase):
120
def testSelect(self):
122
for plan in lib.lib._typelist:
124
plantype,(intype, outtype,length) = plan
125
shape = np.random.randint(2,5,length)
126
inputa = np.zeros(shape=shape, dtype=intype)
127
outputa = np.zeros(shape=shape, dtype=outtype)
129
plantype, (intype,outtype) = plan
130
shape = np.random.randint(2,5,np.random.randint(4,8))
132
inputa = np.zeros(shape=shape, dtype=intype)
133
outputa = np.zeros(shape=shape, dtype=outtype)
134
func, name, types = lib.planning.select(inputa,outputa)
135
self.failUnless(name == plantype, "%s: select returned a "\
136
"wrong type for input array type=%s, output "\
137
"array type=%s, and dimension = %d" \
138
%(lib, inputa.dtype, outputa.dtype, length))
139
self.failUnless(func is getattr(lib.lib.lib, plantype), "%s: "\
140
"wrong library function for type %s"\
143
def testWisdom(self):
147
inputa = lib.create_aligned_array(1024,np.typeDict[_complex[i]])
148
outputa = lib.create_aligned_array(1024,np.typeDict[_complex[i]])
149
plan = lib.Plan(inputa,outputa,flags=['patient'])
150
soriginal = lib.export_wisdom_to_string()
151
lib.import_wisdom_from_string(soriginal)
152
lib.export_wisdom_to_file('test.wisdom')
154
lib.import_wisdom_from_file('test.wisdom')
155
os.remove('test.wisdom')
160
def testPropagation(self):
161
#can only test for fftw3 because longdouble and single are not both implemented for scipy.fft and numpy.fft
162
Ns = [2**i for i in range(10,15)]
167
t,A,f,B, ti = fftw_propagation(Nn,repeats, fftw3, _complex[0])
168
ft,fA,ff,fB, fti = fftw_propagation_aligned(Nn,repeats, fftw3, _complex[0])
169
nt,nA, nf, nB, nti = np_propagation(Nn,repeats, _complex[0])
170
st,sA, sf, sB, sti = scipy_propagation(Nn,repeats, _complex[0])
171
times.append((ti, fti,nti, sti))
172
self.failUnless(sum(abs(A)**2-abs(nA)**2)< epsilon, "Propagation "\
173
"of fftw3 and numpy gives "\
175
self.failUnless(sum(abs(fA)**2-abs(nA)**2)< epsilon, "Propagation "\
176
"of aligned fftw3 and numpy gives "\
178
print "Benchmark: %s" %fftw3
179
print "N fftw3 fftw3_aligned numpy scipy"
180
for i in range(len(Ns)):
181
print "%5d %5.2f %5.2f %5.2f %5.2f" %(Ns[i],\
189
from pylab import imread
192
im = imread('Fourier2.png')
194
a = np.zeros(im.shape, dtype=im.dtype)
196
b = np.zeros((im.shape[0],im.shape[1]/2+1),dtype=np.typeDict['singlecomplex'])
197
p = fftw3f.Plan(a,b,'forward')
198
ip = fftw3f.Plan(b,a,'backward')
202
self.failUnless(a.sum()-im.sum() < epsilon, "2D fft and ifft did not "\
203
"reproduce the same image")
207
if __name__ == '__main__': unittest.main()