Digital filters based on Fourier transform

Eugeniy E. Mikhailov and Greg Bentsen

Today

DFT recap

Often in literature instead of YnY_n is used instead of cnc_n: cn=Yn=∑k=0N−1ykexp(−i2πknN)Fourier transformyk=1N∑n=0N−1cnexp(i2πknN)inverse Fourier transformn,k=0,1,2,⋯,N−1\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}

The operator notation: Y=FyForward Fourier transformy=F−1Yinverse Fourier transform\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={y0,y1,y2,...,yN−1}y=\{y_0, y_1, y_2, ..., y_{N-1}\} and Y={Y0,Y1,Y2,...,YN−1}={c0,c1,c2,...,cN−1}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 yk=y(tk)=1N∑n=0N−1cnexp(i2πtkfn)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πfn=ωn2 \pi f_n = \omega_n

time domain
time domain

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

Sample rate fs=1/Δt=N/Tf_s = 1/\Delta t = N/T.

Spectrum (amplitude vs frequency)
Spectrum (amplitude vs frequency)

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

Frequency of nn-th component fn=nΔff_n = n \Delta f.

Nyquist frequency

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

Naively, we can say (N−1)×f1≈fs(N-1) \times f_1 \approx f_s, since in the DFT spectrum all points are separated by the fundamental frequency f1=1/T=fs/Nf_1=1/T=f_s/N

However, recall that Yn=cn=∑k=0N−1ykexp(−i2πknN)Y_n = c_n = \sum_{k=0}^{N-1} y_k \exp(-i\frac{ 2 \pi k n}{N})

Thus, YN−n=Y−nY_{N-n} = Y_{-n}, i.e., the higher half of the vector YY contains negative frequency. So at most, we can hope to obtain a spectrum with the highest real frequency smaller than

Nyquist frequency

FNq=f1N2=fs2\begin{aligned} F_{Nq}= f_1 \frac{N}{2} = \frac{f_s}{2} \end{aligned}

Nyquist criteria

fs>2fsignal or fNq>fs\begin{aligned} f_s > 2 f_{signal} & \text{ or } & f_{Nq} > f_s \end{aligned}

You must sample your signal more than twice faster than the highest frequency component of it. I.e., the Nyquist frequency of your sample should be >> than the highest signal frequency.

Aliasing: wrong/slow sampling frequency

Sampling with
fs=2fsignalf_s = 2f_{signal}
i.e.
fNq=fsignalf_{Nq} = f_{signal}
Sampled signal appeared to be DC

example of sampling at Nyquist frequency
example of sampling at Nyquist frequency

Aliasing: too slow sampling frequency - reflection

Under sampling fs=1.1fsignalf_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.

fapparentsignal=(fsignal−2fNq)≈fsignal−fsf_{apparent~signal} = (f_{signal} - 2 f_{Nq}) \approx f_{signal} - f_s

example undersampling
example undersampling

Aliasing: too slow sampling frequency - ghosts

Under sampling fs=1.93fsignalf_s = 1.93 f_{signal}

The sampled signal looks very different.

example of under sampling
example of under sampling

DFT filters

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

The recipe is the following

yfiltered(t)=F−1[[Fy(t)]G(f)]=F−1[Y(f)G(f)]\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

Brick wall low-pass filter

Filter gain function G(f)={1,|f|≤fcutoff0,|f|>fcutoff\begin{aligned} G(f) = \begin{cases} 1, |f| \le f_{cut off}\\ 0, |f| \gt f_{cut off} \end{cases} \end{aligned}

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

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)

Brick wall high-pass filter

Filter gain function G(f)={1,|f|≥fcutoff0,|f|<fcutoff\begin{aligned} G(f) = \begin{cases} 1, |f| \ge f_{cut off}\\ 0, |f| \lt f_{cut off} \end{cases} \end{aligned}

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

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)

Brick wall band-pass filter

Filter gain function G(f)={1,||f|−fc|≤fbw20,||f|−fc|>fbw2\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}

Brick wall band-pass filter (continued)

Brick wall band-stop filter

Filter gain function G(f)={0,||f|−fc|≤fbw21,||f|−fc|>fbw2\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}

Brick wall band-stop filter (continued)

Brick wall filters artifacts

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

Filter gain function G(f)={1,|f|≤fcutoff0,|f|>fcutoff\begin{aligned} G(f) = \begin{cases} 1, |f| \le f_{cut off} \\ 0, |f| \gt f_{cut off} \end{cases} \end{aligned}

Low pass smoothed

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

Filter gain function G(f)=G(f)=|11+i(f/fcutoff)|\begin{aligned} G(f) = G(f) = | \frac{1}{1 + i (f / f_{cut off})} | \end{aligned}

Windowing

Windowing artifacts

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

Implicitly assumed
Rectangular window wn=1\begin{aligned} w_n = 1 \end{aligned}

ywindowedn=ynwn\begin{aligned} y_{{windowed}_n} &=& y_n w_n \end{aligned} The Hann window wn=12(1−cos(2πn−1N−1))\begin{aligned} w_n &=& \frac{1}{2} (1-\cos(2 \pi \frac{n-1}{N-1})) \end{aligned}

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

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.