# Spectral Leakage *Leakage* is a phenomena that occurs when the input sequence $x[n]$ contains frequencies other than the analytical frequencies associated with each *bin* of the DFT output. A *bin* simply refers to a single frequency-domain sample in the DFT output. The analytical frequencies of the DFT are: $$f_{analysis} = \frac{m \, F_s}{N} \text{, where } m = \text{0, 1, 2, ..., N-1}$$ When $x[n]$ contains frequencies other than the analytical frequencies, the energy from those frequencies *leak* into all of the other bins in the DFT output. This makes the DFT only an approximation of frequency content for real-world finite-length time sequences. To better understand how leakage affects the amplitude response of the DFT output, we will analyze what happens when the DFT's input sequence is an arbitrary, real sinusoid. When the input sequence is an *N-point* cosine wave containing $k$ cycles (where k can be any real value, not just integers), the amplitude response of an *N-point* DFT bin in terms of the bin index $m$ is *approximated* by the sinc function: $$X(m) = \frac{A_0 \, N}{2} \cdot \frac{sin\left[ \pi (k-m)\right]}{\pi (k-m)}$$ where $A_0$ is the peak value of the DFT's input sinusoid. An amplitude and magnitude plot for this sinc function is provided below.
## DTFT of a Truncated Sinusoid to Better Understand Leakage As stated above, the sinc function is only an *approximation* for the DFT response. As the input frequenecy moves further away from bin center this approximation loses accuracy. To understand the true nature of leakage across all frequencies, we can calculate the discrete-time Fourier transform (DTFT) of a truncated sinusoid. The DTFT is the continuous Fourier transform of an *N*-point discrete time-domain sequence, defined as: $$X(\omega) = \sum_{n=-\infty}^{\infty} x[n] \, e^{-j \omega n}$$ The formula for a truncated sinusoid is: $$sin\left(\frac{2 \pi \, k \, n}{N} + \theta \right) \cdot \left( u[n] - u[n-N] \right)$$ where $k$ is the number of sinusoidal cycles that occur over $N$ samples, and $u[n]$ is the unit step function where: $$u[n] = \begin{cases} 0 \; : \; n \lt 0 \\ 1 \; : \; n \ge 0 \\ \end{cases}$$ An illustration of the truncated sinusoid is provided below.
In order to calculate the DTFT of the truncated sinusoid, we can use a table of discrete-time Fourier transform pairs to find the following: $$\begin{aligned} & \underline{\quad x[n] \quad } & \hspace{12em} & \underline{ \quad X(\omega) \quad } \\ & \hspace{-1.9em} A_0 \, sin(\omega_0 n \, + \, \theta) & & \hspace{-6.5em} A_0 \, \frac{\pi}{j} \left[e^{j \theta} \delta (\omega - \omega_0) - e^{-j \theta} \delta (\omega + \omega_0) \right] , \quad -\pi \lt \omega \lt \pi \\ & \hspace{-1.5em} u[n] \, - \, u[n - N] & & \hspace{-2.5em} e^{-j \omega \frac{N-1}{2}} \cdot \frac{sin\left( \frac{\omega}{2} N\right)}{sin\left( \frac{\omega}{2}\right)} \\ \end{aligned}$$ Looking at the table of discrete-time Fourier transform properties we see that: $$\begin{aligned} & \underline{\quad \text{Property} \quad} & \hspace{3em} & \underline{\quad \text{Time Domain} \quad} & \hspace{3em} & \underline{\quad \text{DTFT Domain} \quad} \\ & \text{Multiplication} & & \hspace{2.15em} a[n] \, b[n] & & \hspace{-1.5em} \frac{1}{2 \pi} \int_{-\pi}^{\pi} A(\phi) \, B(\omega - \phi) \, d \phi\\ \end{aligned}$$ Using the multiplication property of the DTFT with the truncated sinusoid we get: $$A_0 \, sin \left(\omega_0 n \, + \, \theta \right) \cdot \left(u[n] \, - \, u[n-N] \right)$$ $$\Bigg\Downarrow \, \text{DTFT}$$ $$X(\omega) = \, \frac{1}{2 \pi} \int_{-\pi}^{\pi} A(\phi) \, B(\omega - \phi) \, d \phi$$ $$= \frac{1}{2 \pi} \int_{-\pi}^{\pi} \, \left[ A_0 \, \frac{\pi}{j} \left[e^{j \theta} \delta (\phi - \omega_0) - e^{-j \theta}\delta (\phi + \omega_0) \right] \cdot e^{-j (\omega - \phi) \frac{N-1}{2}} \cdot \frac{sin\left( \frac{(\omega - \phi)}{2} N\right)}{sin\left( \frac{(\omega - \phi)}{2}\right)} \right] \hspace{0.5em} d\phi$$ $$= \frac{A_0}{2 j} \left[ e^{-j \left[ (\omega - \omega_0) \frac{N-1}{2} - \theta \right]} \cdot \frac{sin\left( \frac{(\omega - \omega_0)}{2} N\right)}{sin\left( \frac{(\omega - \omega_0)}{2}\right)} \, - \, e^{-j \left[ (\omega + \omega_0) \frac{N-1}{2} + \theta \right] } \cdot \frac{sin\left( \frac{(\omega + \omega_0)}{2} N\right)}{sin\left( \frac{(\omega + \omega_0)}{2}\right)} \right]$$ Now we can let $\omega = m \frac{2 \pi}{N}$, where $m$ is the DFT bin index that we are interested in measuring the leakage at and we can let $\omega_0 = k \frac{2 \pi}{N}$, where $k$ is a real number representing the distance in bins that the input sinusoid is from $m$. Substituting these values into $X(\omega)$ and simplifying a bit we get: $$X(m) = \frac{A_0}{2 j} \left[ e^{-j \left[ (\pi (m - k) \frac{N-1}{N}) - \theta \right]} \cdot \frac{sin\left(\pi (k - m)\right)}{sin\left( \frac{1}{N} \pi (k - m) \right)} \, - \, e^{-j \left[ (\pi (m + k) \frac{N-1}{N}) + \theta \right] } \cdot \frac{sin\left(\pi (k + m)\right)}{sin\left( \frac{1}{N} \pi (k + m) \right)} \right]$$ This equation $X(m)$ can be used to calculate the leakage for any sinusoidal input in bin $m$ of the DFT output. For a typical sinusoidal input, $k$ is related to the input frequency $f$ by the following equation: $$k = f \frac{N}{F_s}$$ The example below calculates an *N*-point fast Fourier tansform (FFT), which is just an efficient implementation of the DFT, on a sinusoidal input with arbitrary phase and frequency. These DFT outputs are then superimposed on top of the DTFT of the input, $X(m)$, to illustrate how the signal energy gets spread out across multiple DFT bins.
## Leakage and the Cyclic Nature of the DFT It is important to remember that the DFT is periodic about $2 \pi$, which means that leakage effects can bleed over across the $0 \pi .. 2 \pi$ boundary. It can be useful to visualize the DFT as a circular plot in 3 dimensions to better understand the cyclic nature of the DFT. Below is an example of a 3D plot of the DFT and DTFT for an arbitrary sinusoid of low frequency with respect to the sample rate. This illustrates how the spectral content can bleed across the $0 \pi .. 2 \pi$ boundary.