def irfftn(x, shape=None, axes=None, norm=None, overwrite_x=False): """Multi-dimensional inverse discrete fourier transform with real output""" tmp = np.asarray(x) # TODO: Optimize for hermitian and real? if np.isrealobj(tmp): tmp = tmp + 0.j noshape = shape is None shape, axes = _init_nd_shape_and_axes(tmp, shape, axes) if len(axes) == 0: raise ValueError("at least 1 axis must be transformed") if noshape: shape[-1] = (x.shape[axes[-1]] - 1) * 2 norm = _normalization(norm, False) # Last axis utilises hermitian symmetry lastsize = shape[-1] shape[-1] = (shape[-1] // 2) + 1 tmp, _ = _fix_shape(tmp, shape, axes) # Note: overwrite_x is not utilised return pfft.irfftn(tmp, axes, lastsize, norm, _default_workers)
def idctn(x, type=2, shape=None, axes=None, norm=None, overwrite_x=False): """ Return multidimensional Discrete Cosine Transform along the specified axes. Parameters ---------- x : array_like The input array. type : {1, 2, 3, 4}, optional Type of the DCT (see Notes). Default type is 2. shape : int or array_like of ints or None, optional The shape of the result. If both `shape` and `axes` (see below) are None, `shape` is ``x.shape``; if `shape` is None but `axes` is not None, then `shape` is ``scipy.take(x.shape, axes, axis=0)``. If ``shape[i] > x.shape[i]``, the i-th dimension is padded with zeros. If ``shape[i] < x.shape[i]``, the i-th dimension is truncated to length ``shape[i]``. If any element of `shape` is -1, the size of the corresponding dimension of `x` is used. axes : int or array_like of ints or None, optional Axes along which the IDCT is computed. The default is over all axes. norm : {None, 'ortho'}, optional Normalization mode (see Notes). Default is None. overwrite_x : bool, optional If True, the contents of `x` can be destroyed; the default is False. Returns ------- y : ndarray of real The transformed input array. See Also -------- dctn : multidimensional DCT Notes ----- For full details of the IDCT types and normalization modes, as well as references, see `idct`. Examples -------- >>> from scipy.fftpack import dctn, idctn >>> y = np.random.randn(16, 16) >>> np.allclose(y, idctn(dctn(y, norm='ortho'), norm='ortho')) True """ x = np.asanyarray(x) shape, axes = _init_nd_shape_and_axes(x, shape, axes) for n, ax in zip(shape, axes): x = idct(x, type=type, n=n, axis=ax, norm=norm, overwrite_x=overwrite_x) return x
def test_np_5d_set_shape_axes(self): x = np.zeros([6, 2, 5, 3, 4]) shape = [10, -1, 2] axes = [1, 0, 3] shape_expected = np.array([10, 6, 2]) axes_expected = np.array([1, 0, 3]) shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected)
def test_np_5d_set_axes(self): x = np.zeros([6, 2, 5, 3, 4]) shape = None axes = [4, 1, 2] shape_expected = np.array([4, 2, 5]) axes_expected = np.array([4, 1, 2]) shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected)
def rfftn(x, shape=None, axes=None, norm=None, overwrite_x=False): """Return multi-dimentional discrete Fourier transform of real input""" tmp = np.asarray(x) if not np.isrealobj(tmp): raise TypeError("x must be a real sequence") shape, axes = _init_nd_shape_and_axes(tmp, shape, axes) tmp, _ = _fix_shape(tmp, shape, axes) norm = _normalization(norm, True) if len(axes) == 0: raise ValueError("at least 1 axis must be transformed") # Note: overwrite_x is not utilised return pfft.rfftn(tmp, axes, norm, _default_workers)
def test_np_5d_set_shape(self): x = np.zeros([6, 2, 5, 3, 4]) shape = [10, -1, -1, 1, 4] axes = None shape_expected = np.array([10, 2, 5, 1, 4]) axes_expected = np.array([0, 1, 2, 3, 4]) shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected) shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected)
def test_py_1d_defaults(self): x = [1, 2, 3] shape = None axes = None shape_expected = np.array([3]) axes_expected = np.array([0]) shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected) shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected)
def test_np_0d_defaults(self): x = np.array(7.) shape = None axes = None shape_expected = np.array([]) axes_expected = np.array([]) shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected) shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected)
def test_np_2d_defaults(self): x = np.arange(0, 1, .1).reshape(5, 2) shape = None axes = None shape_expected = np.array([5, 2]) axes_expected = np.array([0, 1]) shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected) shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected)
def test_py_2d_defaults(self): x = [[1, 2, 3, 4], [5, 6, 7, 8]] shape = None axes = None shape_expected = np.array([2, 4]) axes_expected = np.array([0, 1]) shape_res, axes_res = _init_nd_shape_and_axes(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected) shape_res, axes_res = _init_nd_shape_and_axes_sorted(x, shape, axes) assert_equal(shape_res, shape_expected) assert_equal(axes_res, axes_expected)
def ifftn(x, shape=None, axes=None, norm=None, overwrite_x=False): """ Return inverse multi-dimensional discrete Fourier transform. """ tmp = np.asarray(x) shape, axes = _init_nd_shape_and_axes(tmp, shape, axes) overwrite_x = overwrite_x or _datacopied(tmp, x) if len(axes) == 0: return x tmp, copied = _fix_shape(tmp, shape, axes) overwrite_x = overwrite_x or copied norm = _normalization(norm, False) return pfft.ifftn(tmp, axes, norm, overwrite_x, _default_workers)
def c2cn(forward, x, shape=None, axes=None, norm=None, overwrite_x=False): """ Return multidimensional discrete Fourier transform. """ tmp = _asfarray(x) shape, axes = _init_nd_shape_and_axes(tmp, shape, axes) overwrite_x = overwrite_x or _datacopied(tmp, x) if len(axes) == 0: return x tmp, copied = _fix_shape(tmp, shape, axes) overwrite_x = overwrite_x or copied norm = _normalization(norm, forward) out = (tmp if overwrite_x and tmp.dtype.kind == 'c' else None) return pfft.c2c(tmp, axes, forward, norm, out, _default_workers)
def test_errors(self): with assert_raises(ValueError, match="when given, axes values must be a scalar" " or vector"): _init_nd_shape_and_axes([0], shape=None, axes=[[1, 2], [3, 4]]) with assert_raises(ValueError, match="when given, axes values must be integers"): _init_nd_shape_and_axes([0], shape=None, axes=[1., 2., 3., 4.]) with assert_raises(ValueError, match="axes exceeds dimensionality of input"): _init_nd_shape_and_axes([0], shape=None, axes=[1]) with assert_raises(ValueError, match="axes exceeds dimensionality of input"): _init_nd_shape_and_axes([0], shape=None, axes=[-2]) with assert_raises(ValueError, match="all axes must be unique"): _init_nd_shape_and_axes([0], shape=None, axes=[0, 0]) with assert_raises(ValueError, match="when given, shape values must be a scalar " "or vector"): _init_nd_shape_and_axes([0], shape=[[1, 2], [3, 4]], axes=None) with assert_raises(ValueError, match="when given, shape values must be integers"): _init_nd_shape_and_axes([0], shape=[1., 2., 3., 4.], axes=None) with assert_raises(ValueError, match="when given, axes and shape arguments" " have to be of the same length"): _init_nd_shape_and_axes(np.zeros([1, 1, 1, 1]), shape=[1, 2, 3], axes=[1]) with assert_raises(ValueError, match="invalid number of data points" r" \(\[0\]\) specified"): _init_nd_shape_and_axes([0], shape=[0], axes=None) with assert_raises(ValueError, match="invalid number of data points" r" \(\[-2\]\) specified"): _init_nd_shape_and_axes([0], shape=-2, axes=None)