fftpack.basic

Discrete Fourier Transforms - basic.py

Module Contents

Functions

istype(arr,typeclass)
_datacopied(arr,original) Strict check for arr not sharing any data with original,
_is_safe_size(n) Is the size of FFT such that FFTPACK can handle it in single precision
_fake_crfft(x,n,*a,**kw)
_fake_cfft(x,n,*a,**kw)
_fake_rfft(x,n,*a,**kw)
_fake_cfftnd(x,shape,*a,**kw)
_asfarray(x) Like numpy asfarray, except that it does not modify x dtype if x is
_fix_shape(x,n,axis) Internal auxiliary function for _raw_fft, _raw_fftnd.
_raw_fft(x,n,axis,direction,overwrite_x,work_function) Internal auxiliary function for fft, ifft, rfft, irfft.
fft(x,n=None,axis=None,overwrite_x=False) Return discrete Fourier transform of real or complex sequence.
ifft(x,n=None,axis=None,overwrite_x=False) Return discrete inverse Fourier transform of real or complex sequence.
rfft(x,n=None,axis=None,overwrite_x=False) Discrete Fourier transform of a real sequence.
irfft(x,n=None,axis=None,overwrite_x=False) Return inverse discrete Fourier transform of real sequence x.
_raw_fftnd(x,s,axes,direction,overwrite_x,work_function) Internal auxiliary function for fftnd, ifftnd.
fftn(x,shape=None,axes=None,overwrite_x=False) Return multidimensional discrete Fourier transform.
_raw_fftn_dispatch(x,shape,axes,overwrite_x,direction)
ifftn(x,shape=None,axes=None,overwrite_x=False) Return inverse multi-dimensional discrete Fourier transform of
fft2(x,shape=None,axes=tuple,overwrite_x=False) 2-D discrete Fourier transform.
ifft2(x,shape=None,axes=tuple,overwrite_x=False) 2-D discrete inverse Fourier transform of real or complex sequence.
istype(arr, typeclass)
_datacopied(arr, original)

Strict check for arr not sharing any data with original, under the assumption that arr = asarray(original)

_is_safe_size(n)

Is the size of FFT such that FFTPACK can handle it in single precision with sufficient accuracy?

Composite numbers of 2, 3, and 5 are accepted, as FFTPACK has those

_fake_crfft(x, n, *a, **kw)
_fake_cfft(x, n, *a, **kw)
_fake_rfft(x, n, *a, **kw)
_fake_cfftnd(x, shape, *a, **kw)
_asfarray(x)

Like numpy asfarray, except that it does not modify x dtype if x is already an array with a float dtype, and do not cast complex types to real.

_fix_shape(x, n, axis)

Internal auxiliary function for _raw_fft, _raw_fftnd.

_raw_fft(x, n, axis, direction, overwrite_x, work_function)

Internal auxiliary function for fft, ifft, rfft, irfft.

fft(x, n=None, axis=None, overwrite_x=False)

Return discrete Fourier transform of real or complex sequence.

The returned complex array contains y(0), y(1),..., y(n-1) where

y(j) = (x * exp(-2*pi*sqrt(-1)*j*np.arange(n)/n)).sum().

x : array_like
Array to Fourier transform.
n : int, optional
Length of the Fourier transform. If n < x.shape[axis], x is truncated. If n > x.shape[axis], x is zero-padded. The default results in n = x.shape[axis].
axis : int, optional
Axis along which the fft’s are computed; the default is over the last axis (i.e., axis=-1).
overwrite_x : bool, optional
If True, the contents of x can be destroyed; the default is False.
z : complex ndarray

with the elements:

[y(0),y(1),..,y(n/2),y(1-n/2),...,y(-1)]        if n is even
[y(0),y(1),..,y((n-1)/2),y(-(n-1)/2),...,y(-1)]  if n is odd

where:

y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k* 2*pi/n), j = 0..n-1

