简体   繁体   English

关于MATLAB中的fftshift和fft2的困惑

[英]Confusion about fftshift and fft2 in MATLAB

I'm mighty confused about how to correctly apply the FFT transform and its inverse in Matlab. 我对如何在Matlab中正确应用FFT变换及其逆变得非常困惑。 I have a program where I need to 我有一个我需要的程序

  1. apply FFT2 to a matrix of size 4x32 (corresponding to modes m=-1:2 , n=-15:16 ) 将FFT2应用于大小为4x32的矩阵(对应于模式m=-1:2n=-15:16
  2. do some processing, which leads to a coefficient matrix for another function, whose Fourier coefficients relate to the first set of data by a simple algebraic (component-wise) formula 进行一些处理,导致另一个函数的系数矩阵,其傅立叶系数通过简单的代数(分量)公式与第一组数据相关
  3. Use some properties of the two functions that lets me calculate what I need by just summing up an expression on the form 2*abs(A_n)*cos(phi+n*theta+alpha_n) where A_n is the nth coefficient of the m=1 mode and alpha_n = arg(A_n) . 使用两个函数的一些属性,让我通过在表格2*abs(A_n)*cos(phi+n*theta+alpha_n)上求和表达式来计算我需要的东西,其中A_nm=1的第n个系数m=1模式和alpha_n = arg(A_n)

I have experimented a little with the FFT2 function and tried to understand how it arranges its output. 我已经尝试了一点FFT2功能,并尝试了解它如何安排其输出。 From what I understand (from sources in my course literature), the coefficients will be ordered as is illustrated by the following script: 根据我的理解(来自我的课程文献中的来源),系数将按以下脚本所示进行排序:

>>m = -1:2; n = -7:8;
>>[N,M] = meshgrid(n,m); 
>>MN = M; MN(:,:,2) = N; 
>>asfft = @(X) [X(2:4,8:16,:) X(2:4,1:7,:); X(1,8:16,:) X(1,1:7,:)];
>>asfft(MN)
ans(:,:,1) =
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1
ans(:,:,2) =
     0     1     2     3     4     5     6     7     8    -7    -6    -5    -4    -3    -2    -1
     0     1     2     3     4     5     6     7     8    -7    -6    -5    -4    -3    -2    -1
     0     1     2     3     4     5     6     7     8    -7    -6    -5    -4    -3    -2    -1
     0     1     2     3     4     5     6     7     8    -7    -6    -5    -4    -3    -2    -1

where asfft reorders the indices in the same way I believe fft2 does, but does nothing else. asfft以我相信fft2方式重新排序索引,但没有别的。 In other words, each index is ordered from 0 up to the max, then from the min to -1. 换句话说,每个索引的排序从0到最大,然后从min到-1。 According to the documentation, I should be able to rearrange this so I get 0 in the middle by using fftshift , but it's not giving me the output I expect. 根据文档,我应该可以重新排列这个,所以我通过使用fftshift在中间得到0,但它没有给我我期望的输出。 Instead, I get this: 相反,我明白了:

>> fftshift(asfft(MN))
ans(:,:,1) =
     8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7
     8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7
     8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7
     8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7
ans(:,:,2) =
     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

As you can see, the max is on the wrong side of the spectrum - instead of -7 -6 ... -1 0 1 ... 8 and -1 0 1 2 I have 8 -7 6 ... and 2 -1 0 1 . 正如你所看到的,最大值是在频谱的错误一侧 - 而不是-7 -6 ... -1 0 1 ... 8-1 0 1 2我有8 -7 6 ...2 -1 0 1 This is fatal for me, since to be able to do the calculation described in 3 above, I need to know the indices of the respective coefficients. 这对我来说是致命的,因为为了能够进行上面3中描述的计算,我需要知道各个系数的指数。 (The two layers are also switched, but that doesn't matter for me since I'm only going to do this on MxN matrices, not on Nd-arrays, later.) (这两个层也被切换,但这对我来说无关紧要,因为我只会在MxN矩阵上执行此操作,而不是在Nd阵列上执行此操作,稍后。)

Why is this? 为什么是这样? What am I doing wrong here? 我在这做错了什么?

Considering the simple 1D case first, fft gives you: 首先考虑简单的1D案例, fft为您提供:

[X(0) X(1) X(2) ... X(N/2-1) X(-N/2) X(-N/2+1) ... X(-1)]

where I'm using X(k) to denote elements of the mathematical DFT . 我在哪里使用X(k)来表示数学DFT的元素。

All fftshift does is rotate these by N/2 , so you end up with: 所有fftshift都会将它们旋转N/2 ,所以你最终得到:

[X(-N/2) X(-N/2+1) ... X(-1) X(0) X(1) X(2) ... X(N/2-1)]

ie linear order. 即线性顺序。 1 1

In the multi-dimensional case, fftshift simply applies this rotation in all dimensions. 在多维情况下, fftshift只是在所有维度上应用此旋转。


1. Although since by definition, X(k) == X(N+k) for a DFT, the unrotated vector is also in "linear" order! 虽然根据定义,对于DFT, X(k) == X(N+k) ,未旋转的矢量也是“线性”顺序!

taking a queue from Oli, the problem is that you're not parsing it in the middle: 从Oli排队,问题是你没有在中间解析它:

so for asfft = @(X) [X(2:4,8:16,:) X(2:4,1:7,:); X(1,8:16,:) X(1,1:7,:)]; 所以对于asfft = @(X) [X(2:4,8:16,:) X(2:4,1:7,:); X(1,8:16,:) X(1,1:7,:)]; asfft = @(X) [X(2:4,8:16,:) X(2:4,1:7,:); X(1,8:16,:) X(1,1:7,:)];

size(8:16)
ans =
     1     9
size(1:7)
ans =
     1     7

so do: 所以:

asfft = @(X) [X(3:4,9:16,:) X(3:4,1:8,:); X(1:2,9:16,:) X(1:2,1:8,:)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM