---
author:
- "[Eugeniy E. Mikhailov](http://physics.wm.edu/~evmik/) and [Greg
  Bentsen](https://physics.wm.edu/~gbentsen/)"
title: Fourier transform
---

## Agenda

-   This week:  Fourier transforms
-   Nov 23: Remote class  *(Nov 25 - 29:  Thanksgiving)*
-   Nov 30 - Dec 4:  Machine learning & review

Final exam

-   Dec 9 or Dec 11
-   Same 2-part structure as midterms (take-home, in-class)

## Today

Fourier transform

-   Fourier series
-   Discrete Fourier Transform
-   Fast Fourier Transform
-   Applications

### Next lecture

-   Digital filters

## Fourier series

::: columns
::: {.column width="60% "}
Any periodic single value function with a finite number of
discontinuities, and for which $\int_0^T|f(t)|dt$ is finite, can be
presented as

::: definition
$$\begin{aligned}
        y(t)=\frac{a_0}{2} + \sum_1^\infty \left(a_n \cos(n \omega_1 t) + b_n
        \sin( n \omega_1 t)  \right)
\end{aligned}$$ where $$\begin{aligned}
    \begin{pmatrix}
        a_n\\
        b_n
    \end{pmatrix}
    = \frac{2}{T} \int_0^T dt 
    \begin{pmatrix}
        \cos (n \omega_1 t)\\
        \sin (n \omega_1 t)
    \end{pmatrix}
    y(t)
\end{aligned}$$
:::

$T$
:   is the period, i.e., $y(t)=y(t+T)$

$\omega_1 =2 \pi/T$
:   is the fundamental frequency

Note that $a_0/2 = <y(t)>_T$, i.e. average of the function over the
period
:::

::: {.column width="40%"}
![Periodic
function](./pics/plot_periodic_function_example.png){width="100%"}
:::
:::

## Fourier series example: \|t\|

::: columns
::: {.column width="45%"}
$$\begin{aligned}
    y(t)=|t|, ~ ~ -\pi < t < \pi
\end{aligned}$$ Since the function is even all $b_n=0$ $$\begin{aligned}
 \begin{cases}
    a_0 = \pi, & \\
    a_n = 0, &  n ~ ~ \text{is even} \\
    a_n =  -\frac{4}{\pi n^2}, &  n ~ ~ \text{is odd}
    \end{cases}
\end{aligned}$$
:::

::: {.column width="45%"}
![](./pics/plot_abs_a_coef.png){width="80%"}
:::
:::

::: columns
::: {.column width="33%"}
![](./pics/plot_abs_approximation_N_1.png){width="99%"}
:::

::: {.column width="33%"}
![](./pics/plot_abs_approximation_N_3.png){width="99%"}
:::

::: {.column width="33%"}
![](./pics/plot_abs_approximation_N_11.png){width="99%"}
:::
:::

## Fourier series example: step function

::: columns
::: {.column width="45%"}
$$\begin{aligned}
 \begin{cases}
      0, & -\pi < x < 0,\\
        1, & 0 < x < \pi
    \end{cases}
\end{aligned}$$ Since the function is odd all $a_n=0$ except $a_0 = 1$
$$\begin{aligned}
 \begin{cases}
    b_n = 0, &  n ~ ~ \text{is even} \\
    b_n =  \frac{2}{\pi n}, &  n ~ ~ \text{is odd}
    \end{cases}
\end{aligned}$$

At a discontinuity, the series approaches the mid point
:::

::: {.column width="45%"}
![](./pics/plot_step_b_coef.png){width="80%"}
:::
:::

::: columns
::: {.column width="33%"}
![](./pics/plot_step_approximation_N_1.png){width="99%"}
:::

::: {.column width="33%"}
![](./pics/plot_step_approximation_N_10.png){width="99%"}
:::

::: {.column width="33%"}
![](./pics/plot_step_approximation_N_100.png){width="99%"}
:::
:::

## The Gibbs phenomenon

Notice the overshoots, undershoots, and oscillations near discontinuity

::: columns
::: {.column width="50%"}
![](./pics/plot_step_approximation_N_100.png){width="99%"}
:::

::: {.column width="50%"}
![](./pics/plot_step_approximation_N_1000.png){width="99%"}
:::
:::

## Complex representation

Recall that $$\begin{aligned}
    \exp( i \omega t) = \cos( \omega t) + i \sin(\omega t)
\end{aligned}$$

It can be shown that

::: definition
$$\begin{aligned}
    y(t) &=&  \sum_{n=-\infty}^\infty c_n \exp(i n \omega_1 t) \\
    c_n &=&   \frac{1}{T}\int_0^T y(t) \exp( - i \omega_1 n t) dt
\end{aligned}$$
:::

$$\begin{aligned}
    a_n &=&  c_n + c_{-n} \\
    b_n &=&  i(c_n - c_{-n})
\end{aligned}$$

## What to do if function is not periodic?

-   $T \to \infty$
-   $\sum \to \int$
-   discrete spectrum $\to$ continuous spectrum
    -   $c_n \to c_\omega$

$$\begin{aligned}
        y(t) &=& \frac{1}{\sqrt{2 \pi}} \int_{-\infty}^\infty c_\omega \exp(i
        \omega t) d \omega \\
        c_\omega &=&   \frac{1}{\sqrt{2 \pi}}\int_{-\infty}^\infty y(t) \exp( - i \omega t) dt
\end{aligned}$$ `\alert{Above requires}`{=tex} that
$\int_{-\infty}^\infty dt~ y(t)$ exists and is finite.

`\alert{Note}`{=tex} that $c_\omega$ has the extra $\sqrt{2 \pi}$ when
compared to $c_n$, and $T$ is gone.

## Discrete Fourier transform (DFT)

::: columns
::: {.column width="60% "}
In reality, `\alert{we cannot have}`{=tex}

-   infinitely large interval
-   infinite amount of points to calculate true integral

Assuming that $y(t)$ has a period $T$ and we took $N$
`\alert{equidistant}`{=tex} points $$\begin{aligned}
        \Delta t &=&  \frac{T}{N} &  ~ ~ \text{samples spacing,}\\
        f_s &=& \frac{1}{\Delta t} & ~ ~ \text{sampling rate,}\\
        f_1 &=& \frac{1}{T} = \frac{1}{N \Delta t} & ~ ~ \text{smallest observed frequency,} \\
        & & & ~ ~ \text{also resolution bandwidth,}\\
        t_k&=&  \Delta t \times k & \\
        y( t_{k+N}) &=&  y(t_{k}) & ~ ~ \text{periodicity condition,} \\
        y_{k} &=&  y(t_k) & ~ ~ \text{shortcut notation,} \\
        y_0, y_1, y_2, y_3, \cdots, y_{N-1} & & & ~ ~ \text{data set}
\end{aligned}$$ We replace the integral in the Fourier series with the
sum
:::

::: {.column width="40% "}
![Time representation](./pics/signal_vs_time.svg){width="99%"} ``{=html}
:::
:::

## DFT

$$\begin{aligned}
        c_n &=&  \sum_{k=0}^{N-1} y_k \exp(-i\frac{ 2 \pi k n}{N}) 
        ~ ~ \text{ Fourier transform} \\
        y_k &=& \frac{1}{N} \sum_{n=0}^{N-1} c_n \exp(i\frac{ 2 \pi k n}{N} ) 
        ~ ~ \text{ inverse Fourier transform} \\
        n &=& 0, 1, 2, \cdots,  N-1
\end{aligned}$$

Notice that $y_k$ calculation has $1/N$ factor and $c_n$ is not. This
just one of the possible normalization. Some put $1/N$ up front of
$c_n$, and some put $1/\sqrt{N}$ in front of both.

Confusion keeps increasing: where are the negative coefficients $c_{-n}$
? In DFT, they moved to the right end of the $c_n$ vector :
$$\begin{aligned}
        c_{-n}=c_{N-n}
\end{aligned}$$

## Fast Fourier transform (FFT)

Fast numerical realization of DFT is FFT. This is just the smart way to
do DFT. Python has one built in either `numpy` or `scipy` packages

-   $y$ is a python vector of data points ($y_k$)
-   `c=np.fft.fft(y)`{.python} Fourier transform
-   `y=np.fft.ifft(c)`{.python} inverse Fourier transform

Both `fft` and `ifft` have `norm` parameter which decides at which
points to account for $N$. Make sure you understand it. This is usually
the reason when calculated coefficients do not make "physical" sense at
the first glance.

`y = np.fft.ifft(np.fft.fft(y))`
