MATLAB 行代码在 C++ 和使用 FFTW 中的确切等价物是什么?
fftshift(fft(x,4096)));
注意:X 是 4096 个双精度数据的数组。
现在我在 c++ 和 FFTW 中使用这些代码行来计算 fft
int n = 4096
fftw_complex *x;
fftw_complex *y;
x = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * n);
y = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * n);
for (int i=0; i<n; i++)
{
x[i][REAL] = MyDoubleData[i];
x[i][IMAG] = 0;
}
fftw_plan plan = fftw_plan_dft_1d(n, x, y, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
fftw_cleanup();
它相当于MATLAB中的FFT函数。FFTW库中的FftShift是否有任何等效的功能?
您提供的 FFTW 函数调用等同于 fft(x,4096)
。如果x是真实的,matlab知道给你共轭对称FFT(我认为)。 如果要使用 FFTW 执行此操作,则需要使用r2c
和c2r
函数(实数到复杂/复杂到实数)。
你必须自己做转变。您可以进行直接替换(性能不佳,但应该直观)
for (int i=0; i<n; i++)
{
fftw_complex tmp;
int src = i;
int dst = (i + n/2 - 1) % n;
tmp=y[src];
y[src]=x[dst];
y[dst]=tmp;
}
或者使用几个memcpy(和/或memmove)或修改您的输入数据
fftw 的输出基于以下频率序列格式存储:
[0....N-1]
其中 N 是频率数,是烤箱。并 fftshift 将其更改为:
[-(N-1)/2,..., 0..., (N-1)/2]
但您应该注意,FFTW 输出等效于:
[0,.., N-1]
与[0,...,(N-1)/2,-(N-1)/2,...,-1]
相同
这意味着在DFT中,频率-i
与N-i
相同。