FFT

namespace fft

Functions

Tensor fft(const Tensor &input, int64_t n = -1, int axis = -1, const std::string &norm = "backward")

Compute the 1-dimensional discrete Fourier Transform.

Parameters:
  • input – Input tensor (can be real or complex)

  • n – Length of the transformed axis (default: input size along axis)

  • axis – Axis over which to compute the FFT (default: -1, last axis)

  • norm – Normalization mode: “backward” (default), “forward”, “ortho”

Returns:

Complex tensor with FFT result

Tensor ifft(const Tensor &input, int64_t n = -1, int axis = -1, const std::string &norm = "backward")

Compute the 1-dimensional inverse discrete Fourier Transform.

Parameters:
  • input – Input tensor (typically complex)

  • n – Length of the transformed axis

  • axis – Axis over which to compute the inverse FFT

  • norm – Normalization mode

Returns:

Complex tensor with inverse FFT result

Tensor rfft(const Tensor &input, int64_t n = -1, int axis = -1, const std::string &norm = "backward")

Compute the 1-dimensional FFT of real input.

For real input of length n, output has length n//2+1 (Hermitian symmetry).

Parameters:
  • input – Real input tensor

  • n – Length of the FFT

  • axis – Axis over which to compute the FFT

  • norm – Normalization mode

Returns:

Complex tensor with rfft result

Tensor irfft(const Tensor &input, int64_t n = -1, int axis = -1, const std::string &norm = "backward")

Compute the inverse FFT for real output.

Parameters:
  • input – Complex input tensor (typically from rfft)

  • n – Length of the output (required if input was zero-padded)

  • axis – Axis over which to compute the inverse FFT

  • norm – Normalization mode

Returns:

Real tensor with irfft result

Tensor fft2(const Tensor &input, const std::vector<int64_t> &s = {}, const std::vector<int> &axes = {-2, -1}, const std::string &norm = "backward")

Compute the 2-dimensional discrete Fourier Transform.

Parameters:
  • input – Input tensor

  • s – Shape of the output along transformed axes

  • axes – Axes over which to compute the FFT (default: last two)

  • norm – Normalization mode

Returns:

Complex tensor with 2D FFT result

Tensor ifft2(const Tensor &input, const std::vector<int64_t> &s = {}, const std::vector<int> &axes = {-2, -1}, const std::string &norm = "backward")

Compute the 2-dimensional inverse discrete Fourier Transform.

Tensor rfft2(const Tensor &input, const std::vector<int64_t> &s = {}, const std::vector<int> &axes = {-2, -1}, const std::string &norm = "backward")

Compute the 2-dimensional FFT of real input.

Tensor irfft2(const Tensor &input, const std::vector<int64_t> &s = {}, const std::vector<int> &axes = {-2, -1}, const std::string &norm = "backward")

Compute the 2-dimensional inverse FFT for real output.

Tensor fftn(const Tensor &input, const std::vector<int64_t> &s = {}, const std::vector<int> &axes = {}, const std::string &norm = "backward")

Compute the N-dimensional discrete Fourier Transform.

Tensor ifftn(const Tensor &input, const std::vector<int64_t> &s = {}, const std::vector<int> &axes = {}, const std::string &norm = "backward")

Compute the N-dimensional inverse discrete Fourier Transform.

Tensor fftshift(const Tensor &input, const std::vector<int> &axes = {})

Shift the zero-frequency component to the center of the spectrum.

Parameters:
  • input – Input tensor

  • axes – Axes over which to shift (default: all axes)

Returns:

Shifted tensor

Tensor ifftshift(const Tensor &input, const std::vector<int> &axes = {})

Inverse of fftshift.

Tensor fftfreq(int64_t n, double d = 1.0, DType dtype = DType::Float64, Device device = Device::CPU)

Return the Discrete Fourier Transform sample frequencies.

Parameters:
  • n – Window length

  • d – Sample spacing (default: 1.0)

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length n containing sample frequencies