ifft : Inverse FFT rfft : FFT of a real sequence

The packing of the result is “standard”: If A = fft(a, n), then A[0] contains the zero-frequency term, A[1:n/2] contains the positive-frequency terms, and A[n/2:] contains the negative-frequency terms, in order of decreasingly negative frequency. So for an 8-point transform, the frequencies of the result are [0, 1, 2, 3, -4, -3, -2, -1]. To rearrange the fft output so that the zero-frequency component is centered, like [-4, -3, -2, -1, 0, 1, 2, 3], use fftshift.

Both single and double precision routines are implemented. Half precision inputs will be converted to single precision. Non floating-point inputs will be converted to double precision. Long-double precision inputs are not supported.

This function is most efficient when n is a power of two, and least efficient when n is prime.

Note that if x is real-valued then A[j] == A[n-j].conjugate(). If x is real-valued and n is even then A[n/2] is real.

If the data type of x is real, a “real FFT” algorithm is automatically used, which roughly halves the computation time. To increase efficiency a little further, use rfft, which does the same calculation, but only outputs half of the symmetrical spectrum. If the data is both real and symmetrical, the dct can again double the efficiency, by generating half of the spectrum from half of the signal.

>>> from scipy.fftpack import fft, ifft
>>> x = np.arange(5)
>>> np.allclose(fft(ifft(x)), x, atol=1e-15)  # within numerical accuracy.
True
ifft(x, n=None, axis=None, overwrite_x=False)

Return discrete inverse Fourier transform of real or complex sequence.

The returned complex array contains y(0), y(1),..., y(n-1) where

y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean().

x : array_like
Transformed data to invert.
n : int, optional
Length of the inverse Fourier transform. If n < x.shape[axis], x is truncated. If n > x.shape[axis], x is zero-padded. The default results in n = x.shape[axis].
axis : int, optional
Axis along which the ifft’s are computed; the default is over the last axis (i.e., axis=-1).
overwrite_x : bool, optional
If True, the contents of x can be destroyed; the default is False.
ifft : ndarray of floats
The inverse discrete Fourier transform.

fft : Forward FFT

Both single and double precision routines are implemented. Half precision inputs will be converted to single precision. Non floating-point inputs will be converted to double precision. Long-double precision inputs are not supported.

This function is most efficient when n is a power of two, and least efficient when n is prime.

If the data type of x is real, a “real IFFT” algorithm is automatically used, which roughly halves the computation time.

rfft(x, n=None, axis=None, overwrite_x=False)

Discrete Fourier transform of a real sequence.

x : array_like, real-valued
The data to transform.
n : int, optional
Defines the length of the Fourier transform. If n is not specified (the default) then n = x.shape[axis]. If n < x.shape[axis], x is truncated, if n > x.shape[axis], x is zero-padded.
axis : int, optional
The axis along which the transform is applied. The default is the last axis.
overwrite_x : bool, optional
If set to true, the contents of x can be overwritten. Default is False.
z : real ndarray

The returned real array contains:

[y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))]              if n is even
[y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))]   if n is odd

where:

y(j) = sum[k=0..n-1] x[k] * exp(-sqrt(-1)*j*k*2*pi/n)
j = 0..n-1

fft, irfft, numpy.fft.rfft

Within numerical accuracy, y == rfft(irfft(y)).

Both single and double precision routines are implemented. Half precision inputs will be converted to single precision. Non floating-point inputs will be converted to double precision. Long-double precision inputs are not supported.

To get an output with a complex datatype, consider using the related function numpy.fft.rfft.

>>> from scipy.fftpack import fft, rfft
>>> a = [9, -9, 1, 3]
>>> fft(a)
array([  4. +0.j,   8.+12.j,  16. +0.j,   8.-12.j])
>>> rfft(a)
array([  4.,   8.,  12.,  16.])
irfft(x, n=None, axis=None, overwrite_x=False)

