FFT & Transforms

AppleAccelerate wraps Apple's vDSP transform functions for FFT, real FFT, DFT, and DCT.

FFT

The FFT API wraps Apple's vDSP FFT functions and follows the same naming conventions as AbstractFFTs.jl. Both 1D vectors and 2D matrices are supported with ComplexF64 and ComplexF32 inputs. All dimensions must be powers of 2.

x = randn(ComplexF64, 1024)

# Forward FFT (auto-creates plan)
X = AppleAccelerate.fft(x)

# Normalized inverse FFT
x_recovered = AppleAccelerate.ifft(X)

# Unnormalized inverse (backward) FFT
X_back = AppleAccelerate.bfft(X)

# Reusable plan for repeated transforms of the same size
setup = AppleAccelerate.plan_fft(x)
X = AppleAccelerate.fft(x, setup)
x_recovered = AppleAccelerate.ifft(X, setup)

2D FFT works the same way — fft dispatches on the input shape:

x2 = randn(ComplexF64, 16, 32)
X2 = AppleAccelerate.fft(x2)
x2_recovered = AppleAccelerate.ifft(X2)

In-place FFT

In-place variants avoid allocating the output array:

x = randn(ComplexF64, 1024)
y = copy(x)
AppleAccelerate.fft!(y)       # forward, modifies y in-place
AppleAccelerate.ifft!(y)      # inverse, modifies y in-place
@assert y ≈ x
AppleAccelerate.fftFunction
fft(x::VecOrMat{Complex{T}}, [setup::FFTSetup{T}])

Compute the forward FFT of x via Apple vDSP. Supports 1D vectors and 2D matrices with ComplexF32 or ComplexF64 elements. All dimensions must be powers of 2. If setup is omitted, a temporary plan is created automatically. Wraps vDSP_fft_zop (1D) / vDSP_fft2d_zop (2D).

source
AppleAccelerate.bfftFunction
bfft(x::VecOrMat{Complex{T}}, [setup::FFTSetup{T}])

Compute the unnormalized inverse (backward) FFT of x via Apple vDSP. The result is not divided by length(x); use ifft for the normalized version. Wraps vDSP_fft_zop (1D) / vDSP_fft2d_zop (2D).

source

Real FFT

rfft computes the FFT of real input, returning only the non-redundant complex coefficients (length N÷2+1). irfft inverts it back to real output.

x = randn(Float64, 1024)
X = AppleAccelerate.rfft(x)          # Complex vector of length 513
x_recovered = AppleAccelerate.irfft(X, 1024)  # Back to real, length 1024
@assert x_recovered ≈ x
AppleAccelerate.rfftFunction
rfft(x::Vector{T}, [setup::FFTSetup{T}])

Compute the forward FFT of a real-valued vector x via Apple vDSP. Returns the non-redundant complex coefficients of length N÷2+1. Input length must be a power of 2. Wraps vDSP_fft_zrop.

source
AppleAccelerate.irfftFunction
irfft(X::Vector{Complex{T}}, n::Int, [setup::FFTSetup{T}])

Compute the normalized inverse real FFT, returning a real vector of length n. Satisfies irfft(rfft(x), length(x)) ≈ x. Wraps vDSP_fft_zrop.

source
AppleAccelerate.brfftFunction
brfft(X::Vector{Complex{T}}, n::Int, [setup::FFTSetup{T}])

Compute the unnormalized inverse real FFT, returning a real vector of length n. X must have length n÷2+1. The result is not divided by n; use irfft for the normalized version. Wraps vDSP_fft_zrop.

source

AbstractFFTs Integration

AppleAccelerate provides a package extension for AbstractFFTs.jl, allowing you to use the standard Julia FFT interface backed by Apple's vDSP library. Load both packages to activate the extension:

x = randn(ComplexF64, 1024)

# Out-of-place
X = AppleAccelerate.fft(x)
x_recovered = AppleAccelerate.ifft(X)
@assert x_recovered ≈ x

# In-place
y = copy(x)
AppleAccelerate.fft!(y)
AppleAccelerate.ifft!(y)
@assert y ≈ x

The extension supports the full AbstractFFTs interface for 1D and 2D:

