2
# Created by Pearu Peterson, September 2002
3
""" Test functions for fftpack.pseudo_diffs module
7
python setup_fftpack.py build
8
Run tests if scipy is installed:
9
python -c 'import scipy;scipy.fftpack.test(<level>)'
10
Run tests if fftpack is not installed:
11
python tests/test_pseudo_diffs.py [<level>]
14
from numpy.testing import *
16
from fftpack import diff,fft,ifft,tilbert,itilbert,hilbert,ihilbert,rfft
17
from fftpack import shift
18
from fftpack import fftfreq
21
from numpy import arange, add, array, sin, cos, pi,exp,tanh,sum,sign
26
def direct_diff(x,k=1,period=None):
31
w = fftfreq(n)*2j*pi/period*n
39
return ifft(w*fx).real
41
def direct_tilbert(x,h=1,period=None):
46
w = fftfreq(n)*h*2*pi/period*n
52
def direct_itilbert(x,h=1,period=None):
57
w = fftfreq(n)*h*2*pi/period*n
61
def direct_hilbert(x):
68
def direct_ihilbert(x):
69
return -direct_hilbert(x)
71
def direct_shift(x,a,period=None):
76
k = fftfreq(n)*2j*pi/period*n
77
return ifft(fft(x)*exp(k*a)).real
80
class test_diff(ScipyTestCase):
82
def check_definition(self):
83
for n in [16,17,64,127,32]:
85
assert_array_almost_equal(diff(sin(x)),direct_diff(sin(x)))
86
assert_array_almost_equal(diff(sin(x),2),direct_diff(sin(x),2))
87
assert_array_almost_equal(diff(sin(x),3),direct_diff(sin(x),3))
88
assert_array_almost_equal(diff(sin(x),4),direct_diff(sin(x),4))
89
assert_array_almost_equal(diff(sin(x),5),direct_diff(sin(x),5))
90
assert_array_almost_equal(diff(sin(2*x),3),direct_diff(sin(2*x),3))
91
assert_array_almost_equal(diff(sin(2*x),4),direct_diff(sin(2*x),4))
92
assert_array_almost_equal(diff(cos(x)),direct_diff(cos(x)))
93
assert_array_almost_equal(diff(cos(x),2),direct_diff(cos(x),2))
94
assert_array_almost_equal(diff(cos(x),3),direct_diff(cos(x),3))
95
assert_array_almost_equal(diff(cos(x),4),direct_diff(cos(x),4))
96
assert_array_almost_equal(diff(cos(2*x)),direct_diff(cos(2*x)))
97
assert_array_almost_equal(diff(sin(x*n/8)),direct_diff(sin(x*n/8)))
98
assert_array_almost_equal(diff(cos(x*n/8)),direct_diff(cos(x*n/8)))
100
assert_array_almost_equal(diff(sin(4*x),k),direct_diff(sin(4*x),k))
101
assert_array_almost_equal(diff(cos(4*x),k),direct_diff(cos(4*x),k))
103
def check_period(self):
105
x = arange(n)/float(n)
106
assert_array_almost_equal(diff(sin(2*pi*x),period=1),
108
assert_array_almost_equal(diff(sin(2*pi*x),3,period=1),
109
-(2*pi)**3*cos(2*pi*x))
114
assert_array_almost_equal(diff(sin(x)),cos(x))
115
assert_array_almost_equal(diff(cos(x)),-sin(x))
116
assert_array_almost_equal(diff(sin(x),2),-sin(x))
117
assert_array_almost_equal(diff(sin(x),4),sin(x))
118
assert_array_almost_equal(diff(sin(4*x)),4*cos(4*x))
119
assert_array_almost_equal(diff(sin(sin(x))),cos(x)*cos(sin(x)))
121
def check_expr(self):
122
for n in [64,77,100,128,256,512,1024,2048,4096,8192][:5]:
124
f=sin(x)*cos(4*x)+exp(sin(3*x))
125
df=cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x))
126
ddf=-17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\
127
-9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x))
129
assert_array_almost_equal(d1,df)
130
assert_array_almost_equal(diff(df),ddf)
131
assert_array_almost_equal(diff(f,2),ddf)
132
assert_array_almost_equal(diff(ddf,-1),df)
133
#print max(abs(d1-df))
135
def check_expr_large(self):
136
for n in [2048,4096]:
138
f=sin(x)*cos(4*x)+exp(sin(3*x))
139
df=cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x))
140
ddf=-17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\
141
-9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x))
142
assert_array_almost_equal(diff(f),df)
143
assert_array_almost_equal(diff(df),ddf)
144
assert_array_almost_equal(diff(ddf,-1),df)
145
assert_array_almost_equal(diff(f,2),ddf)
150
assert_array_almost_equal(diff(sin(x),-1),-cos(x))
151
assert_array_almost_equal(diff(sin(x),-2),-sin(x))
152
assert_array_almost_equal(diff(sin(x),-4),sin(x))
153
assert_array_almost_equal(diff(2*cos(2*x),-1),sin(2*x))
155
def check_random_even(self):
157
for n in [60,32,64,56,55]:
161
# zeroing Nyquist mode:
162
f = diff(diff(f,1),-1)
163
assert_almost_equal(sum(f),0.0)
164
assert_array_almost_equal(diff(diff(f,k),-k),f)
165
assert_array_almost_equal(diff(diff(f,-k),k),f)
167
def check_random_odd(self):
168
for k in [0,1,2,3,4,5,6]:
173
assert_almost_equal(sum(f),0.0)
174
assert_array_almost_equal(diff(diff(f,k),-k),f)
175
assert_array_almost_equal(diff(diff(f,-k),k),f)
177
def check_zero_nyquist (self):
178
for k in [0,1,2,3,4,5,6]:
179
for n in [32,33,64,56,55]:
183
# zeroing Nyquist mode:
184
f = diff(diff(f,1),-1)
185
assert_almost_equal(sum(f),0.0)
186
assert_array_almost_equal(diff(diff(f,k),-k),f)
187
assert_array_almost_equal(diff(diff(f,-k),k),f)
189
def bench_random(self,level=5):
191
print 'Differentiation of periodic functions'
192
print '====================================='
193
print ' size | convolve | naive'
194
print '-------------------------------------'
195
for size,repeat in [(100,1500),(1000,300),
205
x = arange (size)*2*pi/size
207
f = sin(x)*cos(4*x)+exp(sin(3*x))
210
assert_array_almost_equal(diff(f,1),direct_diff(f,1))
211
assert_array_almost_equal(diff(f,2),direct_diff(f,2))
212
print '| %9.2f' % self.measure('diff(f,3)',repeat),
214
print '| %9.2f' % self.measure('direct_diff(f,3)',repeat),
216
print ' (secs for %s calls)' % (repeat)
219
class test_tilbert(ScipyTestCase):
221
def check_definition(self):
222
for h in [0.1,0.5,1,5.5,10]:
223
for n in [16,17,64,127]:
225
y = tilbert(sin(x),h)
226
y1 = direct_tilbert(sin(x),h)
227
assert_array_almost_equal (y,y1)
228
assert_array_almost_equal(tilbert(sin(x),h),
229
direct_tilbert(sin(x),h))
230
assert_array_almost_equal(tilbert(sin(2*x),h),
231
direct_tilbert(sin(2*x),h))
233
def check_random_even(self):
234
for h in [0.1,0.5,1,5.5,10]:
239
assert_almost_equal(sum(f),0.0)
240
assert_array_almost_equal(direct_tilbert(direct_itilbert(f,h),h),f)
242
def check_random_odd(self):
243
for h in [0.1,0.5,1,5.5,10]:
248
assert_almost_equal(sum(f),0.0)
249
assert_array_almost_equal(itilbert(tilbert(f,h),h),f)
250
assert_array_almost_equal(tilbert(itilbert(f,h),h),f)
252
def bench_random(self,level=5):
254
print ' Tilbert transform of periodic functions'
255
print '========================================='
256
print ' size | optimized | naive'
257
print '-----------------------------------------'
258
for size,repeat in [(100,1500),(1000,300),
268
x = arange (size)*2*pi/size
270
f = sin(x)*cos(4*x)+exp(sin(3*x))
273
assert_array_almost_equal(tilbert(f,1),direct_tilbert(f,1))
274
print '| %9.2f' % self.measure('tilbert(f,1)',repeat),
276
print '| %9.2f' % self.measure('direct_tilbert(f,1)',repeat),
278
print ' (secs for %s calls)' % (repeat)
280
class test_itilbert(ScipyTestCase):
282
def check_definition(self):
283
for h in [0.1,0.5,1,5.5,10]:
284
for n in [16,17,64,127]:
286
y = itilbert(sin(x),h)
287
y1 = direct_itilbert(sin(x),h)
288
assert_array_almost_equal (y,y1)
289
assert_array_almost_equal(itilbert(sin(x),h),
290
direct_itilbert(sin(x),h))
291
assert_array_almost_equal(itilbert(sin(2*x),h),
292
direct_itilbert(sin(2*x),h))
294
class test_hilbert(ScipyTestCase):
296
def check_definition(self):
297
for n in [16,17,64,127]:
300
y1 = direct_hilbert(sin(x))
301
assert_array_almost_equal (y,y1)
302
assert_array_almost_equal(hilbert(sin(2*x)),
303
direct_hilbert(sin(2*x)))
305
def check_tilbert_relation(self):
306
for n in [16,17,64,127]:
308
f = sin (x)+cos (2*x)*sin(x)
310
y1 = direct_hilbert(f)
311
assert_array_almost_equal (y,y1)
313
assert_array_almost_equal (y,y2)
315
def check_random_odd(self):
320
assert_almost_equal(sum(f),0.0)
321
assert_array_almost_equal(ihilbert(hilbert(f)),f)
322
assert_array_almost_equal(hilbert(ihilbert(f)),f)
324
def check_random_even(self):
329
# zeroing Nyquist mode:
330
f = diff(diff(f,1),-1)
331
assert_almost_equal(sum(f),0.0)
332
assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)),f)
333
assert_array_almost_equal(hilbert(ihilbert(f)),f)
335
def bench_random(self,level=5):
337
print ' Hilbert transform of periodic functions'
338
print '========================================='
339
print ' size | optimized | naive'
340
print '-----------------------------------------'
341
for size,repeat in [(100,1500),(1000,300),
351
x = arange (size)*2*pi/size
353
f = sin(x)*cos(4*x)+exp(sin(3*x))
356
assert_array_almost_equal(hilbert(f),direct_hilbert(f))
357
print '| %9.2f' % self.measure('hilbert(f)',repeat),
359
print '| %9.2f' % self.measure('direct_hilbert(f)',repeat),
361
print ' (secs for %s calls)' % (repeat)
363
class test_ihilbert(ScipyTestCase):
365
def check_definition(self):
366
for n in [16,17,64,127]:
369
y1 = direct_ihilbert(sin(x))
370
assert_array_almost_equal (y,y1)
371
assert_array_almost_equal(ihilbert(sin(2*x)),
372
direct_ihilbert(sin(2*x)))
374
def check_itilbert_relation(self):
375
for n in [16,17,64,127]:
377
f = sin (x)+cos (2*x)*sin(x)
379
y1 = direct_ihilbert(f)
380
assert_array_almost_equal (y,y1)
381
y2 = itilbert(f,h=10)
382
assert_array_almost_equal (y,y2)
384
class test_shift(ScipyTestCase):
386
def check_definition(self):
387
for n in [18,17,64,127,32,2048,256]:
390
assert_array_almost_equal(shift(sin(x),a),direct_shift(sin(x),a))
391
assert_array_almost_equal(shift(sin(x),a),sin(x+a))
392
assert_array_almost_equal(shift(cos(x),a),cos(x+a))
393
assert_array_almost_equal(shift(cos(2*x)+sin(x),a),
394
cos(2*(x+a))+sin(x+a))
395
assert_array_almost_equal(shift(exp(sin(x)),a),exp(sin(x+a)))
396
assert_array_almost_equal(shift(sin(x),2*pi),sin(x))
397
assert_array_almost_equal(shift(sin(x),pi),-sin(x))
398
assert_array_almost_equal(shift(sin(x),pi/2),cos(x))
400
def bench_random(self,level=5):
402
print ' Shifting periodic functions'
403
print '=============================='
404
print ' size | optimized | naive'
405
print '------------------------------'
406
for size,repeat in [(100,1500),(1000,300),
416
x = arange (size)*2*pi/size
419
f = sin(x)*cos(4*x)+exp(sin(3*x))
420
sf = sin(x+a)*cos(4*(x+a))+exp(sin(3*(x+a)))
423
sf = sin(x+a)*cos(4*(x+a))
424
assert_array_almost_equal(direct_shift(f,1),sf)
425
assert_array_almost_equal(shift(f,1),sf)
426
print '| %9.2f' % self.measure('shift(f,a)',repeat),
428
print '| %9.2f' % self.measure('direct_shift(f,a)',repeat),
430
print ' (secs for %s calls)' % (repeat)
432
if __name__ == "__main__":
433
ScipyTest('fftpack.pseudo_diffs').run()