问题描述
我正在尝试找出如何使用fftw3逐行计算以下矩阵(存储为数组)的fft:
double signal[9] = {
1,2,3,4,5,6,7,8,9
}
获得具有行的矩阵(伪代码)
fft([signal[0],signal[1],signal[2]])
fft([signal[3],signal[4],signal[5]])
fft([signal[6],signal[7],signal[8]])
我可以仅使用np.fft.fft([[1,3],[4,6],[7,9]],axis=1)
就可以使用Python检查结果,而我想通过fftw3获得相同的结果。
我的代码:
#include <fftw3.h>
#include <iostream>
int main()
{
// it is needed to compute fft row-by-row of the following matrix
double signal[9] {
1,9
}; // 3x3 matrix
// so that the output will have the form (pseudo-code)
// fft(signal[0],signal[2])
// fft(signal[3],signal[5])
// fft(signal[6],signal[8])
fftw_complex result[9]{ };
// prepare parameters
int rank = 1;
int n[] = { 3 };
int howmany = 3;
int idist = 3;
int odist = 3;
int istride = 1;
int ostride = 1;
auto plan_ = fftw_plan_many_dft_r2c(
rank,// 1D problem
n,// 1D transforms of length n[0] = 3
howmany,// 3 1D transforms
signal,// input matrix (array)
NULL,// will be assigned to n
istride,// distance between two elements in the same row
idist,// distance between the first elements of neighboring rows
result,// output
NULL,// will be assigned to n
ostride,// distance between two elements in the same row
odist,// distance between the firsn elements of neighboring rows
FFTW_ESTIMATE
);
fftw_execute(plan_);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
std::cout << result[i * 3 + j][0] << " + 1j*" << result[i * 3 + j][1] << '\t';
}
std::cout << '\n';
}
fftw_destroy_plan(plan_);
return 0;
}
相应的解释在代码中。 Python提供以下答案:
array([[ 6. +0.j,-1.5+0.8660254j,-1.5-0.8660254j],[15. +0.j,[24. +0.j,-1.5-0.8660254j]])
6 + 1j*0 -1.5 + 1j*0.866025 0 + 1j*0
15 + 1j*0 -1.5 + 1j*0.866025 0 + 1j*0
24 + 1j*0 -1.5 + 1j*0.866025 0 + 1j*0
我看到输出几乎与Python相同,但是每行的第三个元素为零。有人可以帮我吗?
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)