DFT of pure sinusoidal wave

I'm writing a program in which you can synthesize waves by adding to a sound's Fourier transform, and then inverse the transform to get the modified sound. In order to do this, I need to know what to add to the DFT to synthesize a pure wave. I've tried to learn about Fourier synthesis from many sources, but they all talk about the Fourier series instead of the Fourier transform, and they all say that for a pure wave all you need is a value in the coefficient of that wave's frequency, and 0 everywhere else. But the Fourier transform being a continuous function. And you can see here what the Fourier transform of a pure 12KHz sine wave looks like (on a logarithmic scale). As you can see, it's not just an instantaneous peak and 0 everywhere else.

So I tried to do the math myself. To put it formally, say we have a sampled signal $$S_t=A cos(\frac{-2 \pi k}{N} t + \phi)$$, where , $$N$$ is the number of samples in our signal, $$0 \le t < N$$, $$A$$ is an amplitude $$0 \le k < \frac{N}{2}$$ determines the wave's frequency, and $$\phi$$ is the phase of the wave. The DFT of this signal would be a sequence $$F_0,...,F_{N-1}$$ where:

$$F_r = \sum_{t=0}^{N-1}{{S_t}e^{\frac{-2 \pi i t r}{N}}}$$

My hope was that since our signal is a pure wave, there will be a direct formula for calculating $$F_r$$ that can be computed much quicker than by directly doing the math above, and that that formula will be the shape of that spike in the image I linked, because that's what I'm really interested in. For the sake of simplicity, I assumed that $$\phi=0$$, since once I figure out how to solve that case I'll probably be able to generalize it. I won't bore you with all the math because it's not important and you probably already know the answer. What I got in the end is that $$F_k = F_{N-k} = \frac{NA}{2}$$, and for all other $$0 \le r < N, F_r = 0$$.

Shocker. So in the end I found that it does equal to 0 everywhere except the frequency of the wave. But then I don't understand where that spike shape from the graph I linked is coming from. I'm still after it though, because just adding to the one sample which corresponds to the frequency I'm interested in doesn't work for me (I've tried).

So I guess my question is: how come plotting the Fourier transform of a pure wave doesn't actually produce zero in all samples except the one corresponding to that wave's frequency, and what's the formula for that spike that you do see.

• One word: Leakage. You don't hit the frequency of your DFT bin exactly exactly, and what you see is the sinc interpolation that produces. – Marcus Müller Oct 11 '20 at 11:00
• See here and here. Beware those who only explain the DFT "in light of" (continuous) FT; DFT is its own transform. – OverLordGoldDragon Oct 11 '20 at 18:06

First of all, welcome to DSP SE.

What you see in the image you have linked is termed (spectral) leakage. When you are dealing with the Fourier series you deal with a periodic continuous function which is "decomposed" into a (possibly) infinite sum (series). Then, when you go to the Fourier transform, you have a non-periodic function (which you could possibly presume to be periodic in some interval if, for example, it starts and ends on the x-axis) which is decomposed into an infinite sum of spectral components (you have energy in every point on the spectrum).

Before moving further on, please note that this is a very brief explanation about the Fourier series and Fourier transform with a lot of details omitted and the mathematical interpretation presented here in more of a convenient way than the absolutely correct. For more information on the topic, I would strongly suggest the textbook Mathematical Methods for Engineers and Scientists 3 - Fourier Analysis, Partial Differential Equations and Variational Methods by Dr Kwong-Tin Tang (the first part of the book is relevant here).

Now, if you go from the continuous domain to the discrete domain (we will deal with the discretisation of the free variable here, which is time and not the amplitude as is the reality when you deal with digital signals) you move from the Fourier transform to the Discrete Fourier Transform (DFT). Once again, omitting the "technical" details, in DFT when the signal contains frequency (spectral) components that do not make an integer number of complete periods in the duration of the signal to be analysed, you end up with the phenomenon called leakage (see link above). This is due to the "nature" of the transform, in which the signal is assumed to be periodic. Thus, when you have spectral components that do not complete an integer number of periods in the duration of the signal, you will end up with an amplitude (for this component) other than zero at either the end or the beginning of the signal. If you try to "copy and paste" the signal before and after your original signal (in order to make it periodic for the purpose of the transform) you will realise that you end up with some discontinuity at the point where the original and the "copy-and-pasted" signal starts. In order to reconstruct this discontinuity, you have to introduce an infinite amount of spectral components, which show up as energy in frequencies around the main spectral component (in the case you only have a "pure" (co)sine wave). You can see an example of a single frequency in the picture below.

Now, another concept you will most probably encounter quite often (if not always!) when dealing with DFT is the term window. In the simple case where you apply no windowing function (bear with me a wee bit more for an explanation) to the signal is like applying a "rectangular" window where you multiply all the values of your signal with one, thus, effectively doing nothing to them.

If you apply a windowing function to your signal you effectively suppress the signal at its edges in order to avoid the appearance of discontinuities such as those visible in the second plot on the right-hand side of the image above. Thus you somehow suppress the leakage effect. Please keep in mind that the simplest way of applying a windowing function is to element-wise multiply the samples of your signal with the windowing function in the time domain.

The signal on the right-hand side of the above image after the application of a Hann window would look like in the image below

And for a "clearer" representation of the spectrum before and after the windowing you can see this image

You can see that the peak is not so well localised but the leakage has been constrained to a smaller "neighbourhood" than before. For more information on windowing and window functions, you can have a look at the Wikipedia page (link above at the word "window") and any elementary Digital Signal Processing textbook such as the Digital Signal Processing - Principles Algorithms and Applications by Proakis & Manolakis or Introduction to Signal Processing by Orfanidis (which is freely distributed).

A pure single frequency sinusoid is infinite in duration. Once you chop it short to fit in a DFT (and the real universe), the finite length cut adds windowing artifacts to the perfect spectrum of the infinite length sinusoid.

For a pure sinusoid in zero noise, you only need 3 or 4 non-aliased points to solve for the 3 unknowns in ia pure sine waves specifying equation. Add noise, and now you need all the points to estimate what’s hidden in the noise.