FFTW:用于3D数据的2D傅里叶变换的输出阵列的大小

Hem*_*mer 4 c fftw

我有一个尺寸为Nx*Ny*Nz的真实3D阵列,并希望使用FFTW对每个z值进行 2D傅立叶变换.这里的z索引是内存中变化最快的.目前,以下代码按预期工作:

int Nx = 16; int Ny = 8; int Nz = 3;

// allocate memory
const int dims = Nx * Ny * Nz;

// input data (pre Fourier transform)
double *input = fftw_alloc_real(dims);              

// why is this the required output size?
const int outdims = Nx * (Ny/2 + 1) * Nz;           
// we want to perform the transform out of place
// (so seperate array for output)
fftw_complex *output = fftw_alloc_complex(outdims);    


// setup "plans" for forward and backward transforms
const int rank = 2; const int howmany = Nz;
const int istride = Nz; const int ostride = Nz;
const int idist = 1; const int odist = 1;
int n[] = {Nx, Ny};
int *inembed = NULL, *onembed = NULL;


fftw_plan fp = fftw_plan_many_dft_r2c(rank, n, howmany,
        input, inembed, istride, idist,
        output, onembed, ostride, odist,
        FFTW_PATIENT);

fftw_plan bp = fftw_plan_many_dft_c2r(rank, n, howmany,
        output, onembed, ostride, odist,
        input, inembed, istride, idist,
        FFTW_PATIENT);
Run Code Online (Sandbox Code Playgroud)

根据我的理解,转换长度为N的1D序列需要(N/2 + 1)个复数值,那么为什么上面的代码outdims = (Nx/2 + 1)*(Ny/2 + 1)*Nz会因为我设置为2D变换的预期而崩溃?

其次,我认为我可以qx = 0 to Nx/2使用以下内容从(包含)访问模式的实部和虚部:

#define outputRe(qx,qy,d) ( output[(d) + Nz * ((qy) + (Ny/2 + 1) * (qx))][0] )
#define outputIm(qx,qy,d) ( output[(d) + Nz * ((qy) + (Ny/2 + 1) * (qx))][1] )
Run Code Online (Sandbox Code Playgroud)

编辑:完整的代码Makefile为那些想玩的人.假设安装了fftw和gsl.

编辑2:如果我理解正确,索引(允许正负频率)应该是(可能对宏来说太乱了!):

#define outputRe(qx,qy,d) ( output[(d) + Nz * ((qy) + (Ny/2 + 1) * ( ((qx) >= 0) ? (qx) : (Nx + (qx)) ) )  ][0] )
#define outputIm(qx,qy,d) ( output[(d) + Nz * ((qy) + (Ny/2 + 1) * ( ((qx) >= 0) ? (qx) : (Nx + (qx)) ) )  ][1] )

for (int qx = -Nx/2; qx < Nx/2; ++qx)
    for (int qy = 0; qy <= Ny/2; ++qy)
        outputRe(qx, qy, d) = ...
Run Code Online (Sandbox Code Playgroud)

其中outputRe(-Nx/2, qy, d)指向相同的数据outputRe(Nx/2, qy, d).在实践中,我可能只是循环第一个索引并转换为频率,而不是反过来!

Hem*_*mer 5

为了帮助澄清(专注于2D,因为它很容易扩展到3D数据的2D变换):

存储

一个Nx * Ny阵列需要Nx * (Ny / 2 + 1)变换傅立叶后复杂的元素.

首先,在y方向上,可以从复共轭对称性(来自变换实序列)来重构负频率.然后y模式ky0 to Ny/2包含运行.所以对于y我们需要Ny/2 + 1复杂的价值观.

接下来,我们在x方向上进行变换,在这里我们不能使用相同的对称假设,因为我们正在对复值y值进行处理.因此,我们必须包括正负频率,因此x模式kx-Nx/2 to Nx/2包含性开始.但是kx = -Nx/2并且kx = Nx/2是等效的,所以只存储一个(见这里).所以对于x我们需要Nx复杂的值.

访问

由于tir38指出x变换后的索引从0运行到Nx-1,但这并不意味着模式kx从0运行到Nx-1.FFTW在阵列的前半部分包含正频率,然后在后半部分包含负频率(以相反的顺序),如:

kx = 0, 1, 2, ..., Nx/2, -Nx/2 + 1, ..., -2, -1
Run Code Online (Sandbox Code Playgroud)

我们可以通过两种方式来考虑访问这些元素.首先,因为tir38建议我们可以按顺序循环并kx从索引中计算出模式:

for (int i = 0; i < Nx; i++)
{
    // produces the list of kxs above
    int kx = (i <= Nx/2) ? i : i - Nx;

    // here we index with i, but with the knowledge that the mode is kx
    outputRe(i, ...) = some function of kx 
}
Run Code Online (Sandbox Code Playgroud)

或者我们可以遍历模式kx并转换为索引:

for (int kx = -Nx/2; kx < Nx/2; kx++)
{
    // work out index from mode kx
    int i = (kx >= 0) ? i : Nx + i;

    // here we index with i, but with the knowledge that the mode is kx
    outputRe(i, ...) = some function of kx 
}
Run Code Online (Sandbox Code Playgroud)

可以在此处找到两种类型的索引以及其余代码.