# What is going wrong with my DFT program?

• [Testing frequency's amplitude: $56.9708$, and its phase: $1.32018 \pi \textrm{ rad}.$]

Testing frequency's amplitude should be $57$, then it is okay. But its phase should be $-0.68\pi\textrm{ rad}$. Obviously it is obtained by subtracting: $1.32018\pi\textrm{ rad} - 2\pi\textrm{ rad}$.

But why this?

// 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 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 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;
}

• why the 0.5 is added in the final phase computation? – lxg Feb 22 '17 at 15:57
• according to your data, your result was right because there is only difference of one period. – lxg Feb 22 '17 at 16:02
• Since sine and cosine are $2\pi$-periodic, the phase is only determined up to multiples of $2\pi$. A phase value of $\phi$ is equivalent to another value $\phi+2k\pi$, where $k$ is an arbitrary integer. One convention is to choose the phase inside the interval $[-\pi,\pi)$, and this can always be achieved by adding or subtracting multiples of $2\pi$. – Matt L. Feb 22 '17 at 16:13
• @ Mr. lxg, in my dear dsp Teacher's book "Understanding DSP"? section 3.1, example 3.1.1, every phase is 90° greater than computed. For this, I added 0.5 π rad. – George Theodosiou Feb 22 '17 at 16:23
• @ Messrs lxg and Matt L., please let me one more question: Do I have add in program "if" statement that, if computed phase is greater than 1 π rad, 2 π rad be subtracted? Regards. – George Theodosiou Feb 22 '17 at 16:29