signal::fft
On this page
Discrete Fourier transforms of any length: complex, real, n-d, the frequency helpers and the cosine and sine transforms built on them.
Each function builds a plan for its length on every call. To
transform many signals of one length, make a Plan / RealPlan
once and call it.
Modules
dctCosine and sine transforms of types II and III by one complex FFT of the same length (Makhoul 1980, "A fast cosine transform in one and two dimensions"): the even samples, then the odd ones reversed, then a quarter-sample twiddle.freqThe frequencies of FFT bins, and moving the zero frequency to the middle.ndN-dimensional FFTs: one 1-d transform per chosen axis, each over every line of the array along that axis (row-column method).planPlan: a length's passes and twiddles, built once and reused.realReal-input FFT: an even length n packs x into n / 2 complex points (z_j = x_2j + i x_2j+1), runs one half-length FFT and untangles the even and odd halves with one twiddle per bin; an odd length runs the full complex FFT.
Re-exports
dct::{dct, idct, dst, idst}Source
pub use dct::{dct, idct, dst, idst}freq::{fftfreq, rfftfreq, fftshift, ifftshift}Source
pub use freq::{fftfreq, rfftfreq, fftshift, ifftshift}fftfreqThe frequency of eachfftbin fornsamples spaceddapart (d = 1 / fs):[0, 1, .., n/2 - 1, -n/2, .., -1] / (d n)for evenn, as NumPy.rfftfreqThe frequency of eachrfftbin:[0, 1, .., n/2] / (d n).fftshiftxwith the zero-frequency bin moved to the middle of each axis inaxes(every axis by default), for plotting a spectrum.ifftshiftThe inverse offftshift(they differ for odd lengths).
nd::{fftn, ifftn, fft2, ifft2, rfftn, irfftn}Source
pub use nd::{fftn, ifftn, fft2, ifft2, rfftn, irfftn}fftnThe N-d DFT ofxoveraxes(every axis by default), in the order given; an axis named twice is transformed twice.ifftnThe inverse offftnover the sameaxes.fft2The 2-d DFT of a matrix (fftnover both axes).ifft2The inverse offft2.rfftnThe N-d DFT of a real array: the last axis keeps bins0..=n/2(rfft), the others are full complex transforms.irfftnThe inverse ofrfftn:nis the length of the last axis of the result (default2 * (bins - 1)).
plan::{Plan, Norm, next_fast_len}Source
pub use plan::{Plan, Norm, next_fast_len}PlanA complex FFT of one length with its twiddles computed once: build it withPlan::new(n)and run it on many signals.NormHow a transform is scaled:Backward(the default, as NumPy) leaves the forward transform unscaled and divides the inverse byn;Orthodivides both bysqrt(n), so the pair is unitary;Forwarddivides the forward transform byn.next_fast_lenThe smallestm >= nwhose prime factors are 2, 3 and 5 only: the fastest lengths to pad to (for convolution, say).
real::RealPlanSource
pub use real::RealPlanRealPlanA real FFT of one length, reusable:forwardgives then / 2 + 1non-negative frequency bins,inversetakes them back.
Functions
fftSource
pub fn fft(x: some Samples, n: usize? = none, norm: Norm = Norm::Backward) throws (SignalError) -> Vector<Complex<f64>> The DFT X_k = sum_j x_j exp(-2 pi i j k / n) of a real or complex
signal, cut or zero-padded to n samples when n is given. Any
length, O(n log n) (see Plan).
use signal::fft::fft;
use core::array::Vector;
let x: Vector<f64> = [1, 0, 0, 0];
println(signal::real(try fft(x))); // [1.0, 1.0, 1.0, 1.0]
ifftSource
pub fn ifft(x: some Samples, n: usize? = none, norm: Norm = Norm::Backward) throws (SignalError) -> Vector<Complex<f64>> The inverse DFT x_j = (1/n) sum_k X_k exp(2 pi i j k / n) (the
1/n under Norm::Backward), so ifft(fft(x)) is x to about
1e-15 relative.
use signal::fft::{fft, ifft};
use core::array::Vector;
let x: Vector<f64> = [1, 2, 3];
let back = try x |> fft |> ifft;
println(signal::real(back)); // [1.0, 2.0, 3.0] (to rounding)
rfftSource
pub fn rfft(x: some RealSamples, n: usize? = none, norm: Norm = Norm::Backward) throws (SignalError) -> Vector<Complex<f64>> Bins 0..=n/2 of the DFT of a real signal: the rest are their
conjugates. About half the work of fft.
use signal::fft::rfft;
use core::array::Vector;
let x: Vector<f64> = [1, 2, 3, 4];
let s = try rfft(x);
println(signal::real(s)); // [10.0, -2.0, -2.0]
println(signal::imag(s)); // [0.0, 2.0, 0.0]
irfftSource
pub fn irfft(x: Vector<Complex<f64>>, n: usize? = none, norm: Norm = Norm::Backward) throws (SignalError) -> Vector<f64> The n real samples whose non-negative bins are x (as rfft
gives them); n defaults to 2 * (x.len() - 1). Bins past n / 2
are dropped and missing ones are zero.
use signal::fft::{rfft, irfft};
use core::array::Vector;
let x: Vector<f64> = [1, 2, 3, 4, 5];
println(try x |> rfft |> irfft(n: 5)); // [1.0, 2.0, 3.0, 4.0, 5.0]