Skip to content

Add stft and istft to mlx.core.fft - #3639

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
eyupcanakman:feat/fft-stft-istft
Oct 10, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
eyupcanakman:feat/fft-stft-istft

Conversation

@eyupcanakman

@eyupcanakman eyupcanakman commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Proposed changes

Adds stft and istft to mlx.core.fft, closes #1004. Both are written in C++ so they are available from every language binding.

stft frames the last axis with as_strided, applies the window, and runs rfft (or fft for two-sided output). istft inverts that with an overlap-add normalized by the squared-window envelope. The signature follows torch.stft, except norm takes the same "backward"/"ortho"/"forward" string the rest of mlx.fft uses. Batched input works and the output has shape (..., n_freq, n_frames).

Tested against torch.stft and torch.istft over reflect, constant, and edge padding, one-sided and two-sided transforms, the ortho norm, batched input, and n_fft that is not a multiple of hop_length. The C++ tests cover the shapes, an overlap-add round-trip, and the error paths.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Comment thread mlx/fft.cpp Outdated

namespace {

// Pad the last axis; mx::pad has no reflect mode, so build it from take.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would #3608 help?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, its reflect mode matches what this helper does and also handles pad >= n, so I can drop the helper and call pad directly once it lands. Want me to rebase on it after it merges, or keep the local version for now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for verifying, let's just keep the local version for now.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit I'm not familiar with short-time fft at all, but the implementation looks good, and the tests are well-written, so this PR looks good to me.

Comment thread mlx/fft.h
@eyupcanakman
eyupcanakman force-pushed the feat/fft-stft-istft branch from ab45a15 to bae1de8 Compare June 16, 2026 15:41
@eyupcanakman

Copy link
Copy Markdown
Contributor Author

Approved and green since mid-June. Not sure if this is waiting on something or just got buried.

@zcbenz zcbenz added the await verification This pull request is non-trivial and requires a human expert to verify its correctness. label Aug 4, 2026

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eyupcanakman Sorry for the long wait, I was waiting if someone else more familiar with fourier transform could take another look.

Would you mind replacing the pad_last_axis with pad op which supports reflect mode now? And then we can merge.

@zcbenz zcbenz removed the await verification This pull request is non-trivial and requires a human expert to verify its correctness. label Oct 5, 2026
@zcbenz zcbenz added the await response This pull request is waiting for response from the author. label Oct 6, 2026
Remove the pad_last_axis helper and call pad directly now that reflect
mode is supported. Inputs no longer than n_fft / 2 with pad_mode="reflect"
now follow NumPy's repeated reflection behavior.
@eyupcanakman

Copy link
Copy Markdown
Contributor Author

Replaced pad_last_axis with pad, removed the helper, and rebased on main (signals no longer than n_fft / 2 with reflect padding now follow NumPy's repeated reflection). Could you please approve the CI run when you get a chance?

@zcbenz zcbenz removed the await response This pull request is waiting for response from the author. label Oct 10, 2026
@zcbenz

zcbenz commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thanks for your patience!

@zcbenz
zcbenz merged commit 19ffe23 into ml-explore:main Oct 10, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Support Short-Time Fourier Transforms

2 participants