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 ≈ xAppleAccelerate.plan_fft — Function
plan_fft(x::VecOrMat{Complex{T}}) where T <: Union{Float32, Float64}
plan_fft(n::Integer, [T=Float64], [radix=2])Create a reusable FFT setup object for repeated transforms of the same size. Wraps vDSP_create_fftsetup / vDSP_create_fftsetupD. The setup is automatically destroyed when garbage collected.
AppleAccelerate.fft — Function
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).
AppleAccelerate.ifft — Function
ifft(x::VecOrMat{Complex{T}}, [setup::FFTSetup{T}])Compute the normalized inverse FFT of x via Apple vDSP. Equivalent to bfft(x) / length(x). Satisfies ifft(fft(x)) ≈ x. Wraps vDSP_fft_zop (1D) / vDSP_fft2d_zop (2D).
AppleAccelerate.bfft — Function
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).
AppleAccelerate.fft! — Function
fft!(x::VecOrMat{Complex{T}}, [setup::FFTSetup{T}])Compute the forward FFT of x in-place via Apple vDSP, overwriting x with the result. Wraps vDSP_fft_zip (1D) / vDSP_fft2d_zip (2D).
AppleAccelerate.ifft! — Function
ifft!(x::VecOrMat{Complex{T}}, [setup::FFTSetup{T}])Compute the normalized inverse FFT of x in-place via Apple vDSP. Satisfies ifft!(fft!(copy(x))) ≈ x. Wraps vDSP_fft_zip (1D) / vDSP_fft2d_zip (2D).
AppleAccelerate.bfft! — Function
bfft!(x::VecOrMat{Complex{T}}, [setup::FFTSetup{T}])Compute the unnormalized inverse FFT of x in-place via Apple vDSP. Wraps vDSP_fft_zip (1D) / vDSP_fft2d_zip (2D).
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 ≈ xAppleAccelerate.plan_rfft — Function
plan_rfft(x::Vector{T}) where T <: Union{Float32, Float64}Create a reusable FFT setup for real-input forward transforms of the same size as x. Wraps vDSP_create_fftsetup.
AppleAccelerate.rfft — Function
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.
AppleAccelerate.irfft — Function
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.
AppleAccelerate.brfft — Function
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.
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 ≈ xThe extension supports the full AbstractFFTs interface for 1D and 2D:
| Function | Description |
|---|---|
fft, ifft, bfft | Out-of-place complex FFT |
fft!, ifft!, bfft! | In-place complex FFT |
plan_fft, plan_ifft, plan_bfft | Out-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_seqReal 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_dft — Function
plan_dft(length::Int, direction::Int, ::Type{T}=Float32; previous=C_NULL) where TCreate 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}
AppleAccelerate.dft — Function
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.
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}}
dft(X::Vector{Complex{T}}, direction::Int) where T
dft(X::Vector{Complex{T}}) where TCompute the DFT of X, auto-creating a setup. Default direction is forward. Wraps vDSP_DFT_Execute.
Returns: Vector{Complex{T}}
AppleAccelerate.idft — Function
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}}
DCT (Discrete Cosine Transform)
Wraps Apple's vDSP DCT functions. Float32 only. Supports DCT types II, III, and IV.
| Function | Description |
|---|---|
plan_dct | Create a DCT setup object |
dct | Compute the Discrete Cosine Transform |
plan_destroy | Destroy a DCT setup object |
AppleAccelerate.plan_dct — Function
plan_dct(length, dct_type, [previous])Create a DCT setup object. dct_type must be 2, 3, or 4 (Type II, III, IV). Length must be f * 2^n where f ∈ {1,3,5,15} and n ≥ 4. Wraps vDSP_DCT_CreateSetup.
AppleAccelerate.dct — Function
dct(X::Vector{Float32}, setup::DFTSetup)
dct(X::Vector{Float32}, [dct_type=2])Compute the Discrete Cosine Transform of X. Wraps vDSP_DCT_Execute.
AppleAccelerate.plan_destroy — Function
plan_destroy(setup::DFTSetup)Destroy a DCT/DFT setup object, freeing its resources. Wraps vDSP_DFT_DestroySetup.