c++ - FFTW库c++中matlab的FFT和FFTShift

标签 c++ matlab fftw

在 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/

相关文章:

matlab - 如何创建一个表来保存 MATLAB 迭代中的数据?

MATLAB:如何求解线性系统模 m

c++ - 如何在加载的样本上使用 FFTW?

c++ - CRTP 中的模板化派生类(奇怪的重复模板模式)

c++ - 多线程和多进程的性能差异

Linux 与 Windows 在 MatLab 中执行 lsqcurvefit 和 importdata

c++ - 使用 Visual Studio 2013 编译 FFTW 3.3.4

以void *和其他指针类型为参数的C++多态函数: is it considered ambiguous?

c++ - 需要 map 但只需要知道是否包含?

c++ - 搜索基于 3D fft 卷积的库