gpt4 book ai didi

c - 使用 FFTW 对 2D 复数数组进行 FFT

转载 作者:行者123 更新时间:2023-11-30 16:32:47 25 4
gpt4 key购买 nike

我在 C 中有一个大小为 1001(行)* 144(列)的 2D 双复数数组。我想对每一行应用 FFT,最后希望以 4096*144 格式输出。这里N点=4096。最后将结果与matlab输出进行比较。我正在使用著名的 FFTW C 库。我已阅读tutorials但无法理解如何正确使用。我应该使用哪个例程,例如 1D 例程或 2D 例程,然后如何?

#更新

double complex reshaped_calib[1001][144]={{1.0 + 0.1 * I}};

double complex** input; //[4096][144];
// have to take dynamic array as I was getting segmentation fault here
input = malloc(4096 * sizeof(double complex*));
for (i = 0; i < 4096; i++) {
input[i] = malloc(144* sizeof(double complex));
}
// input is array I am sending to fftw to apply fft

for (i= 0; i< 1001; i++)
{
for (j= 0; j< 144; j++)
{
input[i][j]=reshaped_calib[i][j];
}
}

// Pad the extra rows
for (i= 1001; i< 4096; i++)
{
for (j= 0; j< 144; j++)
{
input[i][j] = 0.0;
}
}

int N=144, howmany=4096;
fftw_complex* data = (fftw_complex*) fftw_malloc(N*howmany*sizeof(fftw_complex));

i=0,j=0;
int dataCount=0;
for(i=0;i<4096;i++)
{
for(j=0;j<144;j++)
{
data[dataCount++]=CMPLX(creal(input[i][j]),cimag(input[i][j]));
}
}
int istride=1, idist=N;// as in C data as row major
// ... if data is column-major, set istride=howmany, idist=1
// if data is row-major, set istride=1, idist=N
fftw_plan p = fftw_plan_many_dft(1,&N,howmany,data,NULL,howmany,1,data,NULL,howmany,1,FFTW_FORWARD,FFTW_MEASURE);
fftw_execute(p);

最佳答案

您尝试用

填充数组
int pad = 4096;
memset(reshaped_calib, 0.0, pad * sizeof(double complex)); //zero padding

本质上会覆盖数组reshape_calib的前4096个值。为了获得正确的填充,您需要将 2D 数组的大小扩展到所需的 4096 x 144 大小,并仅将输入 1001 x 144 范围之外的条目设置为零。

由于您只是扩展行数,因此可以使用以下内容进行填充:

double complex input[1001][144]={{1.0 + 0.1 * I}};

// Allocate storage for the larger 4096x144 2D size, and copy the input.
double complex reshaped_calib[4096][144];
for (int row = 0; row < 1001; row++)
{
for (int col = 0; col < 144; col++)
{
reshaped_calib[row][col] = input[row][col];
}
}
// Pad the extra rows
for (int row = 1001; row < 4996; row++)
{
for (int col = 0; col < 144; col++)
{
reshaped_calib[row][col] = 0.0;
}
}

也就是说,如果您想要分别对每一行进行 1D FFT,则应该使用 fftw_plan_dft_1d 并多次调用 fftw_execute,或者使用 fftw_plan_many_dft 。这有更详细的描述in this answer通过 @Dylan .

关于c - 使用 FFTW 对 2D 复数数组进行 FFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49976386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com