【发布时间】:2016-05-04 18:07:55
【问题描述】:
我一直在 matlab 中使用函数“fft(x)”,其中“x”是复数向量。我正在寻找一个易于使用的 C++ 函数,它可以返回复数。
我找到了这个代码:http://paulbourke.net/miscellaneous/dft/
如果它是等效的,我该如何使用它?感谢您的宝贵时间!
/*
This computes an in-place complex-to-complex FFT
x and y are the real and imaginary arrays of 2^m points.
dir = 1 gives forward transform
dir = -1 gives reverse transform
*/
short FFT(short int dir,long m,double *x,double *y)
{
long n,i,i1,j,k,i2,l,l1,l2;
double c1,c2,tx,ty,t1,t2,u1,u2,z;
/* Calculate the number of points */
n = 1;
for (i=0;i<m;i++)
n *= 2;
/* Do the bit reversal */
i2 = n >> 1;
j = 0;
for (i=0;i<n-1;i++) {
if (i < j) {
tx = x[i];
ty = y[i];
x[i] = x[j];
y[i] = y[j];
x[j] = tx;
y[j] = ty;
}
k = i2;
while (k <= j) {
j -= k;
k >>= 1;
}
j += k;
}
/* Compute the FFT */
c1 = -1.0;
c2 = 0.0;
l2 = 1;
for (l=0;l<m;l++) {
l1 = l2;
l2 <<= 1;
u1 = 1.0;
u2 = 0.0;
for (j=0;j<l1;j++) {
for (i=j;i<n;i+=l2) {
i1 = i + l1;
t1 = u1 * x[i1] - u2 * y[i1];
t2 = u1 * y[i1] + u2 * x[i1];
x[i1] = x[i] - t1;
y[i1] = y[i] - t2;
x[i] += t1;
y[i] += t2;
}
z = u1 * c1 - u2 * c2;
u2 = u1 * c2 + u2 * c1;
u1 = z;
}
c2 = sqrt((1.0 - c1) / 2.0);
if (dir == 1)
c2 = -c2;
c1 = sqrt((1.0 + c1) / 2.0);
}
/* Scaling for forward transform */
if (dir == 1) {
for (i=0;i<n;i++) {
x[i] /= n;
y[i] /= n;
}
}
return(TRUE);
}
【问题讨论】:
-
您好,欢迎来到 SO。两种可能的方法:尝试理解代码并查看它是否执行类似的计算步骤。第二种方法:尝试数值等效测试。 IE。输入测试数据并比较输出。
-
如果您知道如何调用 C++ 函数,那么您已经知道如何使用它。顶部的 cmets 告诉您预期的输入。此外,至于您的“等价”问题,它们在数值上是等价的,但计算 FFT 的过程并不相同。
fft使用 FFTW,它使用基数分解来实现更快的运行时间。 -
标准是fftw library。在您的搜索中,它有助于优先选择经过测试、记录在案和广泛使用的库。
-
如果您从解释 FFT 应该在 Matlab 中做什么开始,那么您会在 C++ 专家那里获得更多的运气。不是每个人都知道这一点。
-
任何人都无法告诉您“如何”使用它,因为我们不知道您输入的形式是什么。
标签: c++ performance matlab transform fft