get-the-solution

Signal filtering

By
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

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.

Fig. 5. ideal low pass filter

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):

Fig. 6. low pass filter with “Pass band”, “transition band”, “stop band”
  • wc cut off frequency
  • wp end of the pass-band
  • ws start of stop-band
  • δ1 peak pass-band ripple −20db ⋅ log10(1 − δ1)[db]
  • δ2 peak stop-band ripple −20db ⋅ log10(δ2)[db]

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 (wp and ws) 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 hideal which shifts the signal by n ⋅ ddelay.

hideal[n] = δ[n − n ⋅ ddelay]

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}}$

Fig. 7. linear phase or phase equal zero”

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

  1. Use a finite impulse response filter (FIR-Filter) because they have a linear phase response (for real time application)
  2. 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:

  1. analytical specify the desired frequency response

    1. 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 wc and zero otherwise

      $H_{lp}(\omega)=\begin{cases} 1; |\omega|\leq\omega_c\\ 0;otherwise \end{cases}$

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

    hlp − infinity[n] = sinc[wc ⋅ n]

  3. The above filter from step 2 is infinity long and acausal (none zero values for n)

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

    hlp − finite[n] = hlp infinity[n] ⋅ ωfiniteWindow[n]

    1. Typical windows (M=length of filter):
      1. Rectangular: $\omega[n]=\begin{cases} 1; |n|\leq M\\ 0;otherwise \end{cases}$
      2. bartlett: $\omega[n]=\begin{cases} 1-|\frac{2n}{M}-1|; |n|\leq M\\ 0;otherwise \end{cases}$
      3. hann: $\omega[n]=\begin{cases} 0,5-0,5\cdot cos(\frac{2\cdot\pi}{M}); |n|\leq M\\ 0;otherwise \end{cases}$
      4. hamming: $\omega[n]=\begin{cases} 0,54-0,46\cdot cos(\frac{2\cdot\pi}{M}); |n|\leq M\\ 0;otherwise \end{cases}$

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 hlp − 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.

  1. 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

  2. Specify the desired filter frequency response H(ωi) with $\omega_i=\frac{\pi}{k}\cdot i$ with i=0,…,k-1; k >> N

  3. How find the optimal filter response which optimal approximates frequency response

  4. 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 ⋅ sin(ω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 = (AT ⋅ A)−1 ⋅ AT ⋅ d…Moore-Penrose-Inverse

  5. Mirror 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)

    Fig. 7. Butterworth”
  • Chebyshev type I (Allow ripples in passband)

    Fig. 7. Chebyshev type 1”
  • Chebyshev type II (allow ripples in stopband)

    Fig. 7. Chebyshev type 2”
  • Elliptic (allow ripples in pass and stopband)

    Fig. 7. Elliptic”

Build lowpass, highpass, bypass filter

Hlp(ω)   (lowpassfilter)

Hhp(ω) = 1 − Hlp(ω)   (highpassfilter)

Hbandpass(ω) = Hlp(ω) ⋅ Hhp(ω)   (bandpassfilter)

Hbandstopp(ω) = 1 − Hbandpass   (bandstopppassfilter)

Sample:

Assume we have the following low-pass filters: Hlp20 with 20 Hz and a Hlp40 with 40 Hz in the frequency domain. With the above formulars we can construct a band-pass filter:

Hbandpass(ω) = Hlp40(ω) * [1 − Hlp20(ω)]

Continue to hilbert transformation

Sources: