Is there fft or fftshift in pytorch?

is there fft or fftshift in pytorch?

There is torch.fft available in PyTorch.
similarly rfft and the inverse are also available

thank you. i tried with:
FL = torch.fft(f, 2) / torch.sqrt(M * N) # (f is a torch.FloatTensor[1, 3, 400, 300])
but came across the error:
Expected an input tensor with a last dimension of size 2 representing real + imaginary components, but got input torch.FloatTensor[1, 3, 400, 300]

Hey @micklexqg,

If you check out the torch.fft documentation you will discover that it expects a tensor with signal_dim + 1 dimensions, where the last axis is expected to be of size 2. This is because the input is expected to be complex-valued. The last axis, therefore, needs to hold both the real and complex coefficients.

I am going to assume you only have real-valued input, in which case you should torch.rfft instead. This function expects an input tensor with signal_dim dimensions. Note also that the second argument to both torch.fft and torch.rfft is the signal_dim, which must be set to either 1, 2, or 3.

I have not tested this, but the following version of your example should work as expected;

FL = torch.rfft(f, 3) / torch.sqrt(M * N)  # f is a torch.FloatTensor[1, 3, 400, 300]

Although this tensor has 4 dimensions, the first is considered the batch dimension not considered part of the “signal dimensions”. This is noted in the documentation.

One little side note to my reply above is that torch.rfft (and torch.fft) returns a complex-valued tensor. In other words, the dimension of the output tensor will be greater than the input, and the last axis/dimension contains both the real and complex coefficients.

The last dimension of your tensor is always to be 2 representing the real and complex parts of a data point. Suppose I have a data point of shape [1, 22, 313] representing 22 channels and 313 timesteps, then one way to go ahead would represent every number in the form of a + ib therefore making it of shape [1, 22, 313, 2] where the last dimension suggest 2 parts to every number in the datapoint.

Thank you very much. the f is a rgb image, i want to compute its fft.
i just tried with FL = torch.rfft(f, 3) / torch.sqrt(M * N) .
it works well. i am a little puzzled. what doesthe parameter 3 mean for ?
rgb 3channels?

thank you. i just google again and find the latest version has more simple fft api.
but it is only on preview version. so how to use the preview version, pip install --upgrade pytorch does not work…

The 3 is the same as signal_dim=3, and it refers to the number of dimensions in your tensor. In your case, that is 3 because you have the first dimension with size 3 that represents RGB, and the second and third dimensions represent the height (400) and width (300) of your image. If you were working with a single-channel image you would use signal_dim=2 because in that case you would only need the height and width dimensions. Put differently, then your tensor would have a shape of [1, 400, 300].

The pip package is called torch, not pytorch. Try using the following instead

pip install --upgrade torch

thank you. quite explicit!
i also tried pip install --upgrade torch, still failed.
maybe pip install --upgrade torch would not work for the beta version and only for the released stable version…

sorry for unclear statement. not failed, but no meaning.
the results is :

Version 1.6.0 is the latest release of PyTorch. In all likelihood 1.8.0 will not be released until sometime next year. PyTorch 1.7.0 is being worked on right now, but is still a few weeks away from release.

You can monitor the progress of v1.7.0 here:

Out of curiosity, where did you find information about a “more simple fft api”? No fundamental changes will be made to torch.fft, torch.rfft and similar functions in 1.7.0 or 1.8.0. There are several new helper functions and additions currently being worked, but that is about the extent of changes being made to the spectral functions in PyTorch in the foreseeable future.

Notably, torch.fftshift is being worked on in the helper function PR:

en…“more simple” just means the calling way is a little simpler. not to amend the input for fft:

it looks that it works well even the input is not with a limit that the last dimension of the tensor is always to be 2 representing the real and complex parts of a data point. (and i am matlab user, i am a little lazy. in matlab, i just call right function for right shaped data, such as fft for 1-dim, fft2 for 2-dim… i need to use pytorch but am not familiar with it. i am learning.)

is the demo for fftn right?
the input should be x but not be t?

OK, I just noticed that the new FFT API will most likely be released in v1.7.0. It is currently available in the nightly releases of PyTorch. Note that the nightly builds are not stable, so you probably should not use them for anything important. If you want to install the nightly build and get access to the new (and simplified) FFT API, use and select “Preview (Nightly)”.

If you are running a Linux distro with a GPU, you can install the latest nightly build with

pip install --pre torch torchvision -f

or if you do not want GPU support use

pip install --pre torch torchvision -f

For any other OS or specific CUDA versions, use the configuration tool on