xrscipy.fft.fft
- xrscipy.fft.fft(x, coord, n=None, norm=None)
Compute the 1-D discrete Fourier Transform.
This function computes the 1-D n-point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm [1].
- Parameters:
x (xarray object) – The data to transform.
coord (string) – The axis along which the transform is applied. The coordinate must be evenly spaced.
n (int, optional) – Length of the transformed axis of the output. If n is smaller than the length of the input, the input is cropped. If it is larger, the input is padded with zeros. If n is not given, the length of the input along the axis specified by axis is used.
norm ({"backward", "ortho", "forward"}, optional) –
Normalization mode. Default is “backward”, meaning no normalization on the forward transforms and scaling by
1/n
on the ifft. “forward” instead applies the1/n
factor on the forward tranform. Fornorm="ortho"
, both directions are scaled by1/sqrt(n)
.New in version 1.6.0:
norm={"forward", "backward"}
options were added
- Returns:
out – The truncated or zero-padded input, transformed along the axis indicated by axis, or the last one if axis is not specified.
- Return type:
complex ndarray
- Raises:
IndexError – if axes is larger than the last axis of x.
See also
ifft
The inverse of fft.
fft2
The 2-D FFT.
fftn
The N-D FFT.
rfftn
The N-D FFT of real input.
fftfreq
Frequency bins for given FFT parameters.
next_fast_len
Size to pad input to for most efficient transforms
scipy.fft.fft
scipy.fft.fft : Original scipy implementation
Notes
FFT (Fast Fourier Transform) refers to a way the discrete Fourier Transform (DFT) can be calculated efficiently, by using symmetries in the calculated terms. The symmetry is highest when n is a power of 2, and the transform is therefore most efficient for these sizes. For poorly factorizable sizes, scipy.fft uses Bluestein’s algorithm [2] and so is never worse than O(n log n). Further performance improvements may be seen by zero-padding the input using next_fast_len.
If
x
is a 1d array, then the fft is equivalent toy[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(n)/n))
The frequency term
f=k/n
is found aty[k]
. Aty[n/2]
we reach the Nyquist frequency and wrap around to the negative-frequency terms. 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.Transforms can be done in single, double, or extended precision (long double) floating point. Half precision inputs will be converted to single precision and non-floating-point inputs will be converted to double precision.
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 are both real and symmetrical, the dct can again double the efficiency, by generating half of the spectrum from half of the signal.When
overwrite_x=True
is specified, the memory referenced byx
may be used by the implementation in any way. This may include reusing the memory for the result, but this is in no way guaranteed. You should not rely on the contents ofx
after the transform as this may change in future without warning.The
workers
argument specifies the maximum number of parallel jobs to split the FFT computation into. This will execute independent 1-D FFTs withinx
. So,x
must be at least 2-D and the non-transformed axes must be large enough to split into chunks. Ifx
is too small, fewer jobs may be used than requested.References
Examples
>>> import scipy.fft >>> import numpy as np >>> scipy.fft.fft(np.exp(2j * np.pi * np.arange(8) / 8)) array([-2.33486982e-16+1.14423775e-17j, 8.00000000e+00-1.25557246e-15j, 2.33486982e-16+2.33486982e-16j, 0.00000000e+00+1.22464680e-16j, -1.14423775e-17+2.33486982e-16j, 0.00000000e+00+5.20784380e-16j, 1.14423775e-17+1.14423775e-17j, 0.00000000e+00+1.22464680e-16j])
In this example, real input has an FFT which is Hermitian, i.e., symmetric in the real part and anti-symmetric in the imaginary part:
Examples
>>> from scipy.fft import fft, fftfreq, fftshift >>> import matplotlib.pyplot as plt >>> t = np.arange(256) >>> sp = fftshift(fft(np.sin(t))) >>> freq = fftshift(fftfreq(t.shape[-1])) >>> plt.plot(freq, sp.real, freq, sp.imag) [<matplotlib.lines.Line2D object at 0x...>, <matplotlib.lines.Line2D object at 0x...>]
Examples
>>> plt.show()