不返回预期数据的FFT的逆FFT
inverse fft of fft not returning expected data
我正试图确保FFTW做我认为应该做的事情,但我有问题。我使用的是OpenCV的cv::Mat
。我编写了一个测试程序,给定一个Mat f
,计算ifft(fft(f))
并将结果与f
进行比较。我希望两者之间的差异可以忽略不计,但是数据中有一个奇怪的模式。
在本例中,f
被初始化为一个8x8的浮点数数组,其正数值小于1。
Mat f = .. //populate f
if (f.type() != CV_32FC1)
DLOG << "Bad f type";
const int y = f.rows;
const int x = f.cols;
double* input = fftw_alloc_real(y * 2*(x/2 + 1));
// forward fft
fftw_plan plan = fftw_plan_dft_r2c_2d(x, y, input, (fftw_complex*)input, FFTW_MEASURE);
// inverse fft
fftw_plan iplan = fftw_plan_dft_c2r_2d(x, y, (fftw_complex*)input, input, FFTW_MEASURE);
// populate fftw data from f
for (int yi = 0; yi < y; ++yi)
{
const float* yptr = f.ptr<float>(yi);
for (int xi = 0; xi < x; ++xi)
input[yi*x + xi] = (double)yptr[xi];
}
fftw_execute(plan);
fftw_execute(iplan);
// put data into another cv::Mat for comparison
Mat check(y, x, f.type());
for (int yi = 0; yi < y; ++yi)
{
float* yptr = check.ptr<float>(yi);
for (int xi = 0; xi < x ; ++xi)
yptr[xi] = (float)input[yi*x + xi];
}
DLOG << Util::summary(f, "f");
DLOG << f;
DLOG << Util::summary(check, "check");
DLOG << check;
Mat diff = f*x*y - check;
DLOG << Util::summary(diff, "diff");
DLOG << diff;
其中DLOG
是我的记录器和Util::summary(cv::Mat m)
只是打印传递字符串和尺寸,通道,最小值和最大值的垫。
数据是这样的(输出):
f: rows:8 cols:8 chans:1 min:0.00257996 max:0.4
[0.050668437, 0.04509116, 0.033668514, 0.10986148, 0.12855141, 0.048241843, 0.12613985,.09731093;
0.028602425, 0.0092236707, 0.037089188, 0.118964, 0.075040311, 0.40000001, 0.11959606, 0.071930833;
0.0025799556, 0.051522054, 0.22233701, 0.052993439, 0.032000393, 0.12673819, 0.015244827, 0.044803992;
0.13946071, 0.019708242, 0.0112687, 0.047459811, 0.019342113, 0.030085485, 0.018739942, 0.0098618753;
0.041809395, 0.029681522, 0.026837418, 0.16038358, 0.29034778, 0.17247421, 0.1789207, 0.042179305;
0.025630442, 0.017192598, 0.060540862, 0.1854037, 0.21287154, 0.04813192, 0.042614728, 0.034764063;
0.0030835248, 0.018511582, 0.0071733585, 0.017076733, 0.064545207, 0.0026390438, 0.088922881, 0.045725599;
0.12798512, 0.23215951, 0.027465452, 0.03174505, 0.04352935, 0.025079668, 0.044403922, 0.035459157]
check: rows:8 cols:8 chans:1 min:-3.26489 max:25.6
[3.24278, 2.8858342, 2.1547849, 7.0311346, 8.2272902, 3.0874779, 8.0729504, 6.2278996;
0.30818239, 0, 2.373708, 7.6136961, 4.8025799, 25.6, 7.6541481, 4.6035733;
0.16511716, 3.2974114, -3.2648909, 0, 2.0480251, 8.1112442, 0.97566891, 2.8674555;
8.9254856, 1.2613275, 0.72119683, 3.0374279, -0.32588482, 0, 1.1993563, 0.63116002;
2.6758013, 1.8996174, 1.7175947, 10.264549, 18.582258, 11.038349, 0.042666838, 0;
1.6403483, 1.1003263, 3.8746152, 11.865837, 13.623778, 3.0804429, 2.7273426, 2.2249;
0.44932228, 0, 0.45909494, 1.0929109, 4.1308932, 0.16889881, 5.6910644, 2.9264383;
8.1910477, 14.858209, -0.071794562, 0, 2.7858784, 1.6050987, 2.841851, 2.2693861]
diff: rows:8 cols:8 chans:1 min:-0.251977 max:17.4945
[0, 0, 0, 0, 0, 0, 0, 0;
1.5223728, 0.59031492, 0, 0, 0, 0, 0, 0;
0, 0, 17.494459, 3.3915801, 0, 0, 0, 0;
0, 0, 0, 0, 1.5637801, 1.9254711, 0, 0;
0, 0, 0, 0, 0, 0, 11.408258, 2.6994755;
0, 0, 0, 0, 0, 0, 0, 0;
-0.2519767, 1.1847413, 0, 0, 0, 0, 0, 0;
0, 0, 1.8295834, 2.0316832, 0, 0, 0, 0]
对我来说困难的部分是diff
矩阵中的非零项。我已经考虑了FFTW对值的缩放和对真实数据进行就地fft所需的填充;我错过了什么?
我发现令人惊讶的是,当有这么多零时,数据可能会偏离17的值(这是最大值的66%)。此外,数据的不规则性似乎形成了一个对角线模式。
在编写fftw_alloc_real(y * 2*(x/2 + 1));
时,您可能已经注意到fftw在x方向上需要额外的空间来存储复杂的数据。在您的示例中,当x=8时,它需要2*(x/2+1)=10
real。
http://www.fftw.org/doc/Real_002ddata-DFT-Array-Format.html Real_002ddata-DFT-Array-Format
所以…在填充input
数组或从中检索值时,应该注意这一点。
你的方式改变
input[yi*x + xi] = (double)yptr[xi];
int xfft=2*(x/2 + 1);
...
input[yi*xfft + xi] = (double)yptr[xi];
和
yptr[xi] = (float)input[yi*x + xi];
yptr[xi] = (float)input[yi*xfft + xi];
它应该解决你的问题,因为你的diff中的非空点对应于额外的填充。
再见,
相关文章:
- std::numeric_limits::infinity() 的逆数为零吗?
- 模板化回调参数的逆变,如 C# 中的逆变
- FFT的研究 - 为什么它不快?
- NTRUEncrypt:使用开源标准算法中的描述无法正确找到两个多项式的GCD,无法定义是否存在多边形的逆
- 带参数的累积正态分布函数的逆
- (伪)-行列式为零的N乘N矩阵的逆
- cuBLAS矩阵的逆速度比MATLAB慢得多
- 在垫子矩阵上执行简单的逆和乘法操作
- 如何找到近乎奇异矩阵的逆矩阵?
- 在C++中使用1D FFT的四元数FFT
- 在库中搜索基于 3D FFT 的卷积
- C++中对数正态分布的累积函数的逆
- cpp中基于FFT的图像配准(可选使用OpenCV)
- 使用特征矩阵的逆
- 如何在特征库中计算稀疏矩阵的逆
- 两个矩阵之和的逆
- 8位FFT的CPU架构
- 如何在OpenCV中做复矩阵的逆
- 如何在Cuda中获得2D真实到复杂FFT的所有数据
- 不返回预期数据的FFT的逆FFT