女士们,先生们,请让我再次为您解决我的问题。在下面的程序中,只有一个频率为 256 Hz、幅度为 100,000、相位为 0.67 pi rad 的正弦曲线。测试频率网格为 1 Hz 步长,从 0 Hz 到 1000 Hz。输出在 256 Hz 时的最大幅度为 99,221,相位为 0.670561 pi rad。考虑到准确性的错误,我认为这没问题。
但是伪频率幅度有起有落,而我对 DFT 的理解是它们应该上升到 256 Hz 然后下降。为什么会有这些起伏?我仍然问候。
// Grid is created by variables lowTestFreq and testFreqStep
// both of double type, and integer type M.
// In comments are units of variables.
#include <iostream>
#include "math.h"
int main()
{
//Unchanged variables.
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.
double testAmpl = 0; // pure number.
double testPhase = 0; // rad and then pirad.
//Sampling frequency and samples of segment.
int samplFreq = 50000; // sample/second.
int N = 3000; //sample.
// Existing sinusoid.
double ampl_1 = 100000; //pure number.
double existFreq_1 = 256; // Hz.
double phase_1 = 0.67 * pi; // rad.
double unitAngle_1 = (existFreq_1 / samplFreq) * (2 * pi); // rad/sample.
// Testing grid.
double testFreq = 0; // Hz.
double testUnitAngle = 0; // rad.
double testAngle = 0; //
double lowTestFreq = 0; //Hz.
double testFreqStep = 1; // Hz.
int M = 1000; // pure number.
int m = 0; // pure number.
for (m = 0; m <= M; ++m)
{
testFreq = lowTestFreq + (m * testFreqStep); // Hz.
testUnitAngle = (testFreq / samplFreq) * (2 * pi); // rad/sample.
Re = 0; Im = 0;
for (n = 1; n <= N; ++n)
{
xn = ampl_1 * sin(n * unitAngle_1 + phase_1);
testAngle = n * testUnitAngle;
xn_cos = xn * cos(testAngle);
xn_sin = xn * -sin(testAngle);
Re += xn_cos;
Im += xn_sin;
}
std::cout << "Test Freq " << testFreq; // Hz.
testAmpl = sqrt(Re * Re + Im * Im) / N * 2; //pure number.
std::cout << " Test Freq's Ampl: " << testAmpl;
testPhase = atan2 (Im, Re); // rad.
testPhase = testPhase / pi; // pirad.
testPhase = testPhase + 0.5; // pirad. Corrected phase.
std::cout << ", and its phase: " << testPhase
<< " pirad." << std::endl;
}
return 0;
}