DFT of a Sine Wave - The Math
Sine and Cosine¶
Sine and cosine are identical in the magnitude spectrum. Both show only two delta impulses - one at the negative and one at the positive frequency of the sine or cosine. Since the two functions differ only in phase, their phase spectrum is different.
$$ \begin{eqnarray} \cos(\omega t) &~& \mapsto &~& \pi \left[\delta(\omega -\omega_0 ) + \delta(\omega +\omega_0 ) \right] \\ \cos(\omega t) &~& \mapsto &~& \frac{\pi}{j} \left[\delta(\omega -\omega_0 ) - \delta(\omega +\omega_0 ) \right] \\ \end{eqnarray} $$
DFT of a Sine Wave¶
In the field of musical signal processing, the sine wave (respectively the cosine wave) are the basic elements of complex sounds. They can be used to model and synthesize any periodic signal. Hence, the frequency domain representation of these harmonic functions is fundamental to the understanding of many algorithms for analysis and synthesis.
In many visualizations, a sinusoidal spectrum is shown as a single peak at the oscillation's frequency. However, this is only true for a signal of infinite length. When viewed closely, these peaks are smeared and accompanied by several so called side lobes. This is the so called spectral leakage effect. The following example derives these characteristics, based on a $1024$ sample sine wave with a frequency of $f_0 = 100\ \mathrm{Hz}$ at a sampling rate of $f_s = 16\ \mathrm{kHz}$:
For calculating the DFT of sinusoidal signals, it makes sense to express them in the complex notation through Euler's formula:
$$ \sin \left(2 \pi f_0 \frac{n}{fs} \right) = \frac{1}{2j} \left( e^{j 2 \pi f_0 \frac{n}{fs} } -e^{-j2 \pi f_0 \frac{n}{fs}} \right) \\ $$
The DFT of the sine wave thus extends to:
$$ X[k] = \sum\limits_{n=0}^{N-1} \frac{1}{2j} \left(e^{j 2 \pi \frac{f_0}{fs} n} -e^{-j2 \pi \frac{f_0}{fs} n} \right) e^{-j 2 \pi k \frac{n}{N}} \\ $$
Solving:
$$ \begin{eqnarray} X[k] & = & \frac{1}{2j} \sum\limits_{i=0}^{N-1} e^{- j 2 \pi k \frac{n}{N} + j 2 \pi \frac{f_0}{fs} n } - e^{- j 2 \pi k \frac{n}{N} - j 2 \pi\frac{f_0}{fs} n} \\ % % & = & \frac{1}{2j} \sum\limits_{i=0}^{N-1} e^{- j 2 \pi ( \frac{k}{N} - \frac{f_0}{fs} ) n } - e^{- j 2 \pi ( \frac{k}{N} + \frac{f_0}{fs}) n} \\ % % & = & \frac{1}{2j} \sum\limits_{i=0}^{N-1} e^{- j 2 \pi ( \frac{k}{N} - \frac{f_0}{fs} ) n } - \frac{1}{2j} \sum\limits_{i=0}^{N-1} e^{- j 2 \pi ( \frac{k}{N} + \frac{f_0}{fs}) n} \\ % % \end{eqnarray} $$
Geometric Series
Using the geometric series formula (with acknowledgements to 1)
$$ \sum\limits_{n=0}^{N-1} a^n = \frac{1-a^{N}}{1-a} $$
the above equation results in:
$$ X[k] = \frac{1}{2j} \frac{1-e^{-j 2 \pi \left( \frac{k}{N} - \frac{f_0}{fs} \right) N}}{1-e^{-j 2 \pi \left( \frac{k}{N} - \frac{f_0}{fs} \right)}} % + \frac{1}{2j} \frac{1-e^{-j 2 \pi (\left( \frac{k}{N} + \frac{f_0}{fs} \right)) N}}{1-e^{-j 2 \pi \left( \frac{k}{N} + \frac{f_0}{fs} \right)}} $$
Factoring out
The above equation features the following term:
$$ E = \frac{1-e^{-j \Lambda N}}{1-e^{-j \Lambda}} $$
After factoring out the term $\frac{e^{-j \Lambda \frac{N}{2}}}{e^{-j \frac{\Lambda}{2}}}$ we can solve further, using Euler's formula:
$$ \begin{eqnarray} E & = & \frac{e^{-j \Lambda \frac{N}{2}}} {e^{-j \frac{\Lambda}{2}}} \cdot \frac{e^{j \Lambda \frac{N}{2}} - e^{-j \Lambda \frac{N}{2}}}{e^{j \frac{\Lambda}{2}} - e^{-j \frac{\Lambda}{2}}} \\ % % & = & e^{-j \Lambda \frac{N+1}{2} } \underbrace{\frac{\sin(\Lambda \frac{N}{2} )}{\sin(\frac{\Lambda}{2})}}_{\text{Dirichlet function}} \end{eqnarray} $$
As the plot below shows, the term is characterized by a Dirichlet function, which is related to the sinc
function. Inserting
$$ \begin{eqnarray} \Lambda(-f_0) & = & 2 \pi \left( \frac{k}{N} - \frac{f_0}{fs} \right) \\ \Lambda(+f_0) & = & 2 \pi \left( \frac{k}{N} + \frac{f_0}{fs} \right) \\ \end{eqnarray} $$
we get the following result for the spectrum of the sinusoid:
$$ \begin{eqnarray} X [k] & = & \frac{1}{2j} \left( e^{-j \Lambda(-f_0) \frac{N+1}{2} } \frac{\sin(\Lambda(-f_0) \frac{N}{2} )}{\sin(\frac{\Lambda(-f_0) }{2})} % + e^{-j \Lambda(+f_0) \frac{N+1}{2} } \frac{\sin(\Lambda(+f_0) \frac{N}{2} )}{\sin(\frac{\Lambda(+f_0)}{2})} \right) \end{eqnarray} $$
According to the shift theorem, the above equation holds two components - one cenered at $f_0$, another centered at $-f_0$, each with a main lobe and an infinite number of sidelobes: