Skip to content

Instantly share code, notes, and snippets.

@speedcell4
Last active July 20, 2021 10:29
Show Gist options
  • Save speedcell4/21ab246930928d15beed079922717eaf to your computer and use it in GitHub Desktop.
Save speedcell4/21ab246930928d15beed079922717eaf to your computer and use it in GitHub Desktop.
import torch
from torch import Tensor
from torch.testing import assert_close
def fft(tensor: Tensor, pi: Tensor = torch.acos(torch.tensor(-1.))) -> Tensor:
n = tensor.size()[-1]
index = torch.arange(n, dtype=torch.float32)
fourier = torch.exp(index[:, None] * index[None, :] * pi * -2j / n)
return tensor.to(dtype=fourier.dtype) @ fourier
a = torch.randn((1, 4))
assert_close(fft(a), torch.fft.fft(a, dim=-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment