Skip to content

Fix spectrogram results on builds without complex numbers - #761

Merged
v923z merged 1 commit into
v923z:masterfrom
mikeysklar:spectrogram-no-complex
Nov 21, 2026
Merged

v923z merged 1 commit into
v923z:masterfrom
mikeysklar:spectrogram-no-complex

Conversation

@mikeysklar

@mikeysklar mikeysklar commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

utils.spectrogram returns wrong values when ULAB_SUPPORTS_COMPLEX is 0.

Why

Since #694, the FFT writes one data layout and the magnitude loop reads another.

CircuitPython uses this config, so every CircuitPython build is affected.

from ulab import numpy as np
from ulab import utils
d = np.zeros(8); d[0] = 1
utils.spectrogram(d)   # [1.41, 1.41, 1.41, 1.41, 0, 0, 0, 0], should be all 1.0

Fix

The magnitude loop and the free now use the same #if as the FFT.

512-point sine at bin 40 Before After
Peak bin 20 40
Bin 40 (abs(fft) = 3.072e6) 7.93 3.072e6
Max relative error vs abs(fft) 1.0 3.1e-13
Tested Result
Metro RP2350, CircuitPython main with this ulab, 3 runs pass
Without this patch: Metro ESP32-S3, and the cp-desktop-lab emulator (macOS arm64, Linux x86_64) fails the same way
ulab CI runs tests only with complex on, so no new test

Written with Claude Code. I ran the tests above on hardware.

🤖 Generated with Claude Code

With ULAB_FFT_IS_NUMPY_COMPATIBLE 1 and ULAB_SUPPORTS_COMPLEX 0, the FFT
fills separate real and imaginary arrays, but the magnitude loop read them
as interleaved pairs. v923z#694 changed only the FFT guard. Use the same guard
for the magnitude loop and the free.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@v923z

v923z commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thanks for catching this! In your comment above you mention that desktop builds fail the same way. Does that mean that the issue is actually not resolved?

@mikeysklar

Copy link
Copy Markdown
Contributor Author

No, the fix works. "Stock" in that row means without this patch.

On cp-desktop-lab, a software emulator of CircuitPython (Linux x86_64, CircuitPython's ulab config), same tree, 3 runs each:

Delta input 512-point sine
Without patch [1.41, 1.41, 1.41, 1.41, 0, 0, 0, 0] wrong peak, fails
With patch all 1.0 matches abs(fft) exactly

The Metro RP2350 matches: fails without the patch, passes with it.

@v923z
v923z merged commit 0cb255a into v923z:master Nov 21, 2026
15 of 16 checks passed
@v923z

v923z commented Nov 21, 2026

Copy link
Copy Markdown
Owner

The Metro RP2350 matches: fails without the patch, passes with it.

Thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants