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

## Today

-   DFT and spectrum of the signal
-   Filters

## DFT recap

::: columns
::: {.column width="50%"}
Often in literature instead of $Y_n$ is used instead of $c_n$:
$$\begin{aligned}
        c_n = Y_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, k &=& 0, 1, 2, \cdots,  N-1 &
\end{aligned}$$
:::

::: {.column width="50%"}
The *operator* notation: $$\begin{aligned}
        Y&=&  F y 
        &~ ~ \text{Forward Fourier transform} \\
        y &=& F^{-1} Y
        &~ ~ \text{inverse Fourier transform}
\end{aligned}$$
:::
:::

Where the time domain signal $y=\{y_0, y_1, y_2, ..., y_{N-1}\}$ and
$Y=\{Y_0, Y_1, Y_2, ..., Y_{N-1}\}= \{c_0, c_1, c_2, ..., c_{N-1}\}$ is
its spectrum.

Note that DFT itself does not care about *time*, it is in the "eye of
the beholder" and usually accounted separately.

. . .

Recall that
$y_k = y(t_k)  =\frac{1}{N} \sum_{n=0}^{N-1} c_n \exp(i{ 2 \pi t_k f_n})$,
with $2 \pi f_n = \omega_n$

::: columns
::: {.column width="50%"}
![time domain](./pics/signal_vs_time.svg)

Sample spacing $\Delta t = T/N$.

Sample rate $f_s = 1/\Delta t = N/T$.
:::

::: {.column width="50%"}
![Spectrum (amplitude vs frequency)](./pics/spectrum_frequency.svg)

Resolution bandwidth $\Delta f = 1/T$.

Frequency of $n$-th component $f_n = n \Delta f$.
:::
:::

## Nyquist frequency

If we take $N$ data points with the sampling rate $f_s$, what is the
maximum frequency which we can expect to see in our spectrum?

`\alert{Naively}`{=tex}, we can say $(N-1) \times f_1 \approx f_s$,
since in the DFT spectrum all points are separated by the fundamental
frequency $f_1=1/T=f_s/N$

However, recall that
$$Y_n = c_n =  \sum_{k=0}^{N-1} y_k \exp(-i\frac{ 2 \pi k n}{N})$$

Thus, $Y_{N-n} = Y_{-n}$, i.e., the higher half of the vector $Y$
contains negative frequency. So at most, we can hope to obtain a
spectrum with the highest *real* frequency `\alert{smaller than}`{=tex}

::: definition
Nyquist frequency

$$\begin{aligned}
        F_{Nq}= f_1  \frac{N}{2} = \frac{f_s}{2}
\end{aligned}$$
:::

::: definition
Nyquist criteria

$$\begin{aligned}
    f_s > 2 f_{signal} & \text{ or } & f_{Nq} > f_s
\end{aligned}$$

`\alert{You must sample your signal more than twice faster than the highest frequency component of it.}`{=tex}
I.e., the Nyquist frequency of your sample should be $>$ than the
highest signal frequency.
:::

## Aliasing: wrong/slow sampling frequency

::: columns
::: {.column width="35%"}
Sampling with\
$f_s = 2f_{signal}$\
i.e.\
$f_{Nq} = f_{signal}$\
Sampled signal appeared to be DC
:::

::: {.column width="65%"}
![example of sampling at Nyquist
frequency](./pics/nyquist_frequency_sampling.png){width="90%"}
:::
:::

## Aliasing: too slow sampling frequency - reflection

::: columns
::: {.column width="35%"}
Under sampling $f_s = 1.1 f_{signal}$

The sampled signal seems to have a lower frequency.

The sampled signal appears to have a slower frequency. This is case of
the reflection/folding where the signal frequency is slightly higher
than the sampling frequency.

$f_{apparent~signal} = (f_{signal} - 2 f_{Nq}) \approx f_{signal} - f_s$
:::

::: {.column width="65%"}
![example undersampling](./pics/under_sampling.png){width="90%"}
:::
:::

## Aliasing: too slow sampling frequency - ghosts

::: columns
::: {.column width="35%"}
Under sampling $f_s = 1.93 f_{signal}$

The sampled signal looks very different.
:::

::: {.column width="65%"}
![example of under sampling](./pics/gost.png){width="90%"}
:::
:::

## DFT filters

Once you get a signal, you can filter the unwanted frequencies out of
it.

The recipe is the following

-   sample the signal
-   calculate DFT (use Python `numpy.fft.fft` )
-   have a look at the spectrum and decide which frequencies are
    unwanted
-   apply a filter which attenuates unwanted frequencies amplitudes
    -   If you attenuate the component of the frequency $f$ by $g_f$,
        you need to attenuate the component at $-f$ by $g_f^*$.
        Otherwise, the inverse Fourier transform will have non zero
        imaginary part.
-   calculate inverse DFT (`numpy.fft.ifft`) of the filtered spectrum
-   repeat if needed

$$\begin{aligned}
    y_{filtered} (t) = {F}^{-1} \left[ [F  y(t) ]  G(f) \right] =
    { F}^{-1} \left[  Y(f)   G(f) \right] 
\end{aligned}$$

Applications

-   noise reduction
-   compression

## Brick wall low-pass filter

::: columns
::: {.column width="30%"}
![](./pics/low_pass_signal.png){width="90%"}

![](./pics/low_pass_signal_spectrum.png){width="90%"}
:::

::: {.column width="40%"}
Filter gain function $$\begin{aligned}
                G(f) = 
                \begin{cases}
                    1, |f| \le f_{cut off}\\
                    0, |f| \gt f_{cut off}
                \end{cases}
\end{aligned}$$

![](./pics/low_pass_filter_spectrum.png){width="90%"}
:::

::: {.column width="30%"}
![](./pics/low_pass_filtered_signal.png){width="90%"}

![](./pics/low_pass_filtered_spectrum.png){width="90%"}
:::
:::

$$\begin{aligned}
    y_{filtered} (t) = {F}^{-1} \left[ {F} ( y(t) )  G(f) \right] =
    {F}^{-1} \left[  Y(f)   G(f) \right] 
\end{aligned}$$

``` python
freq = np.fft.fftfreq(N, dt)
g = np.ones(N)
g[np.abs(freq) > Fcutoff] = 0
y_filtered = np.fft.ifft( np.fft.fft(y) * g)
```

## Brick wall low-pass filter (continued)

![](./pics/low_pass_filtered_and_raw_signal.png){width="40%"}

## Brick wall high-pass filter

::: columns
::: {.column width="30%"}
![](./pics/high_pass_signal.png){width="90%"}

![](./pics/high_pass_signal_spectrum.png){width="90%"}
:::

::: {.column width="40%"}
Filter gain function $$\begin{aligned}
                G(f) = 
                \begin{cases}
                    1, |f| \ge f_{cut off}\\
                    0, |f| \lt f_{cut off}
                \end{cases}
\end{aligned}$$ ![](./pics/high_pass_filter_spectrum.png){width="90%"}
:::

::: {.column width="30%"}
![](./pics/high_pass_filtered_signal.png){width="90%"}

![](./pics/high_pass_filtered_spectrum.png){width="90%"}
:::
:::

$$\begin{aligned}
    y_{filtered} (t) = {F}^{-1} \left[ {F} ( y(t) )  G(f) \right] =
    {F}^{-1} \left[  Y(f)   G(f) \right] 
\end{aligned}$$

``` python
freq = np.fft.fftfreq(N, dt)
g = np.ones(N)
g[np.abs(freq) < Fcutoff] = 0
y_filtered = np.fft.ifft( np.fft.fft(y) * g)
```

## Brick wall high-pass filter (continued)

![](./pics/high_pass_filtered_and_raw_signal.png){width="40%"}

## Brick wall band-pass filter

::: columns
::: {.column width="30%"}
![](./pics/band_pass_signal.png){width="90%"}

![](./pics/band_pass_signal_spectrum.png){width="90%"}
:::

::: {.column width="40%"}
Filter gain function $$\begin{aligned}
                G(f) = 
                \begin{cases}
                    1, | |f|-f_c| \le \frac{f_{bw}}{2}\\
                    0, | |f|-f_c| \gt \frac{f_{bw}}{2}
                \end{cases}
\end{aligned}$$ ![](./pics/band_pass_filter_spectrum.png){width="90%"}
:::

::: {.column width="30%"}
![](./pics/band_pass_filtered_signal.png){width="90%"}

![](./pics/band_pass_filtered_spectrum.png){width="90%"}
:::
:::

## Brick wall band-pass filter (continued)

![](./pics/band_pass_filtered_and_raw_signal.png){width="40%"}

## Brick wall band-stop filter

::: columns
::: {.column width="30%"}
![](./pics/band_cut_signal.png){width="90%"}

![](./pics/band_cut_signal_spectrum.png){width="90%"}
:::

::: {.column width="40%"}
Filter gain function $$\begin{aligned}
                G(f) = 
                \begin{cases}
                    0, | |f|-f_c| \le \frac{f_{bw}}{2}\\
                    1, | |f|-f_c| \gt \frac{f_{bw}}{2}
                \end{cases}
\end{aligned}$$ ![](./pics/band_cut_filter_spectrum.png){width="90%"}
:::

::: {.column width="30%"}
![](./pics/band_cut_filtered_signal.png){width="90%"}

![](./pics/band_cut_filtered_spectrum.png){width="90%"}
:::
:::

## Brick wall band-stop filter (continued)

![](./pics/band_cut_filtered_and_raw_signal.png){width="40%"}

## Brick wall filters artifacts

Sharp features in the Fourier spectrum produce ring-down like signals

::: columns
::: {.column width="30%"}
![](./pics/low_pass_artefacts_signal.png){width="90%"}

![](./pics/low_pass_artefacts_signal_spectrum.png){width="90%"}
:::

::: {.column width="40%"}
Filter gain function $$\begin{aligned}
                G(f) = 
                \begin{cases}
                    1,  |f| \le f_{cut off} \\
                    0,  |f| \gt f_{cut off} 
                \end{cases}
\end{aligned}$$
![](./pics/low_pass_artefacts_filter_spectrum.png){width="90%"}
:::

::: {.column width="30%"}
![](./pics/low_pass_artefacts_filtered_signal.png){width="90%"}

![](./pics/low_pass_artefacts_filtered_spectrum.png){width="90%"}
:::
:::

## Low pass smoothed

Sharp features in the Fourier spectrum produce ring-down like signals

::: columns
::: {.column width="30%"}
![](./pics/low_pass_smoothed_signal.png){width="90%"}

![](./pics/low_pass_smoothed_signal_spectrum.png){width="90%"}
:::

::: {.column width="40%"}
Filter gain function $$\begin{aligned}
                G(f) = 
                G(f) = | \frac{1}{1 + i (f / f_{cut off})}  |
\end{aligned}$$
![](./pics/low_pass_smoothed_filter_spectrum.png){width="90%"}
:::

::: {.column width="30%"}
![](./pics/low_pass_smoothed_filtered_signal.png){width="90%"}

![](./pics/low_pass_smoothed_filtered_spectrum.png){width="90%"}
:::
:::

# Windowing

## Windowing artifacts

Similarly, sharp features in the time domain take the wider spectrum

::: columns
::: {.column width="30%"}
![](./pics/rectangular_window_signal.png){width="90%"}
:::

::: {.column width="40%"}
Implicitly assumed\
Rectangular window $$\begin{aligned}
                w_n = 1
\end{aligned}$$
:::

::: {.column width="30%"}
![](./pics/rectangular_window_signal_spectrum.png){width="90%"}
:::
:::

::: columns
::: {.column width="30%"}
![](./pics/hann_window_signal.png){width="90%"}
:::

::: {.column width="40%"}
$$\begin{aligned}
                y_{{windowed}_n} &=&  y_n  w_n 
\end{aligned}$$ The Hann window $$\begin{aligned}
                w_n &=&  \frac{1}{2} (1-\cos(2 \pi \frac{n-1}{N-1}))
\end{aligned}$$
:::

::: {.column width="30%"}
![](./pics/hann_window_signal_spectrum.png){width="90%"}
:::
:::

-   Note: spectral resolution $\sim 1/T_{window}$.

Search for other windowing functions: Hamming, Tukey, Cosine, Lanczos,
Triangulars, Gaussians, Bartlett-Hann, Blackmans, Kaisers.

They all decrease a signal at the beginning and at the end to zero.

## Other DFT applications

Fun one: merging of two dimensional high- and low-pass image filters

::: columns
::: {.column width="30%"}
![](./images/im1filtered.png){width="90%"}
:::

::: {.column width="30%"}
![](./images/combo_0_05.png){width="90%"}
:::

::: {.column width="30%"}
![](./images/im2filtered.png){width="90%"}
:::
:::

Depending on distance to the image, you should see either
Prof. Mikhailov or Prof. Novikova in the middle.

To see the other person in the image, either step aside or decrease zoom
till you do not see details on the right most image.

If you can take off your glasses, the illusion is stronger.
