Table of Contents

Class FftProcessor

Namespace
NAudio.Dsp
Assembly
NAudio.Core.dll

A reusable FFT processor specialised for fixed-size real-input audio work. Compared to calling FFT(bool, int, Complex[]) directly, this class:

  • Uses an N/2-point complex FFT plus an unpack pass to compute an N-point real FFT, returning an N/2 + 1-bin half-spectrum. Roughly half the work of the full complex FFT for the same N.
  • Precomputes the window coefficients (if configured) so the time-domain window is applied with one multiply per sample instead of a fresh cos evaluation.
  • Exposes Span<T> APIs so callers can pass stack-allocated or pooled buffers.
public sealed class FftProcessor
Inheritance
FftProcessor
Inherited Members

Remarks

Scaling matches FFT(bool, int, Complex[]): RealForward(ReadOnlySpan<float>, Span<Complex>) applies a 1/N scale (so a DC signal of magnitude 1 yields spectrum[0].Real = 1). RealInverse(ReadOnlySpan<Complex>, Span<float>) is un-scaled, so RealInverse(RealForward(x)) recovers x.

Constructors

FftProcessor(int, FftWindowType)

Creates an FFT processor for a fixed power-of-two size, optionally baking in a time-domain window.

public FftProcessor(int fftSize, FftWindowType window = FftWindowType.None)

Parameters

fftSize int

FFT size in samples. Must be a power of two and at least 2.

window FftWindowType

Window function applied to input samples by RealForward(ReadOnlySpan<float>, Span<Complex>). Use None to skip windowing.

Properties

FftSize

FFT size in samples (time-domain length).

public int FftSize { get; }

Property Value

int

SpectrumLength

Length of the complex half-spectrum produced by RealForward(ReadOnlySpan<float>, Span<Complex>): FftSize/2 + 1.

public int SpectrumLength { get; }

Property Value

int

Methods

ComplexForward(Span<Complex>)

In-place complex forward FFT. Thin forwarder to FFT(bool, int, Span<Complex>) for consumers that hold an FftProcessor instance and want symmetrical method names.

public void ComplexForward(Span<Complex> data)

Parameters

data Span<Complex>

ComplexInverse(Span<Complex>)

In-place complex inverse FFT. Thin forwarder to FFT(bool, int, Span<Complex>).

public void ComplexInverse(Span<Complex> data)

Parameters

data Span<Complex>

RealForward(ReadOnlySpan<float>, Span<Complex>)

Forward real FFT. Applies the configured window (if any), then computes the complex spectrum for positive frequencies k = 0 .. N/2. Negative frequencies are the complex conjugate of the positive ones and are not emitted.

public void RealForward(ReadOnlySpan<float> samples, Span<Complex> spectrum)

Parameters

samples ReadOnlySpan<float>

Time-domain samples. Must be FftSize long.

spectrum Span<Complex>

Output complex half-spectrum. Must be SpectrumLength long. spectrum[0].Imaginary and spectrum[FftSize/2].Imaginary are both zero by construction.

RealInverse(ReadOnlySpan<Complex>, Span<float>)

Inverse real FFT. Given the complex half-spectrum (SpectrumLength bins) produced by RealForward(ReadOnlySpan<float>, Span<Complex>), reconstructs the FftSize time-domain samples. No window is applied on the inverse path — if the consumer applied a window on the forward path they must undo it themselves.

public void RealInverse(ReadOnlySpan<Complex> spectrum, Span<float> samples)

Parameters

spectrum ReadOnlySpan<Complex>

Complex half-spectrum. Must be SpectrumLength long.

samples Span<float>

Output time-domain samples. Must be FftSize long.