## Signal filtering

By Martin on Regards: education; Article; python; sip;# Filters

This text is from the category SIP (Signal and image proccessing). Previous text signal sampling

## Overview

- Designing Filters
- Low Pass filter
- FIR filter design by windowing
- FIR filter design by least square optimization
- Infinite impulse response (IIR) filters
- Build lowpass, highpass, bypass filter

## Designing Filters

Application:

When recording for example brain signals there can be some “frequency noise”. This “noise” we want to filter out. An image (which can also be transformed with the dft) can have frequencies. When taking pictures the sensors of the camera which are computing the pixel also can have some kind of “white noise”. For example an image with a blue sky which contains the color blue which is changing to an orange horizon (to capture the sunset). This kind of image would be represented as a slow shift in the amplitude of the brightness of the picture. This means the image has a low frequency and the noise of the sensor which got a high frequency could be filtered out by a low pass filter.

Filtering is also relevant for the classification problems (machine learning).

Radio signals contains all the information from all radio stations. To receive only the signal of one particular radio station we would need a band pass filter to filter out all other radio stations signals except the one we are interested.

There exists lowpass-, highpass- and bandpassfilter.

The low pass filter is a very important filter typ, as any other filter typ can be build up of a sum of low pass filters.

- lowpass-filter lets through all frequencies up to the cut of frequency.
- high pass filter lets through all frequencies above the cut of frequency.
- The band pass filter allows frequencies through a specific frequency range.

For filtering we distinguish between offline (signal is already recorded) and online (signal is captured in time) signals.

### Low Pass filter

Ideal filters are imaginary. We can’t build them. Its worth studying them to get a picture why they cannot be built and how we can try to approximate ideal filters.

In the following example the transform function of the low pass
filter is called |*H*(*ω*)| w_c is the cut of
point of the frequency. Ideal low pass filter has a gain of 1 for low
frequencies and a gain of 0 for all other frequencies with are larger
than our cut of point w_c.

Ideal filter have zero or linear phase response, as the filter ideally should not shift the original signal.

The challenging about a ideal filter response is that its an acausal system (meaining it should response to the signal before it has seen in time (would work only for offline data) ) and got a infinite impulse response.

In practice we want to work with final length filter and for real-time applications causal filters are required.

Cutting of the acausal part of the filter and cut of the filter of at the length which is suitable but will lead to a filter distortion and will result in a none-ideal filter. As seen below (low pass filter):

*w*_{c}cut off frequency*w*_{p}end of the pass-band*w*_{s}start of stop-band*δ*_{1}peak pass-band ripple −20*d**b*⋅*l**o**g*_{10}(1 −*δ*_{1})[*d**b*]*δ*_{2}peak stop-band ripple −20*d**b*⋅*l**o**g*_{10}(*δ*_{2})[*d**b*]

The low pass filter from figure 6. allows a derivation from 1 + *δ*_{1} and 1 − *δ*_{1} for the passband
and for the stoppband from 0 to *δ*_{2}. The transition phase
from the passband to the stoppband, which does not happen instantly, is
called transition band. For example the width of the transition band
(*w*_{p} and
*w*_{s})
influences the steepness of the curve.

As an ideal filter should not delay the signal the ideal phase is 0. As this is reality not possible we want at least a linear phase response. With a linear phase response the signal would be always delayed with the same time number steps.

Lets assume we have *h*_{ideal}
which shifts the signal by *n* ⋅ *d*_{delay}.

*h*_{ideal}[*n*] = *δ*[*n* − *n* ⋅ *d*_{delay}]

Make use dft:

$h_{ideal}[n]=\delta[n-n\cdot d_{delay}]\Leftrightarrow \sum_{n=\infty}^{\infty} \delta[n-n\cdot d_{delay}]\cdot e^{-j\cdot \omega n\cdot d_{delay}}$

If we don’t have a linear phase we can try to achieve them with:

- Use a finite impulse response filter (FIR-Filter) because they have a linear phase response (for real time application)
- If you need to use an infinite impulse filter (IIR-Filter) use forward-backward filtering (Can only be used with offline data (signal already recorded)). Can be designed better (compared to FIR-Filter) and have therefore better properties. How does the forward-backward filter work? First filter signal, than flip it in time, so that it runs backward in time and put it again through filter. In the first time the signal got delayed by the phase response and putting the flipped signal again to the filter everything gets advanced. In sum this means that the phase response is canceling itself out, which results in a phase zero response.

