The primary way to use torchkbnufft is through PyTorch modules. You initialize a module (like KbNufft) with your imaging setup properties (e.g., im_size). The module handles Kaiser-Bessel kernel calculation and interpolation parameters. You then pass your image data and trajectory tensors to the module. NUFFT operations are wrapped in torch.autograd.Function to support backpropagation and training.
import torch
import torchkbnufft as tkbn
import numpy as np
from skimage.data import shepp_logan_phantom
x = shepp_logan_phantom().astype(complex)
im_size = x.shape
# Prepare image tensor: (batch, coil, ny, nx)
x = torch.tensor(x).unsqueeze(0).unsqueeze(0).to(torch.complex64)
klength = 64
ktraj = np.stack((np.zeros(64), np.linspace(-np.pi, np.pi, klength)))
# Prepare trajectory tensor: (dim, klength)
ktraj = torch.tensor(ktraj).to(torch.float)
nufft_ob = tkbn.KbNufft(im_size=im_size)
# Compute k-space data
kdata = nufft_ob(x, ktraj)