我的 DFT 程序出了什么问题?

信息处理 自由度
2022-02-02 05:25:07

请让我就我的问题寻求您的帮助。我的以下 DFT 程序输出:

  • [测试频率的幅度:56.9708, 及其相位:1.32018π rad.]

测试频率的幅度应该是57,那么就没事了。但它的相位应该是0.68π rad. 显然它是通过减去:1.32018π rad2π rad.

但这是为什么呢?

// Testing program for DFT. I change sampling frequency (samplFreq), samples (N)
// testing frequency (testFreq), and two existing in signal frequencies (existFreq_#)
// with their amplitudes (ampl_#) and phases (phase_#). After variable's declaration
// there is, in comment, its unit.

#include <iostream> 
#include "math.h"

int main()
{
   double pi = 3.141592653589793; //pure number.
   int n = 0; // sample.
   double xn = 0; //pure number.
   double Re = 0; //pure number.
   double Im = 0; //pure number.
   double xn_cos = 0; //pure number.
   double xn_sin = 0; //pure number.

   int samplFreq = 3021; // sample/second.
   int N = 4400; //sample.

   double ampl_1 = 57; //pure number.
   double existFreq_1 = 536 * 2 * pi; // 2 * pi rad (cycle)/sec (multiple unit), basic unit: rad/sec.
   double phase_1 = -0.68 * pi; //pirad = pi rad (multiple unit), basic unit: rad.
   double unitAngle_1 = existFreq_1 / samplFreq; // cycle/sample (multiple unit), basic unit: rad/sample.

   double ampl_2 = 91; // pure number.
   double existFreq_2 = 896 * 2 * pi; // 2 * pi rad (cycle)/sec (multiple unit), basic unit: rad/sec.
   double phase_2 = -0.51 * pi; //pirad = pi*rad (multiple unit), basic unit: rad.
   double unitAngle_2 = existFreq_2 / samplFreq; // angle/sample.

   double testFreq = 536 * 2 * pi; // 2 * pi rad/sec (multiple unit), basic unit: rad/sec.
   double testUnitAngle = testFreq / samplFreq; // cycle/sample (multiple unit), basic unit: rad/sample.    

   for (n = 1; n<= N; ++n)
   {
       xn = ampl_1 * sin(n * unitAngle_1 + phase_1)
          + ampl_2 * sin(n * unitAngle_2 + phase_2);

       xn_cos = xn *  cos(n * testUnitAngle);
       xn_sin = xn * -sin(n * testUnitAngle);

       Re += xn_cos;
       Im += xn_sin;
   }  
   double testAmpl = sqrt(Re * Re + Im * Im) / N * 2; //pure number.       
   std::cout << "Testing frequency's amplitude: " << testAmpl;
   std::cout << ", and its phase: " << atan2 (Im, Re) / pi + 0.5 << " pirad." << std::endl; // rad.
return 0;
}
0个回答
没有发现任何回复~