## Definition of DFT and IDFT

Let’s begin by defining an N-point grid in $[0,2\pi)$. That is, a grid which starts from zero and doesn’t contain the end-point.

$$\lbrace x_j \rbrace_{0 \le j \le N-1} = \left\lbrace \frac{2\pi j}{N} : \quad 0 \le j \le N-1 \right\rbrace$$

Given a vector $v = \lbrace v_j \rbrace_{0 \le j \le N-1}$, the DFT computes:

$$\mathrm{DFT}(v)[k] = \sum_{j=0}^{N-1} e^{-ik x_j}v_j \qquad 0 \le k \le N-1$$

and the corresponding IFFT of a vector v yields

$$\mathrm{IDFT}(v)[j] = \frac{1}{N} \sum_{k=0}^{N-1} e^{ik x_j}v_{k} \qquad 0 \le j \le N-1$$

## Aliasing and Negative Frequencies

I want to highlight this property early on, because it is usually leads to the most confusing errors when we intend to use FFTs in our codes.

In view of the definition we just introduced, the original FFT frequencies appear to be

$$k = 0, \dots, N-1$$

But there is a tricky thing going on behind the scenes. It turns out that, for an N-point grid such as ours, we have the following identity:

$$e^{i N m x_j} = 1 \qquad m \in \mathbf{Z}$$

This implies, in particular, that Fourier modes with frequencies above $N/2$ (also referred to as the ‘Nyquist frequency’) are equivalent, in our finite grid, to negative-frequency modes with $k > -N/2$.

In many cases, it becomes necessary to remove frequencies above Nyquist, and restate the IDFT in the more convenient form

$$\frac{1}{N} \sum_{k=-N/2}^{N/2-1} e^{ik x_j}w_k \quad j = 1,…,N.$$

This simple fact is the source of much of the confusion which usually strikes someone willing to use the FFT algorithm in various applications.

In order to use this last expression, the original FFT coefficients $v_k$ must be reordered into the $w_k$ coefficients illustrated in the above formula. This reordering is handled with an fftshift function, which is a part of almost any FFT library.

## Real FFT

The real fft computes the same transform as defined above, but employs certain symmetry relations for increased performance.

In particular, in the case the input signal is real, it’s transform will have hermitian symmetry:

$$\mathrm{DFT}(v)[j] = \mathrm{DFT}(v)[n-j]^*, j=0,\dots,n$$

where we have extended the definition of the DFT to the negative indices by periodicity.

Computationally, this property is used by FFT libraries to obtain a function, usually referred as RFFT, which avoids computing the duplicate data and obtains a 2X improvement in computing time and storage.

The rfft returns a vector of size $N/2 + 1$, where the division by two is rounded down, containing the first coefficients of the FFT.