在 C++ 中使用 FFTW 的 MATLAB 行代码的确切等价物是什么?
fftshift(fft(x,4096)));
注意:X是4096个double数据的数组。
现在我在 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
and c2r
functions (真实到复杂/复杂到真实)。
您必须自己轮类。可以直接代入(性能较差,但应该直观)
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)或 modify your input data
关于c++ - FFTW库c++中matlab的FFT和FFTShift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41779293/