FunctionDescription
fft, ifft, bfftOut-of-place complex FFT
fft!, ifft!, bfft!In-place complex FFT
plan_fft, plan_ifft, plan_bfftOut-of-place plans
plan_fft!, plan_bfft!In-place plans
inv(plan), p \ x, mul!Plan operations

Input must be Vector or Matrix with Complex{T} elements and power-of-2 dimensions. Non-power-of-2 inputs, empty arrays, and 3D+ arrays will throw an ArgumentError with a message suggesting to use FFTW.jl.

Region argument

For 2D transforms, the region argument selects which dimensions to transform — matching the AbstractFFTs convention:

x = randn(ComplexF64, 16, 32)

p_cols = plan_fft(x, (1,))   # transform along columns only (dim 1)
p_rows = plan_fft(x, (2,))   # transform along rows only (dim 2)
p_both = plan_fft(x, (1,2))  # transform along both (default)

# Equivalent to two 1D FFTs composed:
X_both = p_both * x
X_seq  = p_rows * (p_cols * x)
@assert X_both ≈ X_seq
Note

Real FFT (rfft, irfft, brfft) is available only via the direct AppleAccelerate.* API and is not wired into the AbstractFFTs extension, to avoid dispatch conflicts with other packages that call rfft internally on non-power-of-2 inputs.

DFT (Complex Discrete Fourier Transform)

Wraps Apple's vDSP_DFT_zop for complex-to-complex DFT. Unlike the FFT functions, DFT supports non-power-of-2 lengths of the form f * 2^n where f ∈ {1, 3, 5, 15} and n ≥ 3. Both Float32 and Float64 are supported.

x = randn(ComplexF64, 120)  # 120 = 15 * 2^3, non-power-of-2

# Forward DFT (auto-creates setup)
X = AppleAccelerate.dft(x)

# Normalized inverse DFT
x_recovered = AppleAccelerate.idft(X)
@assert x_recovered ≈ x

# Reusable setup for repeated transforms
setup_fwd = AppleAccelerate.plan_dft(120, AppleAccelerate.DFT_FORWARD, Float64)
setup_inv = AppleAccelerate.plan_dft(120, AppleAccelerate.DFT_INVERSE, Float64)
X = AppleAccelerate.dft(x, setup_fwd)
x_recovered = AppleAccelerate.idft(X, setup_inv)
AppleAccelerate.plan_dftFunction
plan_dft(length::Int, direction::Int, ::Type{T}=Float32; previous=C_NULL) where T

Create a DFT setup for complex-to-complex DFT of the given length and direction (DFT_FORWARD or DFT_INVERSE). Length must be f * 2^n where f ∈ {1, 3, 5, 15} and n ≥ 3. Optionally pass a previous setup to share underlying data. Wraps vDSP_DFT_zop_CreateSetup.

Returns: DFTSetup{T}

source
AppleAccelerate.dftFunction
dft(Ir::Vector{T}, Ii::Vector{T}, setup::DFTSetup{T})

Execute the complex DFT defined by setup on split-complex input (Ir, Ii). Wraps vDSP_DFT_Execute.

Returns: (Or, Oi) — real and imaginary parts of the output.

source
dft(X::Vector{Complex{T}}, setup::DFTSetup{T})

Execute the complex DFT on an interleaved complex vector. Wraps vDSP_DFT_Execute.

Returns: Vector{Complex{T}}

source
dft(X::Vector{Complex{T}}, direction::Int) where T
dft(X::Vector{Complex{T}}) where T

Compute the DFT of X, auto-creating a setup. Default direction is forward. Wraps vDSP_DFT_Execute.

Returns: Vector{Complex{T}}

source
AppleAccelerate.idftFunction
idft(X::Vector{Complex{T}}, setup::DFTSetup{T})
idft(X::Vector{Complex{T}})

Compute the normalized inverse DFT of X. The setup must have been created with DFT_INVERSE direction. Returns dft(X, setup) ./ length(X). Wraps vDSP_DFT_Execute.

Returns: Vector{Complex{T}}

source

DCT (Discrete Cosine Transform)

Wraps Apple's vDSP DCT functions. Float32 only. Supports DCT types II, III, and IV.

FunctionDescription
plan_dctCreate a DCT setup object
dctCompute the Discrete Cosine Transform
plan_destroyDestroy a DCT setup object