1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
#!/usr/bin/env python
# Created by Pearu Peterson, September 2002
""" Test functions for fftpack.pseudo_diffs module
"""
__usage__ = """
Build fftpack:
python setup_fftpack.py build
Run tests if scipy is installed:
python -c 'import scipy;scipy.fftpack.test(<level>)'
Run tests if fftpack is not installed:
python tests/test_pseudo_diffs.py [<level>]
"""
import sys
from numpy.testing import *
set_package_path()
from fftpack import diff,fft,ifft,tilbert,itilbert,hilbert,ihilbert,rfft
from fftpack import shift
from fftpack import fftfreq
restore_path()
from numpy import arange, add, array, sin, cos, pi,exp,tanh,sum,sign
def random(size):
return rand(*size)
def direct_diff(x,k=1,period=None):
fx = fft(x)
n = len (fx)
if period is None:
period = 2*pi
w = fftfreq(n)*2j*pi/period*n
if k<0:
w = 1 / w**k
w[0] = 0.0
else:
w = w**k
if n>2000:
w[250:n-250] = 0.0
return ifft(w*fx).real
def direct_tilbert(x,h=1,period=None):
fx = fft(x)
n = len (fx)
if period is None:
period = 2*pi
w = fftfreq(n)*h*2*pi/period*n
w[0] = 1
w = 1j/tanh(w)
w[0] = 0j
return ifft(w*fx)
def direct_itilbert(x,h=1,period=None):
fx = fft(x)
n = len (fx)
if period is None:
period = 2*pi
w = fftfreq(n)*h*2*pi/period*n
w = -1j*tanh(w)
return ifft(w*fx)
def direct_hilbert(x):
fx = fft(x)
n = len (fx)
w = fftfreq(n)*n
w = 1j*sign(w)
return ifft(w*fx)
def direct_ihilbert(x):
return -direct_hilbert(x)
def direct_shift(x,a,period=None):
n = len(x)
if period is None:
k = fftfreq(n)*1j*n
else:
k = fftfreq(n)*2j*pi/period*n
return ifft(fft(x)*exp(k*a)).real
class test_diff(ScipyTestCase):
def check_definition(self):
for n in [16,17,64,127,32]:
x = arange(n)*2*pi/n
assert_array_almost_equal(diff(sin(x)),direct_diff(sin(x)))
assert_array_almost_equal(diff(sin(x),2),direct_diff(sin(x),2))
assert_array_almost_equal(diff(sin(x),3),direct_diff(sin(x),3))
assert_array_almost_equal(diff(sin(x),4),direct_diff(sin(x),4))
assert_array_almost_equal(diff(sin(x),5),direct_diff(sin(x),5))
assert_array_almost_equal(diff(sin(2*x),3),direct_diff(sin(2*x),3))
assert_array_almost_equal(diff(sin(2*x),4),direct_diff(sin(2*x),4))
assert_array_almost_equal(diff(cos(x)),direct_diff(cos(x)))
assert_array_almost_equal(diff(cos(x),2),direct_diff(cos(x),2))
assert_array_almost_equal(diff(cos(x),3),direct_diff(cos(x),3))
assert_array_almost_equal(diff(cos(x),4),direct_diff(cos(x),4))
assert_array_almost_equal(diff(cos(2*x)),direct_diff(cos(2*x)))
assert_array_almost_equal(diff(sin(x*n/8)),direct_diff(sin(x*n/8)))
assert_array_almost_equal(diff(cos(x*n/8)),direct_diff(cos(x*n/8)))
for k in range(5):
assert_array_almost_equal(diff(sin(4*x),k),direct_diff(sin(4*x),k))
assert_array_almost_equal(diff(cos(4*x),k),direct_diff(cos(4*x),k))
def check_period(self):
for n in [17,64]:
x = arange(n)/float(n)
assert_array_almost_equal(diff(sin(2*pi*x),period=1),
2*pi*cos(2*pi*x))
assert_array_almost_equal(diff(sin(2*pi*x),3,period=1),
-(2*pi)**3*cos(2*pi*x))
def check_sin(self):
for n in [32,64,77]:
x = arange(n)*2*pi/n
assert_array_almost_equal(diff(sin(x)),cos(x))
assert_array_almost_equal(diff(cos(x)),-sin(x))
assert_array_almost_equal(diff(sin(x),2),-sin(x))
assert_array_almost_equal(diff(sin(x),4),sin(x))
assert_array_almost_equal(diff(sin(4*x)),4*cos(4*x))
assert_array_almost_equal(diff(sin(sin(x))),cos(x)*cos(sin(x)))
def check_expr(self):
for n in [64,77,100,128,256,512,1024,2048,4096,8192][:5]:
x = arange(n)*2*pi/n
f=sin(x)*cos(4*x)+exp(sin(3*x))
df=cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x))
ddf=-17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\
-9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x))
d1 = diff(f)
assert_array_almost_equal(d1,df)
assert_array_almost_equal(diff(df),ddf)
assert_array_almost_equal(diff(f,2),ddf)
assert_array_almost_equal(diff(ddf,-1),df)
#print max(abs(d1-df))
def check_expr_large(self):
for n in [2048,4096]:
x = arange(n)*2*pi/n
f=sin(x)*cos(4*x)+exp(sin(3*x))
df=cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x))
ddf=-17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\
-9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x))
assert_array_almost_equal(diff(f),df)
assert_array_almost_equal(diff(df),ddf)
assert_array_almost_equal(diff(ddf,-1),df)
assert_array_almost_equal(diff(f,2),ddf)
def check_int(self):
n = 64
x = arange(n)*2*pi/n
assert_array_almost_equal(diff(sin(x),-1),-cos(x))
assert_array_almost_equal(diff(sin(x),-2),-sin(x))
assert_array_almost_equal(diff(sin(x),-4),sin(x))
assert_array_almost_equal(diff(2*cos(2*x),-1),sin(2*x))
def check_random_even(self):
for k in [0,2,4,6]:
for n in [60,32,64,56,55]:
f=random ((n,))
af=sum(f)/n
f=f-af
# zeroing Nyquist mode:
f = diff(diff(f,1),-1)
assert_almost_equal(sum(f),0.0)
assert_array_almost_equal(diff(diff(f,k),-k),f)
assert_array_almost_equal(diff(diff(f,-k),k),f)
def check_random_odd(self):
for k in [0,1,2,3,4,5,6]:
for n in [33,65,55]:
f=random ((n,))
af=sum(f)/n
f=f-af
assert_almost_equal(sum(f),0.0)
assert_array_almost_equal(diff(diff(f,k),-k),f)
assert_array_almost_equal(diff(diff(f,-k),k),f)
def check_zero_nyquist (self):
for k in [0,1,2,3,4,5,6]:
for n in [32,33,64,56,55]:
f=random ((n,))
af=sum(f)/n
f=f-af
# zeroing Nyquist mode:
f = diff(diff(f,1),-1)
assert_almost_equal(sum(f),0.0)
assert_array_almost_equal(diff(diff(f,k),-k),f)
assert_array_almost_equal(diff(diff(f,-k),k),f)
def bench_random(self,level=5):
print
print 'Differentiation of periodic functions'
print '====================================='
print ' size | convolve | naive'
print '-------------------------------------'
for size,repeat in [(100,1500),(1000,300),
(256,1500),
(512,1000),
(1024,500),
(2048,200),
(2048*2,100),
(2048*4,50),
]:
print '%6s' % size,
sys.stdout.flush()
x = arange (size)*2*pi/size
if size<2000:
f = sin(x)*cos(4*x)+exp(sin(3*x))
else:
f = sin(x)*cos(4*x)
assert_array_almost_equal(diff(f,1),direct_diff(f,1))
assert_array_almost_equal(diff(f,2),direct_diff(f,2))
print '| %9.2f' % self.measure('diff(f,3)',repeat),
sys.stdout.flush()
print '| %9.2f' % self.measure('direct_diff(f,3)',repeat),
sys.stdout.flush()
print ' (secs for %s calls)' % (repeat)
class test_tilbert(ScipyTestCase):
def check_definition(self):
for h in [0.1,0.5,1,5.5,10]:
for n in [16,17,64,127]:
x = arange(n)*2*pi/n
y = tilbert(sin(x),h)
y1 = direct_tilbert(sin(x),h)
assert_array_almost_equal (y,y1)
assert_array_almost_equal(tilbert(sin(x),h),
direct_tilbert(sin(x),h))
assert_array_almost_equal(tilbert(sin(2*x),h),
direct_tilbert(sin(2*x),h))
def check_random_even(self):
for h in [0.1,0.5,1,5.5,10]:
for n in [32,64,56]:
f=random ((n,))
af=sum(f)/n
f=f-af
assert_almost_equal(sum(f),0.0)
assert_array_almost_equal(direct_tilbert(direct_itilbert(f,h),h),f)
def check_random_odd(self):
for h in [0.1,0.5,1,5.5,10]:
for n in [33,65,55]:
f=random ((n,))
af=sum(f)/n
f=f-af
assert_almost_equal(sum(f),0.0)
assert_array_almost_equal(itilbert(tilbert(f,h),h),f)
assert_array_almost_equal(tilbert(itilbert(f,h),h),f)
def bench_random(self,level=5):
print
print ' Tilbert transform of periodic functions'
print '========================================='
print ' size | optimized | naive'
print '-----------------------------------------'
for size,repeat in [(100,1500),(1000,300),
(256,1500),
(512,1000),
(1024,500),
(2048,200),
(2048*2,100),
(2048*4,50),
]:
print '%6s' % size,
sys.stdout.flush()
x = arange (size)*2*pi/size
if size<2000:
f = sin(x)*cos(4*x)+exp(sin(3*x))
else:
f = sin(x)*cos(4*x)
assert_array_almost_equal(tilbert(f,1),direct_tilbert(f,1))
print '| %9.2f' % self.measure('tilbert(f,1)',repeat),
sys.stdout.flush()
print '| %9.2f' % self.measure('direct_tilbert(f,1)',repeat),
sys.stdout.flush()
print ' (secs for %s calls)' % (repeat)
class test_itilbert(ScipyTestCase):
def check_definition(self):
for h in [0.1,0.5,1,5.5,10]:
for n in [16,17,64,127]:
x = arange(n)*2*pi/n
y = itilbert(sin(x),h)
y1 = direct_itilbert(sin(x),h)
assert_array_almost_equal (y,y1)
assert_array_almost_equal(itilbert(sin(x),h),
direct_itilbert(sin(x),h))
assert_array_almost_equal(itilbert(sin(2*x),h),
direct_itilbert(sin(2*x),h))
class test_hilbert(ScipyTestCase):
def check_definition(self):
for n in [16,17,64,127]:
x = arange(n)*2*pi/n
y = hilbert(sin(x))
y1 = direct_hilbert(sin(x))
assert_array_almost_equal (y,y1)
assert_array_almost_equal(hilbert(sin(2*x)),
direct_hilbert(sin(2*x)))
def check_tilbert_relation(self):
for n in [16,17,64,127]:
x = arange(n)*2*pi/n
f = sin (x)+cos (2*x)*sin(x)
y = hilbert(f)
y1 = direct_hilbert(f)
assert_array_almost_equal (y,y1)
y2 = tilbert(f,h=10)
assert_array_almost_equal (y,y2)
def check_random_odd(self):
for n in [33,65,55]:
f=random ((n,))
af=sum(f)/n
f=f-af
assert_almost_equal(sum(f),0.0)
assert_array_almost_equal(ihilbert(hilbert(f)),f)
assert_array_almost_equal(hilbert(ihilbert(f)),f)
def check_random_even(self):
for n in [32,64,56]:
f=random ((n,))
af=sum(f)/n
f=f-af
# zeroing Nyquist mode:
f = diff(diff(f,1),-1)
assert_almost_equal(sum(f),0.0)
assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)),f)
assert_array_almost_equal(hilbert(ihilbert(f)),f)
def bench_random(self,level=5):
print
print ' Hilbert transform of periodic functions'
print '========================================='
print ' size | optimized | naive'
print '-----------------------------------------'
for size,repeat in [(100,1500),(1000,300),
(256,1500),
(512,1000),
(1024,500),
(2048,200),
(2048*2,100),
(2048*4,50),
]:
print '%6s' % size,
sys.stdout.flush()
x = arange (size)*2*pi/size
if size<2000:
f = sin(x)*cos(4*x)+exp(sin(3*x))
else:
f = sin(x)*cos(4*x)
assert_array_almost_equal(hilbert(f),direct_hilbert(f))
print '| %9.2f' % self.measure('hilbert(f)',repeat),
sys.stdout.flush()
print '| %9.2f' % self.measure('direct_hilbert(f)',repeat),
sys.stdout.flush()
print ' (secs for %s calls)' % (repeat)
class test_ihilbert(ScipyTestCase):
def check_definition(self):
for n in [16,17,64,127]:
x = arange(n)*2*pi/n
y = ihilbert(sin(x))
y1 = direct_ihilbert(sin(x))
assert_array_almost_equal (y,y1)
assert_array_almost_equal(ihilbert(sin(2*x)),
direct_ihilbert(sin(2*x)))
def check_itilbert_relation(self):
for n in [16,17,64,127]:
x = arange(n)*2*pi/n
f = sin (x)+cos (2*x)*sin(x)
y = ihilbert(f)
y1 = direct_ihilbert(f)
assert_array_almost_equal (y,y1)
y2 = itilbert(f,h=10)
assert_array_almost_equal (y,y2)
class test_shift(ScipyTestCase):
def check_definition(self):
for n in [18,17,64,127,32,2048,256]:
x = arange(n)*2*pi/n
for a in [0.1,3]:
assert_array_almost_equal(shift(sin(x),a),direct_shift(sin(x),a))
assert_array_almost_equal(shift(sin(x),a),sin(x+a))
assert_array_almost_equal(shift(cos(x),a),cos(x+a))
assert_array_almost_equal(shift(cos(2*x)+sin(x),a),
cos(2*(x+a))+sin(x+a))
assert_array_almost_equal(shift(exp(sin(x)),a),exp(sin(x+a)))
assert_array_almost_equal(shift(sin(x),2*pi),sin(x))
assert_array_almost_equal(shift(sin(x),pi),-sin(x))
assert_array_almost_equal(shift(sin(x),pi/2),cos(x))
def bench_random(self,level=5):
print
print ' Shifting periodic functions'
print '=============================='
print ' size | optimized | naive'
print '------------------------------'
for size,repeat in [(100,1500),(1000,300),
(256,1500),
(512,1000),
(1024,500),
(2048,200),
(2048*2,100),
(2048*4,50),
]:
print '%6s' % size,
sys.stdout.flush()
x = arange (size)*2*pi/size
a = 1
if size<2000:
f = sin(x)*cos(4*x)+exp(sin(3*x))
sf = sin(x+a)*cos(4*(x+a))+exp(sin(3*(x+a)))
else:
f = sin(x)*cos(4*x)
sf = sin(x+a)*cos(4*(x+a))
assert_array_almost_equal(direct_shift(f,1),sf)
assert_array_almost_equal(shift(f,1),sf)
print '| %9.2f' % self.measure('shift(f,a)',repeat),
sys.stdout.flush()
print '| %9.2f' % self.measure('direct_shift(f,a)',repeat),
sys.stdout.flush()
print ' (secs for %s calls)' % (repeat)
if __name__ == "__main__":
ScipyTest('fftpack.pseudo_diffs').run()
|