### Final impulse response (FIR) - How to design FIR-filters

#### FIR filter design by windowing

heuristic approach. compute the impulse response of ideal filter (for example lowpass filter). use certain windows to make this ideal filter of final length. advantage of this filter design is, thats easy and straight forward. there is no principal way approximate the trade off of the filter. you need to play around to see whats the best trade off for you.

steps:

analytical specify the desired frequency response

for example we want to construct an ideal low pass filter, the response will be one if the frequency is lower than the cut of frequency

*w*_{c}and zero otherwise$H_{lp}(\omega)=\begin{cases} 1; |\omega|\leq\omega_c\\ 0;otherwise \end{cases}$

apply the inverse DTFT to obtain the impulse response of ideal filter (which is the sinc function).

*h*_{lp − infinity}[*n*] =*s**i**n**c*[*w*_{c}⋅*n*]The above filter from step 2 is infinity long and acausal (none zero values for n)

Use a window function to obtain a finite-length filter. for example

*h*_{lp − finite}[*n*] =*h*_{lp infinity}[*n*] ⋅*ω*_{finiteWindow}[*n*]- Typical windows (M=length of filter):
- Rectangular: $\omega[n]=\begin{cases} 1; |n|\leq M\\ 0;otherwise \end{cases}$
- bartlett: $\omega[n]=\begin{cases} 1-|\frac{2n}{M}-1|; |n|\leq M\\ 0;otherwise \end{cases}$
- hann: $\omega[n]=\begin{cases} 0,5-0,5\cdot cos(\frac{2\cdot\pi}{M}); |n|\leq M\\ 0;otherwise \end{cases}$
- hamming: $\omega[n]=\begin{cases} 0,54-0,46\cdot cos(\frac{2\cdot\pi}{M}); |n|\leq M\\ 0;otherwise \end{cases}$

- Typical windows (M=length of filter):

This filter approach can only be used if the complete signal is available (offline data) and comes with the following:

- is acausal
- “looks into the future”
- computationally intensive
- is dependent of the fft properties
- periodic signal
- distortion
- bad on non-stationary signals

For an online setting we need to delay impulse response *h*_{lp − finite}[*n*]
to obtain a causal filter. The numbers of sample which we need to Delay
depends on the length of the filter. For example the length of the
filter is M we would need to delay by M/2 and for uneven M it would be
M/2+1.

#### FIR filter design by least square optimization

come up with filter with a given length of filter optimal approximates the response I would like to have. For a filter with given length you try to approximate a desired frequency response. Implemented in matlab by the firls function.

Choose the filter length. the length of the filter response is N+1 if N is even. A larger N will result in a larger delay

Specify the desired filter frequency response

*H*(*ω*_{i}) with $\omega_i=\frac{\pi}{k}\cdot i$ with i=0,…,k-1; k >> NHow find the optimal filter response which optimal approximates frequency response

Make use of DTFT

$H(\omega_i)=\sum_{\frac{-N}{2}}^{\frac{N}{2}}h[n]\cdot e^{-j\omega_i\cdot n}$

To obtain a zero-phase filter with no delay, we want the imaginary parts of our filter response

*H*(*ω*_{i}) to be zero (set*j*⋅*s**i**n*(*ω*_{i}⋅*n*)) = 0). We have to enforce that h[n]=h[-n] is symmetric. Because of sin(x)=sin(-x) the signals phase is canceling its imaginary part out to phase 0.$H(\omega_i)=\sum_{n=\frac{-N}{2}}^{\frac{N}{2}}h[n]\cdot (cos(\omega_i\cdot n)-j\cdot sin(\omega_i\cdot n))=\sum_{n=\frac{-N}{2}}^{\frac{N}{2}}h[n]\cdot (cos(\omega_i\cdot n))$ $H(\omega_i)=h_0+\sum_{n=1}^{\frac{N}{2}}h[n]\cdot (2\cdot cos(\omega_i\cdot n))$

Turn expression theorem into optimization problem. Vectorized expression:

