We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
medium
primitives
Real FFT (rfft)
Why this matters
jnp.fft.rfft(x) is the FFT specialised for real-valued input. Because
the DFT of a real signal has Hermitian symmetry (the upper half of bins are
conjugates of the lower half), rfft only computes and returns the
non-redundant half: n // 2 + 1 complex bins instead of n.
This matters because:
-
~2Γ faster than full
fftfor the same real input. - ~2Γ less memory for the frequency representation.
-
Audio, images, and sensor readings are almost always real β
rfftis the default in practice.
Key facts:
-
Output shape:
(n // 2 + 1,)for input of lengthn. -
Inverse:
jnp.fft.irfft(X, n)β pass the original lengthnexplicitly. -
Not interchangeable with
fftfor complex inputs.
Worked mini-example
import jax.numpy as jnp
x = jnp.array([1.0, 0.0, 0.0, 0.0]) # length 4
mags = jnp.abs(jnp.fft.rfft(x))
# output has length 4//2 + 1 = 3
# mags = [1.0, 1.0, 1.0]
Common pitfalls
-
Output length is
n // 2 + 1, notnβ donβt assume same-length output. -
Inverse needs
nβ callirfft(X, n)with the original signal length; otherwise even-length signals reconstruct fine, odd-length signals may not. -
Complex input is undefined β
rfftsilently produces wrong results for complex input; usefftinstead.
Problem
Implement rfft_magnitude(x) that returns the magnitude of each rfft bin.
-
x: 1-D jax array (real). -
Returns: 1-D array
(n // 2 + 1,)β magnitudes of the half-spectrum.
Hints
jax
fft
rfft
Sign in to attempt this problem and view the solution.