# Difference between two sine generators I have two sine oscillators and I get these two different spectrum analysis (let's disregard the slight frequency difference). Is there any deductions that can be made about the "quality", or sine approximation error or any other type of information about the sine generators derived by these graphs? Why the second is more steep? Is it normal? Which of the two is closer to the ideal sine FFT spectrum? What to read so to understand differences of this type?

The sample rate is 32k and there are 8192 frequency bins.

The code that produces this difference is:

a. (upper graph)

double freq;
static double sine_rate;

sine_rate += freq;
*var = (long) ( sin( sine_rate ) * 2142483647.0 );


b.

double freq;
static double sine_rate;

sine_rate += freq;
*var = (long) ( sin( sine_rate * PI ) * 2142483647.0 );


double "freq" is provided when the function is called and can be any double between 0.0 and 1.0. The difference is the multiplication by 3.14.

• The bottom one is narrower, and thus closer to a "true" sine. – MBaz Apr 22 '19 at 18:12
• Did you use a window function other than rectangular? – Olli Niemitalo Apr 23 '19 at 3:41
• @Olli Niemitalo No the FFT function is the same. The first used sin(counter++) and the secondary sin( (counter++) * PI ). So the difference is that the second is wrapped by PI and the first is not. I would like to be able to understand how the sine function works and hoped someone would give me some hints here. – John Am Apr 23 '19 at 6:14
• How is counter related to the sample number? They can't be equal because the frequency should be 16 k whereas your lower graph shows a frequency of about 200. What do you mean by wrapping? It would be more clear to post also the code in the question. – Olli Niemitalo Apr 23 '19 at 6:29
• @Olli Niemitalo I will do it in a moment. Yes it is simplified as I posted in the comment. By wrapping I mean constrain. – John Am Apr 23 '19 at 7:13

## 1 Answer

The two oscillator types are equivalent, because the factor PI can be moved to freq and need not be present in the final equation.

The difference in the plots is not due to one oscillator type being better than the other; they are equivalent. The difference seen is spectral analysis artifacts that depend on the frequency of the sine and how that relates to the Discrete Fourier transform (DFT) length. DFT decomposes the time-domain signal into harmonic frequencies (multiples of a fundamental frequency that has a fundamental period equal to the analysis window length). If the sine frequency is not exactly at such a harmonic frequency, the sine "energy" still needs to appear somewhere in the frequency domain and will be spread to multiple frequency bins. The plots in this question on such spectral leakage are very similar to yours. Spectral leakage can be ameliorated by multiplying the data by a suitable window function before DFT.

With the oscillator of type a, if freq is of form some_integer*2*PI/N, where N is the DFT length, then the sine frequency is one of the harmonic frequencies seen by DFT and there will be no spectral leakage.