$\underbrace{\begin{bmatrix}H(\omega_0)\\H(\omega_1)\\ \vdots \\H(\omega_k) \end{bmatrix}}_{\substack{=:d}}=\underbrace{\begin{bmatrix} 1 & 2\cdot cos(\omega_0\cdot1) & \dots & 2\cdot cos(\omega_0\cdot\frac{N}{2})\\1 & 2\cdot cos(\omega_1\cdot1) & \dots & 2\cdot cos(\omega_1\cdot\frac{N}{2})\\ & \vdots & & \vdots \\1 & 2\cdot cos(\omega_k\cdot1) & \dots & 2\cdot cos(\omega_k\cdot\frac{N}{2}) \end{bmatrix}}_{\substack{=:A}} \cdot \underbrace{\begin{bmatrix}h[0]\\ \\ \vdots \\h[\frac{N}{2}] \end{bmatrix}}_{\substack{=:h}}$

Matrix which should be minimized

*ĥ*= arg min of h (*d*−*A*⋅*h*)^{2}Derivate to find the minimum

$\frac{\partial }{\partial h} (d-A\cdot h)^2=0$ solve for h

*h*= (*A*^{T}⋅*A*)^{−1}⋅*A*^{T}⋅*d*…Moore-Penrose-InverseMirror h[n] to h[-n] to obtain the desired impulse response

Note: FIR filter design by least square optimization is an acausal filter. If we want to turn it into a causal filter we need to delay it by N/2.

#### Infinite impulse response (IIR) filters

Generic form of IIR response filters: *a*[0]*y*[0] = *b*[0]*x*[*n*] + *b*[1]*x*[*n* − 1] + ... + *b*[*p*]*x*[*n* − *p*] − *a*[1]*y*[*n* − 1]... − *a*[*q*]*y*[*n* − *q*]

The output of the system y at time 0 is convolved with input x and the filter impulse response b. In addition the past output (-a[1]y[n-1]…-a[q]y[n-q]) is feeded back into the system.

- b coefficients which are associated with p; final response
- a coefficient which are associated with q

The maximum delay, in samples, used in creating each output sample is called the order of the filter Filter order by JOS.

$y_n=\sum_{i=0}^{p}a_i\cdot x_{n-i}-\sum_{l=1}^{q}b_l\cdot y_{n-l}$

- the recursion (past signal values) may cause an infinite long impulse response
- order of filter: maximum of p and q
- Advantage: more efficient, less computationally intensive vs lowpass with FIR

See for additional information on the following external resource dspguide.com Chapter 19: Recursive Filters

Notes:

- IIR filters maybe unstable (check if filter is stable)
- IIR filters do not have in general a linear phase
- for offline: use forward-backward if possible

- IIR filters typically have steeper transitions band than FIR filters

There are four prototypical IIR filters. They differ what they allow in the stopband and passband.

Butterworth (Smooth passband and stopband behaviour)

Chebyshev type I (Allow ripples in passband)

Chebyshev type II (allow ripples in stopband)

Elliptic (allow ripples in pass and stopband)

### Build lowpass, highpass, bypass filter

*H*_{lp}(*ω*) (*l**o**w**p**a**s**s**f**i**l**t**e**r*)

*H*_{hp}(*ω*) = 1 − *H*_{lp}(*ω*) (*h**i**g**h**p**a**s**s**f**i**l**t**e**r*)

*H*_{bandpass}(*ω*) = *H*_{lp}(*ω*) ⋅ *H*_{hp}(*ω*) (*b**a**n**d**p**a**s**s**f**i**l**t**e**r*)

*H*_{bandstopp}(*ω*) = 1 − *H*_{bandpass} (*b**a**n**d**s**t**o**p**p**p**a**s**s**f**i**l**t**e**r*)

Sample:

Assume we have the following low-pass filters: *H*_{lp20}
with 20 Hz and a *H*_{lp40}
with 40 Hz in the frequency domain. With the above formulars we can
construct a band-pass filter:

*H*_{bandpass}(*ω*) = *H*_{lp40}(*ω*) * [1 − *H*_{lp20}(*ω*)]

Continue to hilbert transformation

Sources:

- transcript of course signal image processing
- https://ccrma.stanford.edu/~jos/sasp/Least_Squares_Linear_Phase_FIR_Filter.html
- https://en.wikipedia.org/wiki/Window_function
- http://slpl.cse.nsysu.edu.tw/cpchen/courses/dsp/basics-2.pdf
- http://pipad.org/wiki/index.php/DSP
- https://ccrma.stanford.edu/~jos/filters/Filter_Order.html