Tensor rfftfreq(int64_t n, double d = 1.0, DType dtype = DType::Float64, Device device = Device::CPU)

Return the Discrete Fourier Transform sample frequencies for rfft.

Parameters:
  • n – Window length

  • d – Sample spacing

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length n//2+1 containing sample frequencies

Tensor hann_window(int64_t M, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Hann (Hanning) window.

w[n] = 0.5 - 0.5 * cos(2*pi*n / (M-1))

Parameters:
  • M – Number of points in the output window

  • periodic – If true, returns a periodic window (for spectral analysis)

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor hamming_window(int64_t M, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Hamming window.

w[n] = 0.54 - 0.46 * cos(2*pi*n / (M-1))

Parameters:
  • M – Number of points in the output window

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor blackman_window(int64_t M, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Blackman window.

w[n] = 0.42 - 0.5*cos(2*pi*n/(M-1)) + 0.08*cos(4*pi*n/(M-1))

Parameters:
  • M – Number of points in the output window

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor bartlett_window(int64_t M, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Bartlett (triangular) window.

Parameters:
  • M – Number of points in the output window

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor kaiser_window(int64_t M, double beta = 12.0, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Kaiser window.

Parameters:
  • M – Number of points in the output window

  • beta – Shape parameter for the window (default: 12.0)

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor cosine_window(int64_t M, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Cosine window.

w[n] = sin(pi * n / (M-1))

Parameters:
  • M – Number of points in the output window

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor exponential_window(int64_t M, double tau = 1.0, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Exponential (Poisson) window.

w[n] = exp(-|n - center| / tau)

Parameters:
  • M – Number of points in the output window

  • tau – Decay scale parameter (default: 1.0)

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor gaussian_window(int64_t M, double std = 1.0, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Gaussian window.

w[n] = exp(-0.5 * ((n - center) / std)^2)

Parameters:
  • M – Number of points in the output window

  • std – Standard deviation (default: 1.0)

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor nuttall_window(int64_t M, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

Nuttall window (4-term Blackman-Harris variant).

w[n] = a0 - a1*cos(2*pi*n/(M-1)) + a2*cos(4*pi*n/(M-1)) - a3*cos(6*pi*n/(M-1)) with a = {0.3635819, 0.4891775, 0.1365995, 0.0106411}

Parameters:
  • M – Number of points in the output window

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor general_cosine_window(int64_t M, const std::vector<double> &a, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

General cosine window.

w[n] = sum_k (-1)^k * a[k] * cos(2*pi*k*n / (M-1))

Parameters:
  • M – Number of points in the output window

  • a – Cosine coefficients

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor general_hamming_window(int64_t M, double alpha = 0.54, bool periodic = true, DType dtype = DType::Float32, Device device = Device::CPU)

General Hamming window.

w[n] = alpha - (1 - alpha) * cos(2*pi*n / (M-1))

Parameters:
  • M – Number of points in the output window

  • alpha – Window coefficient (default: 0.54 for Hamming)

  • periodic – If true, returns a periodic window

  • dtype – Output dtype

  • device – Output device

Returns:

1D tensor of length M containing the window

Tensor stft(const Tensor &input, int64_t n_fft, int64_t hop_length = -1, int64_t win_length = -1, const Tensor &window = Tensor(), bool center = true, const std::string &pad_mode = "reflect", bool normalized = false)

Compute the Short-Time Fourier Transform.

Parameters:
  • input – 1D or 2D real-valued signal (…, signal_length)

  • n_fft – FFT window size

  • hop_length – Number of samples between frames (default: n_fft/4)

  • win_length – Window length (default: n_fft)

  • window – Optional window tensor of length win_length

  • center – If true, pad signal on both sides with reflect padding

  • pad_mode – Padding mode when center=true (“reflect”)

  • normalized – If true, normalize output by 1/sqrt(n_fft)

Returns:

Complex tensor of shape (…, n_fft/2+1, n_frames)