compute.ndarray.resample¶
Summary¶
compute.ndarray.resample samples selected axes at coordinates you define. Use it when changing the physical spacing or origin of a sampled grid. On each selected axis, output index d reads source coordinate d * scale + offset, with integer coordinates at source sample centers.
Syntax¶
resample<T extends CPUndarray | GPUndarray>(a: T, options: NdarrayResampleOptions): T
const b = nd.resample(a, { axes: 0, size: 4, scale: 0.5 });
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live input array. |
options |
NdarrayResampleOptions |
Yes | Grid coordinates and interpolation settings. |
Returns¶
T - C-contiguous result in the input residency. Destroy it when finished unless you supplied a GPU out.
Type Details¶
type NdarrayResampleOptions = NdarrayOperationOptions & {
axes: number | readonly number[];
size: number | readonly number[];
scale: number | readonly number[];
offset?: number | readonly number[];
method?: NdarrayResizeMethod;
antialias?: boolean;
cubicCoefficient?: number;
boundary?: NdarrayResizeBoundary | readonly NdarrayResizeBoundary[];
value?: number | ComplexScalar;
dtype?: DType;
};
Pass at least one distinct axis. size, scale, offset and boundary may be scalars or lists in the same order as the axes. Sizes are non-negative integers that fit in u32. Sampling an empty axis requires zero output samples. scale and offset must be finite. offset defaults to zero. Negative scale reverses the grid and zero scale repeats one coordinate.
The default method is linear, the default boundary is edge, and the output dtype defaults to the input dtype. Cubic's coefficient defaults to -0.75. Nearest selects the greater index on a half-sample tie. antialias defaults to false and is available for linear and cubic, where it widens the filter for abs(scale) > 1. value defaults to zero. A constant boundary uses it for outside taps, and "renormalize" uses it when no positive weight remains after discarding outside taps.
Storage, conversion, dtype and GPU encoder rules follow compute.ndarray.resize. A non-empty sampled coordinate plus its filter reach must stay within signed 32-bit indexing.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [3], [0, 10, 20]);
const b = nd.resample(a, { axes: 0, size: 2, scale: 1, offset: 0.5 });
console.log(b.data()); // [5, 15]
b.destroy();
a.destroy();
Notes¶
For physical grids with sample centers at origin + index * spacing, use outSpacing / inSpacing for scale and (outOrigin - inOrigin) / inSpacing for offset. For a lattice with ndarray data in [z, y, x] order, translate its [x, y, z] spacings and origins into the selected ndarray axis order.