Following program outputs sum of samples of signal's period. Values in program are:
Sample rate 96 ksamples/sec.
First frequency (fundamental, 1st harmonic) 96 Hz.
Second frequency (2nd harmonic) 192 Hz.
n = 1 to 1000, representing one (the first) period of the signal.
Output (sum of period's samples) should be zero. Actually is 18.4062, apparently due to round off errors. In general, when samples are multiple of fundamental's (in this example: 2000, 3000, ... samples), sum is least. Other sums are much greater. So first harmonic is represented by the first least sum. I have some question about, but first of all, I have to ask whether this process belongs to DSP domain.
// Outputs sum of input values.
#include <iostream>
#include "math.h"
int main()
{
//Unchanged variables.
float pi = 3.141592653589793; //pure number.
int n = 0; // sample.
float xn = 0; //pure number.
float xn_cos = 0; //pure number.
float xn_sin = 0; //pure number.
float sum = 0;
//Sampling frequency and samples of segment.
int samplFreq = 96000; // sample/second.
// 1st existing sinusoidal.
float ampl_1 = 70000; //pure number.
float existFreq_1 = 96; // Hz.
float phase_1 = 0.67 * pi; // rad.
float unitAngle_1 = (existFreq_1 / samplFreq) * (2 * pi); // rad/sample.
// 2nd existing sinusoidal.
float ampl_2 = 600000; // pure number.
float existFreq_2 = 192; // Hz.
float phase_2 = -0.37 * pi; // rad.
float unitAngle_2 = (existFreq_2 / samplFreq) * (2 * pi); // rad/sample.
for (n = 1; n <= 1000; ++n)
{
xn = ampl_1 * sin(n * unitAngle_1 + phase_1)
+ ampl_2 * sin(n * unitAngle_2 + phase_2);
sum += xn;
}
std::cout << sum;
return 0;
}