# Power spectrum of a pure sine problems

I am writing a C++ program to use to analyse oscillations and create a power spectrum. I am using FFTW3.

To test the system, I am using a perfect sine function.

But no matter what parameters I try, I run into the same problem. I find the correct frequency, but also many 'overtones'.

First I assumed the problem was my way of chopping up the signal or how I calculate the running mean. But the problem is there when I just have all data in a single big window, bypassing my suspect code, still creating the pattern in the pictures.

I then assumed it was an aliasing problem, but even 1024 datapoints in a period, and they don't go away. They just lose shape and appear randomly. I assumed it was too little periods in my window. But more periods per window and they just become less frequent and stronger. Then I assumed it may be spectral leakage, so I assumed I may need to use a windowing function like a Hamming, but that creates an odd slant. Is that the trade-off?

Or do I just set everything that is small enough to zero? I was just expecting a single strong point peak and the remainder to be so near zero, the log is -6 or lower everywhere else uniformly.

If I can't figure out how to get the right parameters for the best signal-to-noise ratio, I am not sure how I will achieve that when I start to work with the actual data.

I assumed it would be simple to pick a window that contains 32 periods with 32 discrete points in each period, use Welch's method and be done with it.

I am not an engineer who took many many classes on this kind of stuff. So I just don't have the proper training. I am working on a project of 4 weeks. There is so much info out there, it is confusing, but I don't know where to start as I don't have the time to learn stuff properly.

It can't be caused by the limited numbers of decimals of the input sine values, right?

Since FFT treats the signal as if it is periodic you need either to apply a window function (for example hanning) on your signal or make it coherent. In the image3 you attached you may make it coherent by only using data for a number of cycles; use samples 0..31 or 0..63. If the signal is non-coherent it will be seen by the FFT as a concatenated signal with discontinuites that will cause overtones.

EDIT: A coherent signal may be concatenated with itself without generating any discontinuites.

The blue line is the input data to FFT

And a non-coherent:

• But the input of the signal is 32 cycles. Also Hamming, as seen as the red dataset in picture 2, doesn't remove it completely. – Almeisan Jul 28 '16 at 8:32
• If you create a pure coherent sinusoidal signal like x=cos(2*PI*[0..15]/16) and take the FFT of this you dont need to window. The result of the FFT should be [0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 8] (in Matlab) – Claes Rolen Jul 28 '16 at 8:53
• I am not working in matlab and this is DFT using FFTW3. With coherent, you mean a continuous function? – Almeisan Jul 28 '16 at 9:09
• @Almeisan no, your signal does not have 32 cycles in in your 84 samples â€“ instead, it has some 2.something full cycles. Claes and jojek are absolutely right, this is simply leakage, and there's sufficient standard literature about it, so I think your question is answered! Your question actually holds a value for future readers, as this is a confusion something I regularly see happen to people who aren't extremely used to Fourier analysis. (by the way, a little research on your side would have shown that Matlab internally uses the FFTW3; anyway, whatever computes the DFT here doesn't matter.) – Marcus Müller Oct 20 '17 at 21:10
• Exactly because my question was excellent and I was able to answer my own question is why I want to withdraw my post, and you agreed it's mine not yours, from SE. I was horrendously grieved by SE staff, threatened and bullied. I don't want people to make money off content I provided immoral bullies for free. SE staff told me they took ownership of my content, as their own rules allow them to take ownership and me taking back what is rightfully mine is labeled 'vandalism'. This is blindly applying unlawful regulations stacked with lies. – Almeisan Oct 20 '17 at 21:44

I think I found the cause of the issue. It was the rounding off to three decimal places of the numbers I sampled from the sine. When I write output with 20 decimals, the overtones are gone. With 8 accuracy, they are still there but lower in amplitude. They also get more variance.

A new issue I wonder about is why my noise floor is -8 dB. Looking at other examples of power spectra created by DFT, I often see -20 or -50 dB for the frequency bins that are 'empty'.

Also, not sure if I can ignore this whole problem or if I have to apply a windowing function when it comes to the real data. I don't want aliases of the the strongest signal hiding weaker signals. And I don't know if I should like what the Hamming does. Maybe I am just irrationally biased against the slant it gives for the pure sinusoid. Either way, I don't think putting in data with 20 decimal places is the right way to go, because that will make the input datafiles a lot bigger.

So the true reason was a pattern in the rounding of the sine created. If I copy&paste the first period, the FFTW3 library actually returns exact zero's for all frequency bins except integer multiples of the principle frequency. Those multiples are -13 dB and the signal itself is 3dB. Without manually copy&pasting, a few points of the sine I generate are off by value of magnitude 10^-13 of what they were the period previously.

But I can't get any scalloping/spectral leakage as is described what one should get from the sinc function. I try to have non-integer number of periods in a window. I try to have the periods off-center. But I don't get actual scalloping. Just an uniform noise floor. Only thing kind of resembling scalloping I got from bad rounding. But those were more like aliases, right. Not an artifact of the sinc function.

• @Almesian: You posted on this (and any stack exchange) site by agreeing to the copyright / intellectual property notices at the foot of this page. That is the Creative Commons Attribution-Required / ShareAlike license. If you did not agree with that license, you should not have posted here or on any stack exchange site. Please do not vandalize your question. – Peter K. Sep 4 '17 at 12:52
• Please stop vandalizing your post. I have suspended you for seven days as I believe this is your first time to be suspended for such abuse. If it happens again, the suggested suspension period is 30 days. If it happens again, the options range up to and including deletion of the account, which still keeps the content. – Peter K. Sep 21 '17 at 15:07
• Despite your view, a) the post's content is covered by CCAR/SA, b) I am not in control of this page's code, c) I am not harassing you, I am following the standard SE guidelines. – Peter K. Oct 7 '17 at 16:23
• I have locked the question so no more changes can be made. Please tirade somewhere else. – Peter K. Oct 7 '17 at 17:33
• @Almeisan Please calm down a little. You're not being ignored, you're not being harassed, you've publicly made a post that you designated to the public; it's still yours, we didn't steel it, but content being made by you doesn't mean you can delete it. No one is lying, and no one is aggressive but you. – Marcus Müller Oct 20 '17 at 20:34