Return inverse discrete Fourier transform of real sequence x.

The contents of x are interpreted as the output of the rfft function.

x : array_like
Transformed data to invert.
n : int, optional
Length of the inverse Fourier transform. If n < x.shape[axis], x is truncated. If n > x.shape[axis], x is zero-padded. The default results in n = x.shape[axis].
axis : int, optional
Axis along which the ifft’s are computed; the default is over the last axis (i.e., axis=-1).
overwrite_x : bool, optional
If True, the contents of x can be destroyed; the default is False.
irfft : ndarray of floats
The inverse discrete Fourier transform.

rfft, ifft, numpy.fft.irfft

The returned real array contains:

[y(0),y(1),...,y(n-1)]

where for n is even:

y(j) = 1/n (sum[k=1..n/2-1] (x[2*k-1]+sqrt(-1)*x[2*k])
                             * exp(sqrt(-1)*j*k* 2*pi/n)
            + c.c. + x[0] + (-1)**(j) x[n-1])

and for n is odd:

y(j) = 1/n (sum[k=1..(n-1)/2] (x[2*k-1]+sqrt(-1)*x[2*k])
                             * exp(sqrt(-1)*j*k* 2*pi/n)
            + c.c. + x[0])

c.c. denotes complex conjugate of preceding expression.

For details on input parameters, see rfft.

To process (conjugate-symmetric) frequency-domain data with a complex datatype, consider using the related function numpy.fft.irfft.

_raw_fftnd(x, s, axes, direction, overwrite_x, work_function)

Internal auxiliary function for fftnd, ifftnd.

fftn(x, shape=None, axes=None, overwrite_x=False)

Return multidimensional discrete Fourier transform.

The returned array contains:

y[j_1,..,j_d] = sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
   x[k_1,..,k_d] * prod[i=1..d] exp(-sqrt(-1)*2*pi/n_i * j_i * k_i)

where d = len(x.shape) and n = x.shape.

x : array_like
The (n-dimensional) array to transform.
shape : tuple of ints, 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].
axes : array_like of ints, optional
The axes of x (y if shape is not None) along which the transform is applied.
overwrite_x : bool, optional
If True, the contents of x can be destroyed. Default is False.
y : complex-valued n-dimensional numpy array
The (n-dimensional) DFT of the input array.

ifftn

If x is real-valued, then y[..., j_i, ...] == y[..., n_i-j_i, ...].conjugate().

Both single and double precision routines are implemented. Half precision inputs will be converted to single precision. Non floating-point inputs will be converted to double precision. Long-double precision inputs are not supported.

>>> from scipy.fftpack import fftn, ifftn
>>> y = (-np.arange(16), 8 - np.arange(16), np.arange(16))
>>> np.allclose(y, fftn(ifftn(y)))
True
_raw_fftn_dispatch(x, shape, axes, overwrite_x, direction)
ifftn(x, shape=None, axes=None, overwrite_x=False)

Return inverse multi-dimensional discrete Fourier transform of arbitrary type sequence x.

The returned array contains:

y[j_1,..,j_d] = 1/p * sum[k_1=0..n_1-1, ..., k_d=0..n_d-1]
   x[k_1,..,k_d] * prod[i=1..d] exp(sqrt(-1)*2*pi/n_i * j_i * k_i)

where d = len(x.shape), n = x.shape, and p = prod[i=1..d] n_i.

For description of parameters see fftn.

fftn : for detailed information.

fft2(x, shape=None, axes=tuple, overwrite_x=False)

2-D discrete Fourier transform.

Return the two-dimensional discrete Fourier transform of the 2-D argument x.

fftn : for detailed information.

ifft2(x, shape=None, axes=tuple, overwrite_x=False)

2-D discrete inverse Fourier transform of real or complex sequence.

Return inverse two-dimensional discrete Fourier transform of arbitrary type sequence x.

See ifft for more information.

fft2, ifft