Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Avhz RustQuant Fast Fourier Transform

From Leeroopedia


Knowledge Sources
Domains Numerical_Methods, Signal_Processing
Last Updated 2026-02-07 21:00 GMT

Overview

The Fast Fourier Transform (FFT) is an efficient algorithm for computing the Discrete Fourier Transform, reducing computational complexity from O(n^2) to O(n log n), with applications in signal processing and option pricing via characteristic functions.

Description

The Fast Fourier Transform implementation in RustQuant provides both real-valued and complex-valued FFT computation using the classic Cooley-Tukey radix-2 decimation-in-time algorithm.

The module exposes four primary functions:

  • fft_real(x): computes the FFT of a real-valued input vector, returning a new vector of results
  • fft_real_inplace(x): computes the FFT of a real-valued vector in place, modifying the input
  • fft_complex(x): computes the FFT of a complex-valued input vector, returning a new vector
  • fft_complex_inplace(x): computes the FFT of a complex-valued vector in place

All functions require that the input length be a power of 2, which is validated by the is_valid_length helper function. This constraint arises from the radix-2 algorithm's recursive halving strategy.

The implementation uses a recursive divide-and-conquer approach: 1. Split the input into even-indexed and odd-indexed subsequences 2. Recursively compute the FFT of each half 3. Combine the results using twiddle factors e2πik/n

For real-valued FFT, the twiddle factor's magnitude (norm) is used in the combination step, while the complex-valued FFT uses the full complex exponential multiplication.

The complex number support comes from the num::Complex crate, providing precise arithmetic for the Fourier basis functions.

Usage

Use the FFT for spectral analysis of time series data, frequency-domain filtering, and option pricing via characteristic function methods (e.g., the Carr-Madan method for pricing European options under models with known characteristic functions such as Heston or Variance Gamma). The input vector length must be a power of 2; pad with zeros if necessary.

Theoretical Basis

The Discrete Fourier Transform (DFT) of a sequence x0,x1,,xn1 is:

Xk=j=0n1xje2πijk/n,k=0,1,,n1

The Cooley-Tukey algorithm exploits the periodicity and symmetry of the complex exponential. By separating even and odd indices:

Xk=m=0n/21x2me2πimk/(n/2)+e2πik/nm=0n/21x2m+1e2πimk/(n/2)

This can be written as:

Xk=Ek+WnkOk

Xk+n/2=EkWnkOk

where Ek and Ok are the DFTs of the even and odd subsequences, and Wnk=e2πik/n is the twiddle factor. This butterfly operation reduces the O(n2) DFT to O(nlogn) recursive steps.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment