packages

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

  • dct Cosine 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.
  • freq The frequencies of FFT bins, and moving the zero frequency to the middle.
  • nd N-dimensional FFTs: one 1-d transform per chosen axis, each over every line of the array along that axis (row-column method).
  • plan Plan: a length's passes and twiddles, built once and reused.
  • real Real-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}
  • dct The discrete cosine transform of kind 2 (the default, "the DCT") or 3 (its inverse up to scale).
  • idct The inverse of dct with the same kind and norm: idct(dct(x)) is x.
  • dst The discrete sine transform of kind 2 or 3.
  • idst The inverse of dst with the same kind and norm.

freq::{fftfreq, rfftfreq, fftshift, ifftshift}Source

pub use freq::{fftfreq, rfftfreq, fftshift, ifftshift}
  • fftfreq The frequency of each fft bin for n samples spaced d apart (d = 1 / fs): [0, 1, .., n/2 - 1, -n/2, .., -1] / (d n) for even n, as NumPy.
  • rfftfreq The frequency of each rfft bin: [0, 1, .., n/2] / (d n).
  • fftshift x with the zero-frequency bin moved to the middle of each axis in axes (every axis by default), for plotting a spectrum.
  • ifftshift The inverse of fftshift (they differ for odd lengths).

nd::{fftn, ifftn, fft2, ifft2, rfftn, irfftn}Source

pub use nd::{fftn, ifftn, fft2, ifft2, rfftn, irfftn}
  • fftn The N-d DFT of x over axes (every axis by default), in the order given; an axis named twice is transformed twice.
  • ifftn The inverse of fftn over the same axes.
  • fft2 The 2-d DFT of a matrix (fftn over both axes).
  • ifft2 The inverse of fft2.
  • rfftn The N-d DFT of a real array: the last axis keeps bins 0..=n/2 (rfft), the others are full complex transforms.
  • irfftn The inverse of rfftn: n is the length of the last axis of the result (default 2 * (bins - 1)).

plan::{Plan, Norm, next_fast_len}Source

pub use plan::{Plan, Norm, next_fast_len}
  • Plan A complex FFT of one length with its twiddles computed once: build it with Plan::new(n) and run it on many signals.
  • Norm How a transform is scaled: Backward (the default, as NumPy) leaves the forward transform unscaled and divides the inverse by n; Ortho divides both by sqrt(n), so the pair is unitary; Forward divides the forward transform by n.
  • next_fast_len The smallest m >= n whose prime factors are 2, 3 and 5 only: the fastest lengths to pad to (for convolution, say).

real::RealPlanSource

pub use real::RealPlan
  • RealPlan A real FFT of one length, reusable: forward gives the n / 2 + 1 non-negative frequency bins, inverse takes 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).

nabi
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.

nabi
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.

nabi
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.

nabi
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]