测试离散傅立叶变换程序 [英] Testing Discrete Fourier Transform Program

查看:70
本文介绍了测试离散傅立叶变换程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我一直在测试我的离散傅里叶变换程序的1维数据(使用C ++编写)。



对于以下测试输入数据,

Hello. I have been testing my Discrete Fourier Transform program for 1 dimensional data (written in using C++).

For the following test input data,

REAL[0]=  0.000,   IMAG[0]=  0.000
REAL[1]= +1.000,   IMAG[1]=  0.000
REAL[2]=  0.000,   IMAG[2]=  0.000
REAL[3]=  0.000,   IMAG[3]=  0.000
REAL[4]=  0.000,   IMAG[4]=  0.000
REAL[5]=  0.000,   IMAG[5]=  0.000
REAL[6]=  0.000,   IMAG[6]=  0.000
REAL[7]=  0.000,   IMAG[7]=  0.000





正确的预期结果是(在变换域中应该具有恒定的幅度;在这种情况下,real [x]平方imag [x]平方的平方根在每种情况下等于0.125):



The correct expected results are(should have constant magnitude in the transform domain; in this case, the squareroot of "real[x] squared imag[x] squared" equals 0.125 in every case):

REAL[0]=  0.125,   IMAG[0]= +0.000
REAL[1]= +0.088,   IMAG[1]= -0.088
REAL[2]= +0.000,   IMAG[2]= -0.125
REAL[3]= -0.088,   IMAG[3]= -0.088
REAL[4]= -0.125,   IMAG[4]= +0.000
REAL[5]= -0.088,   IMAG[5]= +0.088
REAL[6]= +0.000,   IMAG[6]= +0.125
REAL[7]= +0.088,   IMAG[7]= +0.088





但是,我的程序返回以下结果:





However, my program returns the following results:

Real[0]: 0.125          Imag[0]:  0
Real[1]: 0.0883883      Imag[1]:  -0.0883883
Real[2]: 7.65404e-018   Imag[2]:  -0.125
Real[3]: -0.0883883     Imag[3]:  -0.0883883
Real[4]: -0.125         Imag[4]:  -1.53081e-017
Real[5]: -0.0883883     Imag[5]:  0.0883883
Real[6]: -2.29621e-017  Imag[6]:  0.125
Real[7]: 0.0883883      Imag[7]:  0.0883883





以下是我的计划。我很感激指导学习错误的位置以及如何解决这个问题,谢谢!





Below is my program. I would appreciate guidance in learning where the error lies and how to fix my this, thank you!

 bool inverse = false;
 int n = 8;
 double gRe[8] = {0,1,0,0,0,0,0,0};
 double gIm[8] = {0,0,0,0,0,0,0,0};
 double GRe[8] = {0,0,0,0,0,0,0,0};
 double GIm[8] = {0,0,0,0,0,0,0,0};


for(int w = 0; w < n; w++)
{
  GRe[w] = GIm[w] = 0;
  for(int x = 0; x < n; x++)
  {
    double a = -2 * pi * w * x / float(n);
    if(inverse) a = -a;
    double ca = cos(a);
    double sa = sin(a);
    GRe[w] += gRe[x] * ca - gIm[x] * sa;
    GIm[w] += gRe[x] * sa + gIm[x] * ca;
  }
  if(!inverse)
  {
    GRe[w] /= n;
    GIm[w] /= n;
  }
}

推荐答案

每个有意义的测试都包含刺激,预期结果,实际结果和比较器比较预期结果和实际结果。

如果你在这里比较实际结果和预期结果,你会发现你有精确问题。

预期结果似乎是仅限三位数。确保你与各自的精确度进行比较。

问候

Andi
Each meaningful test consists of stimulus, expected result, actual result an a comparator that compares the expected and the actual result.
If you compare the actual versus the expected result here, you see that you have a precision issue.
The expected result seems to be on three figures only. Make sure you compare with the respective precision.
Regards
Andi


对我来说,看起来你的结果是正确的15重要数字,这是在 double 类型中计算的预期值。所以没有什么可抱怨的。我假设您的参考结果来自一本书,并且只打印了小数点后的前三位数字。这就是出现差异的原因。


您可能已经意识到您的计算方法效率相对较低。存在所谓的快速傅立叶算法,其需要更少的计算工作。只需使用谷歌,看看他们做了什么,以及他们如何节省许多乘法和添加操作。
To me it looks like your results are correct to 15 significant digits, which is what was to be expected calculating in type double. So there is nothing to complain about. I assume your reference results were taken from a book and only the first three digits after the decimal were printed. So that is the reason of the discrepancy.

You are probably aware that your method of computation is relatively inefficient. There are so-called Fast Fourier algorithms that require much less computational work. Just use google and see what they do and how they save many multiply and add operations.


你的问题是正确的预期结果四舍五入到3位小数而不是你的实际结果。



你只需要将输出格式化为3位小数。

否则,问题在于显示结果的代码,你没有展示的代码。
Your problem is that the "correct expected results" are rounded to 3 decimals and not your "actual results".

You just need to format your output to 3 decimals.
Said otherwise, the problem is in code that display the result, code that you didn't showed.


这篇关于测试离散傅立叶变换程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