viva_math/fft
Fast Fourier Transform over viva_math/complex.
Implements the recursive Cooley-Tukey radix-2 FFT. Inputs to fft and
ifft must have power-of-two length; use pad_to_power_of_two when a
signal needs zero-padding first.
Examples
import viva_math/complex
import viva_math/fft
fft.fft([
complex.real(1.0),
complex.zero(),
complex.zero(),
complex.zero(),
])
// -> [1+0i, 1+0i, 1+0i, 1+0i]
Values
pub fn fft(
signal: List(complex.Complex),
) -> List(complex.Complex)
Forward FFT.
The signal length must be a power of two. Invalid lengths panic via
fft_size, matching the total return type requested for this module.
pub fn fft_size(signal: List(complex.Complex)) -> Int
Return the signal length if it is a power of two.
Panics for empty or non-power-of-two inputs because the public API returns
Int directly.
pub fn ifft(
spectrum: List(complex.Complex),
) -> List(complex.Complex)
Inverse FFT using IFFT(x) = conj(FFT(conj(x))) / N.
The spectrum length must be a power of two. The final result is normalized
by N, so ifft(fft(signal)) recovers the original signal up to floating
point round-off.
pub fn pad_to_power_of_two(
signal: List(complex.Complex),
) -> List(complex.Complex)
Pad a signal with complex zeros up to the next power of two.
Already valid lengths are returned unchanged; an empty signal remains empty.