fft: add irfft as the inverse real FFT (fixes #176) - #261
Merged
Conversation
sbryngelson
reviewed
Sep 2, 2026
sbryngelson
left a comment
Owner
There was a problem hiding this comment.
Verified on device (M5): all 8 tests in the new file pass. Thanks for adding tests/test_fft.py --
the FFT module had no pytest coverage at all before this.
Two small things before merge.
The docstring should say irfft takes the FULL length-N spectrum, the way this module's rfft
returns it, and not the N//2+1 half spectrum that numpy.fft.irfft expects. The name invites the
numpy reading, and the test itself has to slice Xr[:N // 2 + 1] to build its reference, so the
mismatch is already visible in the PR.
The new test file also has no trailing newline.
Both, as a patch:
diff --git a/aneforge/fft.py b/aneforge/fft.py
index 4350d40..d9cfb1c 100644
--- a/aneforge/fft.py
+++ b/aneforge/fft.py
@@ -292,7 +292,10 @@ def rfft(x_real, N: int):
def irfft(X_re, X_im, N: int):
"""Inverse real FFT of a Hermitian-symmetric spectrum on the ANE; returns the real time-domain
signal of length N (the imag part is ~0 by Hermitian symmetry, and numpy.fft.irfft also
- returns only the real part)."""
+ returns only the real part).
+
+ Takes the FULL length-N spectrum, as `rfft` returns it -- not the N//2+1 half spectrum that
+ numpy.fft.irfft expects."""
x_re, _ = ifft_plan(N)(X_re, X_im)
return x_re
diff --git a/tests/test_fft.py b/tests/test_fft.py
index 666d9c3..2be98c5 100644
--- a/tests/test_fft.py
+++ b/tests/test_fft.py
@@ -60,4 +60,4 @@ def test_irfft_returns_real_only():
x = rng.standard_normal(256).astype(np.float32)
Xr, Xi = agfft.rfft(x, 256)
back = agfft.irfft(Xr, Xi, 256)
- assert np.iscomplexobj(np.asarray(back)) is False
\ No newline at end of file
+ assert np.iscomplexobj(np.asarray(back)) is False
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #176. Adds
aneforge.fft.irfft(X_re, X_im, N)as the inverse real FFT, the natural completion of the existingrfft.Approach: thin wrapper over the existing
ifft_planmachinery, as the issue suggests. Runs the on-engine inverse FFT over the Hermitian-symmetric spectrum and returns the real part.ifftalready applies the 1/N normalization, and for a Hermitian spectrum the imaginary part is ~0 (numpy.fft.irfft also returns only the real part), so the real-signal contract holds with no new kernel or plan.Changes
aneforge/fft.py:irfftfunction + export in__all__.aneforge/fft.py_selftest: round-tripirfft(rfft(x)) ~= xacross N=128 and N=512, plus anumpy.fft.irfftoracle over the half spectrum.tests/test_fft.py(new,requires_aneliketest_fft2.py): 8 tests coveringfft/ifft/rfft/irfftagainst numpy, the round-trip, the numpy oracle, and thatirfftreturns a real array.Checks run
_selftestPASS,tests/test_fft.py8 passed.pytest -m "not requires_ane"407 passed, 2 skipped.ruff check, the pylint 2-space gate,pyright aneforge/fft.py tests/test_fft.py(0 errors), andcompileallall clean.CI runs the off-device subset; the transform itself dispatches, so the on-device
_selftestandtests/test_fft.pywere run locally and pass on M2 Pro.