{"record":{"id":"eb6e8ace61030e53","repo":"jax-ml/jax","slug":"the-extension-length-n-n-is-too-big-it-must-n","errorCode":null,"errorMessage":"The extension length n ({n}) is too big. It must not exceed x.shape[axis]-1, which is {x.shape[axis] - 1}.","messagePattern":"The extension length n \\((.+?)\\) is too big\\. It must not exceed x\\.shape\\[axis\\]-1, which is (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/signal.py","lineNumber":601,"sourceCode":"  else:\n    return jnp_fft.rfft(result.real, n=nfft)\n\n\ndef odd_ext(x: Array, n: int, axis: int = -1) -> Array:\n  \"\"\"Extends `x` along with `axis` by odd-extension.\n\n  This function was previously a part of \"scipy.signal.signaltools\" but is no\n  longer exposed.\n\n  Args:\n    x : input array\n    n : the number of points to be added to the both end\n    axis: the axis to be extended\n  \"\"\"\n  if n < 1:\n    return x\n  if n > x.shape[axis] - 1:\n    raise ValueError(\n        f\"The extension length n ({n}) is too big. \"\n        f\"It must not exceed x.shape[axis]-1, which is {x.shape[axis] - 1}.\")\n  left_end = lax.slice_in_dim(x, 0, 1, axis=axis)\n  left_ext = jnp.flip(lax.slice_in_dim(x, 1, n + 1, axis=axis), axis=axis)\n  right_end = lax.slice_in_dim(x, -1, None, axis=axis)\n  right_ext = jnp.flip(lax.slice_in_dim(x, -(n + 1), -1, axis=axis), axis=axis)\n  ext = jnp.concatenate((2 * left_end - left_ext,\n                         x,\n                         2 * right_end - right_ext),\n                         axis=axis)\n  return ext\n\n\ndef _spectral_helper(x: Array, y: ArrayLike | None, fs: ArrayLike = 1.0,\n                     window: str = 'hann', nperseg: int | None = None,\n                     noverlap: int | None = None, nfft: int | None = None,\n                     detrend_type: bool | str | Callable[[Array], Array] = 'constant',\n                     return_onesided: bool = True, scaling: str = 'density',","sourceCodeStart":583,"sourceCodeEnd":619,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/signal.py#L583-L619","documentation":"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.","triggerScenarios":"odd_ext(x, n=10) on an axis of length 8; stft/csd with boundary='even' where nperseg//2 exceeds half the signal length.","commonSituations":"Short signals with large nperseg in STFT; segment-size configs tuned on longer recordings then applied to short clips.","solutions":["Reduce n to at most x.shape[axis]-1 (for stft, reduce nperseg to <= 2*(len-1) or pad the signal first)","Pad the signal with jnp.pad before extending if the extension length is required","Pass boundary=None to skip extension in spectral functions"],"exampleFix":"// before\nf, t, Z = jax.scipy.signal.stft(x, nperseg=256, boundary='even')  # len(x)=100\n// after\nf, t, Z = jax.scipy.signal.stft(x, nperseg=64, boundary='even')\n# or: x = jnp.pad(x, (156, 156)) first","handlingStrategy":"validation","validationCode":"n = min(n, x.shape[axis] - 1)  # clamp extension length\n# or pad: x = jnp.pad(x, [(n, n)] if x.ndim == 1 else [(0,0)]*(x.ndim-1) + [(n, n)])","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep nperseg <= 2 * signal_length - 2 when using boundary extensions","Pad short clips before spectral analysis"],"tags":["jax","scipy","stft","signal-extension","shape-validation"],"backgroundTag":"segment-size-exceeds-data-length","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}