Skip to content

Real FFT

Structs

struct RealFFT

Real-valued FFT implementation using Cooley-Tukey algorithm.

If you're looking to create an FFT-based FX, look to the FFTProcessable trait used in conjunction with FFTProcess instead. This struct is a lower-level implementation that provides FFT and inverse FFT on fixed windows of real values. FFTProcessable structs will enable you to send audio samples (such as in a custom struct's .next() fn) into and out of an FFT, doing some manipulation of the magnitudes and phases in between. (FFTProcess has this RealFFT struct inside of it.)

Traits: AnyType, Copyable, ImplicitlyDeletable, Movable

RealFFT Parameters

Name Type Default Description
num_chans Int 1 Number of channels for SIMD processing.

RealFFT Functions

struct RealFFT . fn init

Initialize the RealFFT struct. All internal buffers and lookup tables are set up here based on the Parameters.

fn init Signature

def __init__(out self, window_size: Int)

fn init Arguments

Name Type Default Description
window_size Int FFT window size in samples.

fn init Returns : Self

Static Method

This is a static method.

struct RealFFT . fn fft

Compute the FFT of the input real-valued samples. The resulting magnitudes and phases are stored in the internal mags and phases lists.

fn fft Signature

def fft(mut self, input: List[SIMD[DType.float64, num_chans]])

fn fft Arguments

Name Type Default Description
input List[SIMD[DType.float64, num_chans]] The input real-valued samples to transform. This can be a List of SIMD vectors for multi-channel processing or a List of Float64 for single-channel processing.

struct RealFFT . fn fft

Compute the FFT of the input real-valued samples. The resulting magnitudes and phases are stored in the provided lists.

fn fft Signature

def fft(mut self, input: List[SIMD[DType.float64, num_chans]], mut mags: List[SIMD[DType.float64, num_chans]], mut phases: List[SIMD[DType.float64, num_chans]])

fn fft Arguments

Name Type Default Description
input List[SIMD[DType.float64, num_chans]] The input real-valued samples to transform. This can be a List of SIMD vectors for multi-channel processing or a List of Float64 for single-channel processing.
mags List[SIMD[DType.float64, num_chans]] A mutable list to store the magnitudes of the FFT result.
phases List[SIMD[DType.float64, num_chans]] A mutable list to store the phases of the FFT result.

struct RealFFT . fn ifft

Compute the inverse FFT using the internal magnitudes and phases. The output real-valued samples are written to the provided output list.

fn ifft Signature

def ifft(mut self, mut output: List[SIMD[DType.float64, num_chans]])

fn ifft Arguments

Name Type Default Description
output List[SIMD[DType.float64, num_chans]] A mutable list to store the output real-valued samples.

struct RealFFT . fn ifft

Compute the inverse FFT using the provided magnitudes and phases. The output real-valued samples are written to the provided output list.

fn ifft Signature

def ifft(mut self, mags: List[SIMD[DType.float64, num_chans]], phases: List[SIMD[DType.float64, num_chans]], mut output: List[SIMD[DType.float64, num_chans]])

fn ifft Arguments

Name Type Default Description
mags List[SIMD[DType.float64, num_chans]] A list of magnitudes for the inverse FFT.
phases List[SIMD[DType.float64, num_chans]] A list of phases for the inverse FFT.
output List[SIMD[DType.float64, num_chans]] A mutable list to store the output real-valued samples.

struct RealFFT . fn fft_frequencies

Compute the FFT bin center frequencies. This implementation is based on Librosa's eponymous function.

fn fft_frequencies Signature

def fft_frequencies(sr: Float64, n_fft: Int, min_bin: Int = 0, num_bins: Int = -1) -> List[Float64]

fn fft_frequencies Arguments

Name Type Default Description
sr Float64 The sample rate of the audio signal.
n_fft Int The size of the FFT.
min_bin Int 0 The minimum FFT bin index to include.
num_bins Int -1 The number of FFT bins to include. Defaults to all bins from min_bin to n_fft//2.

fn fft_frequencies Returns : List[Float64] A List of Float64 representing the center frequencies of each FFT bin.

Static Method

This is a static method.

struct RealFFT . fn buf_analysis

Compute the Short-Time Fourier Transform (STFT) of a buffer.

fn buf_analysis Signature

def buf_analysis[input_window_shape: WindowType = WindowType.hann](buf: Buffer, chan: Int, start_frame: Int, var num_frames: Int, window_size: Int, hop_size: Int) -> Tuple[List[List[Float64]], List[List[Float64]]]

fn buf_analysis Parameters

Name Type Default Description
input_window_shape WindowType WindowType.hann The type of window to apply to each frame before computing the FFT.

fn buf_analysis Arguments

Name Type Default Description
buf Buffer The input audio buffer to analyze.
chan Int The channel index to analyze from the buffer.
start_frame Int The starting frame index in the buffer to begin analysis.
num_frames Int The number of frames to analyze from the starting frame.
window_size Int The size of the FFT window.
hop_size Int The hop size between successive windows.

fn buf_analysis Returns : Tuple[List[List[Float64]], List[List[Float64]]] A tuple containing two lists of lists of Float64 representing the magnitudes and phases of the STFT for each frame and frequency bin.

Static Method

This is a static method.

struct RTPGHI

Real-Time Phase Gradient Heap Integration for spectrogram inversion.

Based on: Zdenek Prusa and Peter L. Soendergaard, "Real-time spectrogram inversion using phase gradient heap integration" Proceedings of DAFX 2016

This implementation uses one look-ahead frame for best quality. The algorithm reconstructs phase for frame n-1 using magnitude information from frames n-2, n-1, and n.

The process_frame method automatically delays the output magnitudes to match the reconstructed phases, so both outputs correspond to the same frame (n-1).

Traits: AnyType, Copyable, ImplicitlyDeletable, Movable


RTPGHI Functions

struct RTPGHI . fn init

Initialize RTPGHI with FFT size and hop size.

fn init Signature

def __init__(out self, fft_size: Int, hop_size: Int)

fn init Arguments

Name Type Default Description
fft_size Int The FFT size (window size).
hop_size Int The hop size between frames.

fn init Returns : Self

Static Method

This is a static method.

struct RTPGHI . fn reset

Reset the RTPGHI state.

fn reset Signature

def reset(mut self)

struct RTPGHI . fn process_frame

Process a magnitude spectrum frame and output matched magnitudes and phases. This function takes the current frame's magnitudes as input and outputs the previous frame's magnitudes along with their reconstructed phases. Both outputs correspond to frame n-1, ensuring they are properly synchronized.

Due to the one-frame look-ahead requirement, the first call will output zeros for both magnitudes and phases.

fn process_frame Signature

def process_frame(mut self, mut magnitudes: List[Float64], mut phases: List[Float64], tolerance: Float64 = 9.9999999999999995E-7)

fn process_frame Arguments

Name Type Default Description
magnitudes List[Float64] On input, the magnitude spectrum for frame n (bins elements).
On output, the magnitude spectrum for frame n-1.
phases List[Float64] Output phase spectrum for frame n-1 (bins elements, radians).
tolerance Float64 9.9999999999999995E-7 Relative magnitude threshold for phase reconstruction.

Documentation generated with mojo doc from Mojo version 1.0.0b2