Is there fft or fftshift in pytorch?

micklexqg: <p>is there fft or fftshift in pytorch?</p>

a_d: <p>There is <code>torch.fft </code> available in PyTorch.<br>
similarly <code>rfft</code> and the inverse are also available</p>

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

thomasjo: <p>Hey <a class="mention" href="/u/micklexqg">@micklexqg</a>,</p>
<p>If you check out the <a href="https://pytorch.org/docs/stable/generated/torch.fft.html#torch.fft" rel="noopener nofollow ugc"><code>torch.fft</code></a> documentation you will discover that it expects a tensor with <code>signal_dim + 1</code> 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.</p>
<p>I am going to assume you only have real-valued input, in which case you should <a href="https://pytorch.org/docs/stable/generated/torch.fft.html#torch.rfft" rel="noopener nofollow ugc"><code>torch.rfft</code></a> instead. This function expects an input tensor with <code>signal_dim</code> dimensions. Note also that the second argument to both <code>torch.fft</code> and <code>torch.rfft</code> is the <code>signal_dim</code>, which must be set to either 1, 2, or 3.</p>
<p>I have not tested this, but the following version of your example should work as expected;</p>
<pre><code class="lang-python">FL = torch.rfft(f, 3) / torch.sqrt(M * N)  # f is a torch.FloatTensor[1, 3, 400, 300]
</code></pre>
<p>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.</p>

thomasjo: <p>One little side note to my reply above is that <code>torch.rfft</code> (and <code>torch.fft</code>) 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.</p>

a_d: <p>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 <code>[1, 22, 313]</code> representing 22 channels and 313 timesteps, then one way to go ahead would represent every number in the form of <code>a + ib</code> therefore making it of shape <code>[1, 22, 313, 2]</code> where the last dimension suggest 2 parts to every number in the datapoint.</p>

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

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

thomasjo: <aside class="quote no-group" data-username="micklexqg" data-post="7" data-topic="98302">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/micklexqg/48/4748_2.png" class="avatar"> micklexqg:</div>
<blockquote>
<p>what doesthe parameter 3 mean for ?</p>
</blockquote>
</aside>
<p>The <code>3</code> is the same as <code>signal_dim=3</code>, and it refers to the number of dimensions in your tensor. In your case, that is <code>3</code> 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 <code>signal_dim=2</code> because in that case you would only need the height and width dimensions. Put differently, then your tensor would have a shape of <code>[1, 400, 300]</code>.</p>
<aside class="quote no-group" data-username="micklexqg" data-post="8" data-topic="98302">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/micklexqg/48/4748_2.png" class="avatar"> micklexqg:</div>
<blockquote>
<p>pip install --upgrade pytorch</p>
</blockquote>
</aside>
<p>The pip package is called <code>torch</code>, not <code>pytorch</code>. Try using the following instead</p>
<pre><code class="lang-plaintext">pip install --upgrade torch
</code></pre>

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

micklexqg: <p>sorry for unclear statement. not failed, but no meaning.<br>
the results is : <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/8/28a526b056d2ec72d9d3e374862321f1a7b8bf85.png" data-download-href="https://discuss.pytorch.org/uploads/default/28a526b056d2ec72d9d3e374862321f1a7b8bf85" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/8/28a526b056d2ec72d9d3e374862321f1a7b8bf85.png" alt="image" data-base62-sha1="5NyVSi19xJwCjjuNM7OaG3Apvil" width="690" height="177" data-dominant-color="1A1818"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">962×247 6.51 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

thomasjo: <p>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.</p>
<p>You can monitor the progress of v1.7.0 here: <a href="https://github.com/pytorch/pytorch/issues/45592" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/issues/45592</a></p>
<p>Out of curiosity, where did you find information about a “more simple fft api”? No fundamental changes will be made to <code>torch.fft</code>, <code>torch.rfft</code> and similar functions in 1.7.0 or 1.8.0. There are several new <a href="https://github.com/pytorch/pytorch/pull/44877" rel="noopener nofollow ugc">helper functions</a> and <a href="https://github.com/pytorch/pytorch/pull/45164" rel="noopener nofollow ugc">additions</a> currently being worked, but that is about the extent of changes being made to the spectral functions in PyTorch in the foreseeable future.</p>
<p>Notably, <code>torch.fftshift</code> is being worked on in the helper function PR: <a href="https://github.com/pytorch/pytorch/pull/44877" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/pull/44877</a></p>

micklexqg: <p>en…“more simple” just means the calling way is a little simpler. not to amend the input for fft:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/6/86b52f175546eedac93998242f751574edc9c7e8.png" data-download-href="https://discuss.pytorch.org/uploads/default/86b52f175546eedac93998242f751574edc9c7e8" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/6/86b52f175546eedac93998242f751574edc9c7e8.png" alt="image" data-base62-sha1="jdGahzD38KLq6sAxT9Dnm52zMA8" width="690" height="316" data-dominant-color="F4F5F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">853×391 9.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
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.)<br>
extra:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/5/35b13027c5ee1dfe2d23dd5a625b22777a667737.png" data-download-href="https://discuss.pytorch.org/uploads/default/35b13027c5ee1dfe2d23dd5a625b22777a667737" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/5/35b13027c5ee1dfe2d23dd5a625b22777a667737.png" alt="image" data-base62-sha1="7EYVDM1dWUnEAOqOy9ddc4EFUcT" width="690" height="291" data-dominant-color="F5F5F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">870×368 11.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
is the demo for fftn right?<br>
the input should be x but not be t?</p>

thomasjo: <p>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. <em>Note that the nightly builds are not stable, so you probably should not use them for anything important.</em> If you want to install the nightly build and get access to the new (and simplified) FFT API, use <a href="https://pytorch.org/get-started/locally/" rel="noopener nofollow ugc">https://pytorch.org/get-started/locally/</a> and select <strong>“Preview (Nightly)”</strong>.</p>
<p>If you are running a Linux distro with a GPU, you can install the latest nightly build with</p>
<pre><code class="lang-nohighlight">pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html
</code></pre>
<p>or if you do not want GPU support use</p>
<pre><code class="lang-nohighlight">pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
</code></pre>
<p>For any other OS or specific CUDA versions, use the configuration tool on <a href="https://pytorch.org/get-started/locally/" rel="noopener nofollow ugc">https://pytorch.org/get-started/locally/</a>.</p>