ai.onnx.STFT
ai.onnx · standard ONNX operator · ONNX opset ≥ 17
Description
Computes the short-time Fourier transform (STFT) of a rank-3 real or complex signal by sliding a complete frame over the input and applying the DFT. The frameStep scalar represents the required positive frame_step input. At least one of window or frameLength is required; when both are supplied, their lengths must match. The output contains the real and imaginary components of every frequency bin for each complete frame.
See the ONNX STFT spec for the reference semantics.
Differences from the ONNX spec
- Input
frame_step: Passed as theframeStepruntime argument instead of a tensor input. - Input
frame_length: Passed as theframeLengthruntime argument instead of a tensor input.
Inputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
signal |
T |
3 |
— | Real input signal with shape (batch_size, signal_length, 1), or complex input with shape (batch_size, signal_length, 2). |
required |
window |
T |
1 |
— | Optional 1-D window tensor with shape (window_length) to multiply by each frame before the DFT. |
optional |
Outputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
output |
T |
4 |
derived | STFT result with shape (batch_size, frames, dft_unique_bins, 2) in one-sided mode or (batch_size, frames, frame_length, 2) in two-sided mode, with real and imaginary parts in the last dimension. frames = floor((signal_length - frame_length) / frame_step) + 1; partial trailing frames are not emitted. |
required |
Runtime arguments
| Name | Kind | Upstream attribute | Description | Presence |
|---|---|---|---|---|
frameStep |
i32 |
frame_step |
Positive number of signal positions between the starts of consecutive frames: ONNX's frame_step input, passed as a scalar. |
required |
frameLength |
i32 |
frame_length |
Optional number of signal values in each frame: ONNX's frame_length input, passed as a scalar. Must equal the window length when both are supplied. |
optional |
Attributes
Default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
onesided |
1 |
When 1 (default), returns floor(n_fft / 2) + 1 non-redundant frequency bins with indices [0, ..., floor(n_fft / 2)] for a real-valued input (RFFT); set to 0 to return the full spectrum. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32 |
Implementation variants
One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
rank3_window_tiled_real_roots— Odd-length real windowed transforms reuse one paired root table across batches, frames and frequency tiles, preserving increasing-sample compensated accumulation.rank3_window_fft— Compensated shared FFT: radix-2 for power-of-two windows; staged small-radix butterflies when their factor cost is below that of direct-tail summation. Workgroup width is bounded by the device invocation and X-dimension limits.
Files
metadata.json— kernel metadata (id, file digests, manifest spec, provenance)manifest.json— the op contract (source of truth)test.json— correctness casesbench.json— benchmark casesdft-paired-roots.wgsl.jinjadft-tiled-real.wgsl.jinjafft-radix2-dit-shared-inplace.wgsl.jinjafft-radix2-dit-storage-pass.wgsl.jinjafft-radix2-dit-storage.wgsl.jinjafft-real-packed-stockham-shared.wgsl.jinjafft-residual-shared.wgsl.jinjafft-stockham-shared.wgsl.jinjafft-stockham-twiddles.wgsl.jinjastft-general.wgsl.jinja
Use with @huggingface/kernels
npm install --save-exact @huggingface/kernels@0.0.1-preview.4
Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version.
It follows the v1 branch as fixes land. To pin exact artifact bytes, pass a 40-character commit revision instead of version.
import { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/ai.onnx.STFT", { version: 1 });
const signalData = Float32Array.of(1, 2, 3, 4, 5, 6);
const { output } = await kernel({
signal: { data: signalData, shape: [1, 6, 1] },
frameStep: 2,
frameLength: 4,
});
console.log({ output });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.