jax-ml/jax · error · ValueError

The extension length n ({n}) is too big. It must not exceed

Error message

The extension length n ({n}) is too big. It must not exceed x.shape[axis]-1, which is {x.shape[axis] - 1}.

What it means

odd_ext mirrors n points from each end of the signal to create an odd-symmetric extension (used by spectral boundary handling). This requires at least n+1 points along the axis; n larger than x.shape[axis]-1 would duplicate interior points, so it is rejected.

Source

Thrown at jax/_src/scipy/signal.py:601

  else:
    return jnp_fft.rfft(result.real, n=nfft)


def odd_ext(x: Array, n: int, axis: int = -1) -> Array:
  """Extends `x` along with `axis` by odd-extension.

  This function was previously a part of "scipy.signal.signaltools" but is no
  longer exposed.

  Args:
    x : input array
    n : the number of points to be added to the both end
    axis: the axis to be extended
  """
  if n < 1:
    return x
  if n > x.shape[axis] - 1:
    raise ValueError(
        f"The extension length n ({n}) is too big. "
        f"It must not exceed x.shape[axis]-1, which is {x.shape[axis] - 1}.")
  left_end = lax.slice_in_dim(x, 0, 1, axis=axis)
  left_ext = jnp.flip(lax.slice_in_dim(x, 1, n + 1, axis=axis), axis=axis)
  right_end = lax.slice_in_dim(x, -1, None, axis=axis)
  right_ext = jnp.flip(lax.slice_in_dim(x, -(n + 1), -1, axis=axis), axis=axis)
  ext = jnp.concatenate((2 * left_end - left_ext,
                         x,
                         2 * right_end - right_ext),
                         axis=axis)
  return ext


def _spectral_helper(x: Array, y: ArrayLike | None, fs: ArrayLike = 1.0,
                     window: str = 'hann', nperseg: int | None = None,
                     noverlap: int | None = None, nfft: int | None = None,
                     detrend_type: bool | str | Callable[[Array], Array] = 'constant',
                     return_onesided: bool = True, scaling: str = 'density',

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Reduce n to at most x.shape[axis]-1 (for stft, reduce nperseg to <= 2*(len-1) or pad the signal first)
  2. Pad the signal with jnp.pad before extending if the extension length is required
  3. Pass boundary=None to skip extension in spectral functions

Example fix

// before
f, t, Z = jax.scipy.signal.stft(x, nperseg=256, boundary='even')  # len(x)=100
// after
f, t, Z = jax.scipy.signal.stft(x, nperseg=64, boundary='even')
# or: x = jnp.pad(x, (156, 156)) first
Defensive patterns

Strategy: validation

Validate before calling

n = min(n, x.shape[axis] - 1)  # clamp extension length
# or pad: x = jnp.pad(x, [(n, n)] if x.ndim == 1 else [(0,0)]*(x.ndim-1) + [(n, n)])

Prevention

When it happens

Trigger: odd_ext(x, n=10) on an axis of length 8; stft/csd with boundary='even' where nperseg//2 exceeds half the signal length.

Common situations: Short signals with large nperseg in STFT; segment-size configs tuned on longer recordings then applied to short clips.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/eb6e8ace61030e53. Report an issue: GitHub.