# Why a DFT of two sinusoids is very noisy even with frequency sampling 5 times higher?

I've set up this case to try to understand DFT implementing a real case in Excel

Frame Size $$\;\color{blue}{(T = 5 \; s})$$
Time Sampling $$\;\color{blue}{(T_S = 0.1 \; s})$$
Block Size $$\;\color{blue}{(N = T/T_S = 50)}\;$$ (It was 51 in my question)
Sampling Rate $$\;\color{blue}{(F_S = T/T_S+1 =10 Hz}$$
Frequency Resolution $$\;\color{blue}{(F_R = F_S/N = 0.2 Hz)}$$

In time spectrum I have put just the sum of 2 sin functions

The first sine has amplitude $$\color{blue}{10}$$, phase $$\color{blue}{\pi/3}$$ and frequency $$\color{blue}{2}$$ Hz. The second sine has amplitude $$\color{blue}{5}$$, phase $$\color{blue}{0}$$ and frequency $$\color{blue}{1}$$ Hz and for $$\color{blue}{\; n = 0\; to\; 49 \;}$$ we have :

$$\color{blue}{x_n = 10\, \sin{(2\pi t 2 + \pi/3)}+ 5\, \sin{(2\pi t 1)}}$$

So I have generated DFT values where $$\color{blue}{\; k=-25\; to\; 24\, }$$ :

$$\color{blue}{\quad X_k=a_k + ib_k}\;$$

$$\color{blue}{a_k = (1/N) \; \Sigma_{n=0,N-1} x_n \cos{(2 \pi n k /N)}}$$ $$\color{blue}{b_k = (1/N) \; \Sigma_{n=0,N-1} x_n \sin{(2 \pi n k /N)}}$$

After, I've calculated module $$\color{blue}{A}$$ and phase $$\color{blue}{\Phi}$$ for $$\color{blue}{\; k = -25\; to \;24\,}$$:

$$\color{blue}{A_k = |X_k| = \sqrt{(a_k)^2 + (b_k)^2}}$$ $$\color{blue}{\Phi_k = atan2(b_k, a_k)}$$

I consider in my analysis for $$\color{blue}{\; k = -25\; to \;24\,}$$: $$\color{blue}{ Freq_k = k.F_S/N}$$

Old text: Now my doubt arise. In my understanding, the signal is concentrated in the values of frequencies $$\color{blue}{-1}$$ Hz, $$\color{blue}{-2}$$ Hz, $$\color{blue}{1}$$ Hz and $$\color{blue}{2}$$ Hz. The negative frequencies I disregard, of course. However, in short, the frequency spectrum is noisy. For instance, $$\color{blue}{2.2}$$ Hz amplitude is around $$\color{blue}{25%}$$ of $$\color{blue}{2}$$ Hz amplitude. Is it normal? What's happening?

New text As Fat32 has showed me, I was using an additional and spurious point ($$\color{blue}{51}$$ in total) in my time sample that was distorting the whole calculation. If I use a specific time frame ($$\color{blue}{5\;s}$$ in this case), I need to suppress the final time point (time = $$\color{blue}{5\;s}$$)

$$\star\star\star$$

I'm working with frequency sampling $$\color{blue}{10}$$ Hz, five times than maximum detected frequency ($$\color{blue}{2}$$ Hz).

Old text: The original signal was restored well, where $$\color{blue}{\;n=0\; to\; 50}\;$$ and imaginary part $$\;\color{blue}{\sim 0}\;$$.

New text: The original signal was restored with no flaws, where $$\color{blue}{\;n=0\; to\; 49}\;$$ and imaginary part $$\;\color{blue}{= 0}\;$$. The only positive frequences other than $$\color{blue}{0}\;$$ is $$\color{blue}{1\;hz}\;$$ and $$\color{blue}{2\;hz}\;$$ , respectively with value $$\color{blue}{125}\;$$ and $$\color{blue}{250}\;$$, $$\color{blue}{25}\;$$ times bigger than the real amplitude. I guess that the factor is $$\color{blue}{25}\;$$ because is the half the samples ($$\color{blue}{N=50}\;$$).

$$\star\star\star$$

$$\color{blue}{ Re(x_n) =(1/N)\Sigma_{k=-N/2,N/2-1}A_k \cos{\Phi_k} \cos{(2 \pi k n/N)} - A_k \sin{\Phi_k} \sin{(2 \pi k n/N)}}$$

$$\color{blue}{ Im(x_n) = -(1/N)\Sigma_{k=-N/2,N/2-1}A_k \sin{\Phi_k} \cos{(2 \pi k n /N)} + A_k \cos{\Phi_k}\sin{(2 \pi k n /N)}}$$

The 50 time data (it was 51 time date before the answer) is available from this link

The $$\color{blue}{50}$$ transformed data(Re(z), Im(z), |z|, $$\Phi$$(z)) (it was $$\color{blue}{51}$$ time date before the answer) is available from this link

• Hi! could you upload a signal and spectrum plot as well ? – Fat32 Oct 6 '18 at 22:53
• I did it. There is a best way to do it? – Paulo Buchsbaum Oct 6 '18 at 23:17

As far as I understood your question, you are complaining about the windowing effect and its consequence of smearing of the spectrum.

So you have a mathematically defined pure sine wave that extends from minus infinity to plus infinity in time, whose theoretical spectrum, you expect to be, a single line in frequency. But you eventually represent that infinitely long ideal sine wave with a finite length record (the windowed version, aka truncated) in practice and take the DFT of the finite length version instead. Then the result will be a broadened single line into a sinc waveform mostly having nonzero amplitudes at all frequencies in general which is what you called noise?

That's the most fundamental manifestation of the effect of time domain windowing (aka aperture effect) on the frequency spectrum; the smearing of the single frequency line and also leakage when multiple tones (including DC) co-exist.

So those are not noise, but an irrecoverable side effect of the practical utilization of finite length DFT. Note however that you might be adding your computational error on top of that. Hence for best verification, you better test your algorithm with an established library from Matlab / Octave kind of platforms.

The following plots are generated by Matlab from your uploaded data, to test your problem and it confirms with the explanation; i.e., those nonzero values you see in the spectrum are due to the windowing effect...

You cannot get rid of it but the effect will minimize by using longer data, however is not the most practical approach. Instead by using special window types such as Hamming, Hanning, Blackman, Kaiser etc, you can tradeoff certain features of it. Incidentally you are using a rectangular window, whose peaks are sharpest but those tails (side lobes) are also largest. All other windows will supress the tails (side lobes) but widen the peaks...

And this is the result of Hamming window applied to your data :

Note that your signal $$x[n]$$ seems 1 sample longer than it would be...

• I've uploaded the data in the question, but there is no computacional error, because I've got return with original data in Excel doing the IDFT. I program in MatLab but it's just dirty calculations. – Paulo Buchsbaum Oct 6 '18 at 23:19
• Great! I will research about Hamming Windows. Why I have 1 additional time point? If I have frame size of 5 seconds time and time sampling of 0.1 second, it's not 51 data points? 5 / 0.1 + 1 from 0 seconds to 5 seconds? Frequencies also has 51 points from -2.5 Hz to 2.5 Hz step 0.2 Hz? – Paulo Buchsbaum Oct 7 '18 at 2:33
• I've got your point. I take out the final point ant now the Inverse DFT works exactly not approximately like before. – Paulo Buchsbaum Oct 7 '18 at 3:54
• When I take out the extra point, the noise is gone. I swear! – Paulo Buchsbaum Oct 7 '18 at 3:57
• I will update the output in the question. – Paulo Buchsbaum Oct 7 '18 at 3:57