def test_simple_multi_fft_1_D(): shift = 1 dt = 'D' c = np.outer(np.ones(64), ref_1D_32hz(shift, dt)) c2 = c.copy() ct = c.copy().T ct2 = c.copy().T _fftn(c, axes=(-1,), inplace=True, shift=shift) _fftn(ct, axes=(0,), inplace=True, shift=shift) c_np = reference_fftn(c2, axes=(-1,), shift=shift) ct_np = reference_fftn(ct2, axes=(0,), shift=shift) yield sum_of_sqr_comp(c, c_np, 'total energy not equal') yield sum_of_sqr_comp(ct, ct_np, 'total energy not equal')
def test_strided_1d_fft_1_D(): shift = 1 dt = 'D' rand_3d = (np.random.randn(40, 50, 60) + \ 1j*np.random.randn(40, 50, 60)).astype(dt) r1 = rand_3d.copy().transpose(0,2,1) r1_2 = rand_3d.copy().transpose(0,2,1) r2 = rand_3d.copy().transpose(1,0,2) r2_2 = rand_3d.copy().transpose(1,0,2) _fftn(r1, axes=(0,), inplace=True, shift=shift) _fftn(r2, axes=(1,), inplace=True, shift=shift) r1_np = reference_fftn(r1_2, axes=(0,), shift=shift) r2_np = reference_fftn(r2_2, axes=(1,), shift=shift) yield sum_of_sqr_comp(r1, r1_np, 'axis0 dtype='+dt) yield sum_of_sqr_comp(r2, r2_np, 'axis1 dtype='+dt)
def test_roundtrip_inplace_1_D(): shift = 1 dt = 'D' grid = np.arange(128) mu = 43. stdv = 3. g = (np.exp(-(grid-mu)**2 / (2*stdv**2)) / (2*np.pi*stdv**2)).astype(dt) g2 = g.copy() g_bkp = g.copy() _fftn(g, inplace=True, shift=shift) gw_np = reference_fftn(g2, shift=shift, axes=(0,)) yield npt.assert_array_almost_equal( g, gw_np, err_msg='differs from numpy fft ref, shift=%d'%shift ) yield sum_of_sqr_comp(g, gw_np) _ifftn(g, inplace=True, shift=shift) yield npt.assert_array_almost_equal( g_bkp, g, err_msg='roundtrip transforms diverge, shift=%d'%shift ) yield sum_of_sqr_comp(g_bkp, g)
def gaussian_smooth(a, fwhm=5.0, axes=(-1,), inplace=True): """Smooth an array by convolving with a Guassian kernel. Parameters ---------- a : ndarray Array to smooth fwhm : float or iterable (optional) The FWHM kernel size, or sizes for each dimension to smooth (pix units) axes : iterable (optional) The axes over which to convolve inplace : bool (optional) Modify the array inplace, or return a new, smoothed array Returns ------- a smoothed array, if inplace==False """ if hasattr(fwhm, '__iter__'): if len(fwhm) != len(axes): raise ValueError('Number of smoothing kernel sizes does not match the number of smoothing dimensions') else: fwhm = [fwhm]*len(axes) sigma = np.array(map(lambda x: x / np.sqrt(8 * np.log(2)), fwhm)) a_real = a.dtype.char not in ['F', 'D'] if a_real: if inplace: warnings.warn('inplace=True not really an option yet for real types') a = a.astype('D') # but now let's work inplace on the temporary casted array inplace = True if inplace: _fftn(a, axes=axes, inplace=True, shift=False) # name-only assignment b = a else: # new array assignment b = _fftn(a, axes=axes, inplace=False, shift=False) null_slices = [np.newaxis] * len(a.shape) all_slices = [slice(None)] * len(a.shape) for n, s in enumerate(sigma): d = 2 * (a.shape[axes[n]]/2) g = __kernel_cache.get((d,s), None) if g is None: print 'making new kernel for', d,s x = np.linspace(-d/2, d/2-1, d) x = np.r_[x[-d/2:], x[:d/2]] g = np.exp(-(x**2)/(2*s**2)).astype('D') fft1(g, shift=False, inplace=True) __kernel_cache[(d,s)] = g ax_slice = null_slices[:] ax_slice[axes[n]] = slice(None) safe_b_slice = all_slices[:] safe_b_slice[axes[n]] = slice(0,d) b[safe_b_slice] *= g[ax_slice].real n = len(sigma) b /= np.sqrt((2*np.pi)**n * np.prod(sigma**2)) _ifftn(b, axes=axes, inplace=True, shift=False) if a_real: br = b.real del b return br if not inplace: return b
def _get_2D_fft(shift, dt): c = ref_2D_grating(shift, dt) c2 = c.copy() _fftn(c, axes=(0,1), shift=shift, inplace=True) c_np = reference_fftn(c2, axes=(0,1), shift=shift) return c, c_np
def _get_1D_fft(shift, dt): c = ref_1D_32hz(shift, dt) c2 = c.copy() _fftn(c, axes=(0,), shift=shift, inplace=True) c_np = reference_fftn(c2, axes=(0,), shift=shift) return c, c_np