# Discrete Fourier Transform
The discrete Fourier transform (DFT) is a mathematical procedure used to determine the harmonic or frequency content in a discrete time signal sequence. The DFT originates from the continuous Fourier transform, $X(f)$, defined as follows:
$$X(f) = \int_{-\infty}^{\infty} x(t) e^{-j 2 \pi f t}\,dt \ $$
where $x(t)$ is some continuous time-domain signal.
The DFT is defined as the discrete frequency-domain sequence, $X(m)$, defined as follows: $$\begin{aligned} X(m) & = \sum_{n=0}^{N-1} x(n) e^{-j \, 2 \pi \frac{n \, m}{N}} \quad \quad &\text{Exponential Form} \\ \\ X(m) & = \sum_{n=0}^{N-1} x(n) \left[cos \left( 2 \pi \, \frac{n \, m}{N} \right) \, - \, j \, sin \left(2 \pi \, \frac{n \, m}{N} \right) \right] \quad \quad &\text{Rectangular Form} \end{aligned}$$ where $x(n)$ is a discrete sequence of time-domain sampled values of the continuous variable $x(t)$, $e$ is th base of natural logarithms and $j=\sqrt{-1}$. The value of $N$ in the DFT equation determines the following things: 1. The number of input samples that are needed 1. The resolution of the frequency-domain results 1. The amount of processing time necessary to compute an N-point DFT
The DFT is defined as the discrete frequency-domain sequence, $X(m)$, defined as follows: $$\begin{aligned} X(m) & = \sum_{n=0}^{N-1} x(n) e^{-j \, 2 \pi \frac{n \, m}{N}} \quad \quad &\text{Exponential Form} \\ \\ X(m) & = \sum_{n=0}^{N-1} x(n) \left[cos \left( 2 \pi \, \frac{n \, m}{N} \right) \, - \, j \, sin \left(2 \pi \, \frac{n \, m}{N} \right) \right] \quad \quad &\text{Rectangular Form} \end{aligned}$$ where $x(n)$ is a discrete sequence of time-domain sampled values of the continuous variable $x(t)$, $e$ is th base of natural logarithms and $j=\sqrt{-1}$. The value of $N$ in the DFT equation determines the following things: 1. The number of input samples that are needed 1. The resolution of the frequency-domain results 1. The amount of processing time necessary to compute an N-point DFT
### DFT Concrete Example
In this concrete DFT example we will let $N=4$. The rectangular form of this DFT is:
$$X(m) = \sum_{n=0}^{3} x(n) \left[cos \left(2 \pi \, \frac{n \, m}{4} \right) \, - \, j \, sin \left(2 \pi \, \frac{n \, m}{4} \right) \right]$$
$$\bigg\downarrow$$
$$\begin{aligned}
X(0) = \quad & x(0) \, cos\left(2 \pi \, \frac{0 \cdot 0}{4}\right) \, - \, j \, x(0) \, sin\left(2 \pi \, \frac{0 \cdot 0}{4}\right) \\
+ \, & x(1) \, cos\left(2 \pi \, \frac{1 \cdot 0}{4}\right) \, - \, j \, x(1) \, sin\left(2 \pi \, \frac{1 \cdot 0}{4}\right) \\
+ \, & x(2) \, cos\left(2 \pi \, \frac{2 \cdot 0}{4}\right) \, - \, j \, x(2) \, sin\left(2 \pi \, \frac{2 \cdot 0}{4}\right) \\
+ \, & x(3) \, cos\left(2 \pi \, \frac{3 \cdot 0}{4}\right) \, - \, j \, x(3) \, sin\left(2 \pi \, \frac{3 \cdot 0}{4}\right) \\
\end{aligned}$$
$$\vdots$$
$$\begin{aligned}
X(1) = \quad & x(0) \, cos\left(2 \pi \, \frac{0 \cdot 1}{4}\right) \, - \, j \, x(0) \, sin\left(2 \pi \, \frac{0 \cdot 1}{4}\right) \\
+ \, & x(1) \, cos\left(2 \pi \, \frac{1 \cdot 1}{4}\right) \, - \, j \, x(1) \, sin\left(2 \pi \, \frac{1 \cdot 1}{4}\right) \\
+ \, & x(2) \, cos\left(2 \pi \, \frac{2 \cdot 1}{4}\right) \, - \, j \, x(2) \, sin\left(2 \pi \, \frac{2 \cdot 1}{4}\right) \\
+ \, & x(3) \, cos\left(2 \pi \, \frac{3 \cdot 1}{4}\right) \, - \, j \, x(3) \, sin\left(2 \pi \, \frac{3 \cdot 1}{4}\right) \\
\end{aligned}$$
$$\vdots$$
$$\begin{aligned}
X(2) = \quad & x(0) \, cos\left(2 \pi \, \frac{0 \cdot 2}{4}\right) \, - \, j \, x(0) \, sin\left(2 \pi \, \frac{0 \cdot 2}{4}\right) \\
+ \, & x(1) \, cos\left(2 \pi \, \frac{1 \cdot 2}{4}\right) \, - \, j \, x(1) \, sin\left(2 \pi \, \frac{1 \cdot 2}{4}\right) \\
+ \, & x(2) \, cos\left(2 \pi \, \frac{2 \cdot 2}{4}\right) \, - \, j \, x(2) \, sin\left(2 \pi \, \frac{2 \cdot 2}{4}\right) \\
+ \, & x(3) \, cos\left(2 \pi \, \frac{3 \cdot 2}{4}\right) \, - \, j \, x(3) \, sin\left(2 \pi \, \frac{3 \cdot 2}{4}\right) \\
\end{aligned}$$
$$\vdots$$
$$\begin{aligned}
X(3) = \quad & x(0) \, cos\left(2 \pi \, \frac{0 \cdot 3}{4}\right) \, - \, j \, x(0) \, sin\left(2 \pi \, \frac{0 \cdot 3}{4}\right) \\
+ \, & x(1) \, cos\left(2 \pi \, \frac{1 \cdot 3}{4}\right) \, - \, j \, x(1) \, sin\left(2 \pi \, \frac{1 \cdot 3}{4}\right) \\
+ \, & x(2) \, cos\left(2 \pi \, \frac{2 \cdot 3}{4}\right) \, - \, j \, x(2) \, sin\left(2 \pi \, \frac{2 \cdot 3}{4}\right) \\
+ \, & x(3) \, cos\left(2 \pi \, \frac{3 \cdot 3}{4}\right) \, - \, j \, x(3) \, sin\left(2 \pi \, \frac{3 \cdot 3}{4}\right) \\
\end{aligned}$$
This process is illustrated in more detail in the code below. Feel free to adjust the input to see how the DFT calculation changes.
## DFT Symmetry
For a real valued input sequence, the complex DFT outputs will be symmetric about $m=\frac{N}{2}$. The real parts of the DFT output are *even symmetric* and the imaginary parts of the DFT output are *odd symmetric*. This means that the real DFT values on either side of $m=\frac{N}{2}$ have the same magnitude, but the imaginary DFT values on either side of $m=\frac{N}{2}$ have their signs flipped. The DFT magnitude is also *even symmetric* about $m=\frac{N}{2}$ and the phase is *odd symmetric* about $m=\frac{N}{2}$.
## DFT Linearity
The DFT has a property called *linearity* that states that the DFT of the sum of two signals is equal to the sum of the transforms of each signal. This allows us to take the DFT of signals that contain multiple different frequency components and we will arrive at the same result as if we took the DFT of each individual signal.
## DFT Magnitudes
The DFT output magnitudes are scaled by a factor of $N$, where $N$ is the length of the DFT. For a real input signal, the magnitude of the DFT output, $M_r$, will be equal to:
$$M_r = A_0 \, \frac{N}{2}$$
where $A_0$ is the amplitude of the real input signal.
For a complex input signal, the magnitude of the DFT output, $M_c$, will be equal to:
$$M_c = A_0 \, N$$
where $A_0$ is the magnitude of the complex input signal ($A_0 \, e^{j \, 2 \pi \, f \, n \, T_s}$).
## DFT Frequency Axis
The frequency that is associated with each value in $X(m)$ is a function of the sample rate, $F_s$. The formula to calculate the frequency for a given $m$ is:
$$f(m) = \frac{m \, F_s}{N}$$
## DFT Shifting Theorem
An important property of the DFT is known as the *shifting theorem*. This states that a shift in time of a periodic input sequencne manifests itself as a constant phase shift in the angles associated with the DFT results.
## Inverse DFT
The inverse discrete Fourier transform (IDFT) is a process that allows us to convert frequency-domain data back to the time-domain representation. The IDFT is defined by the following equations:
$$\begin{aligned}
x(n) & = \frac{1}{N} \sum_{m=0}^{N-1} X(m) e^{j \, 2 \pi \frac{m \, n}{N}} \quad \quad &\text{Exponential Form} \\
\\
x(n) & = \frac{1}{N} \sum_{m=0}^{N-1} X(m) \left[cos \left( 2 \pi \, \frac{m \, n}{N} \right) \, + \, j \, sin \left(2 \pi \, \frac{m \, n}{N} \right) \right] \quad \quad &\text{Rectangular Form} \end{aligned}$$
Notice that the two things that changed between the DFT and the IDFT equations are that:
1. The IDFT equation has a $\frac{1}{N}$ scale factor to normalize the gain incurred in the DFT.
1. The IDFT has the sign flipped in the exponent of the exponential form, and on the imaginary component in the rectangular form.