Signals and Systems Lecture 8: Finite Impulse Response Filters

Dr. Guillaume Ducard

Fall 2018

based on materials from: Prof. Dr. Raffaello D’Andrea

Institute for Dynamic Systems and Control

ETH Zurich, Switzerland

G. Ducard 1 / 46 Outline

1 Finite Impulse Response Filters Definition General Properties 2 (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 2 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Definition Non-Causal Moving Average Filter General Properties Important Considerations Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 3 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Definition Non-Causal Moving Average Filter General Properties Important Considerations FIR filters : definition

The class of causal, LTI finite impulse response (FIR) filters can be captured by the difference equation

M−1 y[n]= bku[n − k], Xk=0 where 1 M is the number of filter coefficients (also known as filter length), 2 M − 1 is often referred to as the filter order,

3 and bk ∈ R are the filter coefficients that describe the dependence on current and previous inputs.

G. Ducard 4 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Definition Non-Causal Moving Average Filter General Properties Important Considerations Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 5 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Definition Non-Causal Moving Average Filter General Properties Important Considerations FIR: general properties

The filter length is equal to the length of the finite impulse response, given by

h = {b0,b1,...,bM−1}.

see brief calculations during class

Stability From the impulse response, we can directly conclude that FIR filters are always stable (they have M − 1 poles at z = 0).

G. Ducard 6 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Definition Non-Causal Moving Average Filter General Properties Important Considerations FIR: general properties

Frequency Response Furthermore, their frequency response is straightforward to calculate from the transfer function:

M−1 M−1 z=ejΩ H(z)= h[k]z−k −−−−→ H(Ω) = h[k]e−jΩk Xk=0 Xk=0 M−1 −jΩk = bke . Xk=0

G. Ducard 7 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Definition Non-Causal Moving Average Filter General Properties Important Considerations

Filter design

FIR filter design methods consist in finding the coefficients bk based on a desired frequency response. There are powerful FIR filter design tools available (see, for example, MATLAB fdatool that can generate almost arbitrary frequency responses.

In this lecture, you will learn : the concepts that underly FIR filters, and, using the simple example of the moving average filter, how to analyze filters using the tools, learnt in the course.

G. Ducard 8 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 9 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations MA filter: simple low-pass FIR filter

A very simple type of low-pass (LP) FIR filter: the moving average (MA) filter. The MA filter averages the current and past inputs to produce its output, and is described by the difference equation

M−1 1 y[n]= u[n − k], M Xk=0

i.e. bk = 1/M for k = 0,...,M − 1. The frequency response of the MA filter is therefore:

M−1 1 − H(Ω) = e jΩk. M Xk=0 We can easily see that H(0) = 1: a constant signal remains unchanged by the filter (property of a LP filter).

G. Ducard 10 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations MA filter: simple low-pass FIR filter

M−1 M−1 1 1 k H(Ω) = e−jΩk = e−jΩ . M M Xk=0 Xk=0  Therefore,

1 (1 − e−jΩM ) ∴ H(Ω) = , M (1 − e−jΩ)

which shows that: H(Ω) = 0 iff e−jΩM = 1 and e−jΩ 6= 1. The zeros of the MA filter therefore occur at frequencies Ω = 2πk/M where k is an integer not equal to 0 or a multiple of M. G. Ducard 11 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations MA filter: simple low-pass FIR filter

1 (1 − e−jΩM ) H(Ω) = M (1 − e−jΩ) Magnitude response : |H(Ω)|

1 M = 2

| M = 3

(Ω) M = 4 0.5 H |

0 π 2π π 2 3 Ω G. Ducard 12 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations MA filter: simple low-pass FIR filter

1 (1 − e−jΩM ) H(Ω) = M (1 − e−jΩ) The phase response ∠H(Ω) of the filter is shown below.

π M = 2 π M = 3 2 M = 4

(Ω) 0 H

∠ π − 2 −π π 2π π 2 3 G. Ducard 13 / 46 Ω Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations MA filter: simple low-pass FIR filter

Phase response analysis: For small values of Ω, the filter’s frequency response can be approximated as: 1+(1 − jΩ) + · · · + (1 − jΩ(M − 1)) H(Ω) ≈ . M The real part of H(Ω) is equal to 1, and the imaginary part is given by

1+2+ · · · +(M − 1) Ω M(M − 1) Ω(M − 1) −Ω = − = − . M M 2 2 Therefore, the phase can be approximated by

Ω(M − 1) Ω(M − 1) ∠H(Ω) ≈ arctan − ≈− ,  2  2 using the small angle assumption. This approximation is exact until the first zero of H(Ω), as you will show in the problem set.

G. Ducard 14 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations Magnitude response

The magnitude response of the filter can be derived as follows:

− 1 (1 − e jΩM ) 1 − cos ΩM + j sin ΩM H(Ω) = = M (1 − e−jΩ) M(1 − cos Ω + j sin Ω) (1 − cos ΩM)2 + sin2 ΩM ∴ |H(Ω)|2 = M 2((1 − cos Ω)2 + sin2 Ω) 1 − 2 cos ΩM + cos2 ΩM + sin2 ΩM = M 2(1 − 2 cos Ω + cos2 Ω + sin2 Ω) (1 − cos ΩM) = M 2(1 − cos Ω) sin2( ΩM ) = 2 since 1 − cos(2p) = 2 sin2(p). 2 2 Ω M sin ( 2 )

G. Ducard 15 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations Magnitude response sin ΩM sin ΩM sin ΩM 1 2 Ω/2 2 2 1 |H(Ω)| = Ω = Ω = · Ω M sin  MΩ/2 sin  MΩ/2  sin 2 2 2   Ω/2  ΩM sinc 2 = Ω sinc  2 ΩM ≈ sinc 2 for small Ω.  Therefore, the magnitude response of the MA filter is approximated by the absolute value of the sinc function for small Ω. This function, below, has peaks (lobes) and is not a great LP filter.

| 1 ) w ( 0.5 sinc | 0 0 5 w G. Ducard 16 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 17 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations Fast MA filter implementation

Consider a MA filter with M coefficients and output given by M−1 1 y[n]= u[n − k]. M Xk=0 We will now derive an alternative form of the MA filter, which is more computationally efficient:

M−1 M−1 M(y[n] − y[n − 1]) = u[n − k] − u[n − k − 1] X X k=0 k=0 = u[n]+ u[n − 1] + u[n − 2] + ··· + u[n − M + 1] − u[n − 1] + u[n − 2] + ··· + u[n − (M − 2) − 1] + u[n − (M − 1) − 1] = u[n]+ u[n − 1] + u[n − 2] + ··· + u[n − M + 1] − u[n − 1] + u[n − 2] + ··· + u[n − M +1]+ u[n − M] = u[n] − u[n − M]

u[n] − u[n − M] ∴ y[n]= y[n − 1] + . M As seen in the above difference equation, by referencing the prior output, the

summation can be simplified to two terms, irrespective of the filter’sG. Ducard order.18 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations

However, one must be careful : this approach has a flaw that we will now investigate.

By adding the term d[n] to the difference equation, we capture the effect of numerical errors (for example, caused by floating-point imprecision):

u[n] − u[n − M] y[n]= y[n − 1] + + d[n]. M

Taking the z-transform we obtain:

−M − U(z) − z U(z) Y (z)= z 1Y (z)+ + D(z) M − 1 1 − z M 1 = U(z)+ D(z) M  1 − z−1  1 − z−1

1 −1 −M+1 1 = 1+ z + . . . + z U(z)+ −1 D(z), M   1 − z

G. Ducard 19 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations

1 −1 −M+1 1 Y (z)= 1+ z + . . . + z U(z)+ −1 D(z), M   1 − z From the above, we note that the transfer function from D(z) to Y (z) is 1 1−z−1 , which is not stable, since it has a pole at z = 1 (integral effect). We note also that 1 − − =1+ z 1 + z 2 + ..., 1 − z−1

We calculate the inverse z-transform of Y (z) to obtain:

M−1 n 1 y[n]= u[n − k] + d[n − k]. M Xk=0 Xk=0

Conclusion : the effect of numerical errors on the output is cumulative.

G. Ducard 20 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 21 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations Weighted Moving Average (WMA) Filter

WMA filter can be described by the difference equation

M−1 1 y[n]= w u[n − k], S k Xk=0

wk is a decreasing function of k and denotes the weight given to the input u[n − k], S is the normalization constant chosen such that the sum of all filter coefficients equals 1.

A common choice for a WMA filter, is wk =(M − k) and S =1+2+ . . . + M = M + . . . + 1 2S = M(M + 1) M(M + 1) ∴ S = . 2 G. Ducard 22 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations

WMA filter can be described by the difference equation M−1 M−1 1 w y[n]= w u[n − k]= k u[n − k], S k S Xk=0 Xk=0

The difference between the WMA and MA filters can be clearly seen in their impulse responses:

2 MA M+1 WMA 1 M

n M − 1

G. Ducard 23 / 46 Finite Impulse Response Filters MA filter as a simple low-pass filter Moving Average (MA) Filter Fast MA filter implementation Non-Causal Moving Average Filter Weighted Moving Average Filter Important Considerations

The WMA filter places less emphasis on older inputs. This results in a less aggressive filter, with a better (smaller) phase response:

1

| MA (M = 5)

(Ω) WMA (M = 5) 0.5 H |

0 2π 4π π 5 5 π π 2 (Ω) 0 H π ∠ − 2 −π 0 2π 4π π 5 5 Ω

G. Ducard 24 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 25 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Non-Causal Moving Average Filter

Filter equation :

M−1 1 2 y[n]= u[n − k] M X− M−1 k= 2

where the number of coefficients M is odd. Let us use the change M−1 of variable l = k + 2 , the filter equation then writes

M−1 1 M − 1 y[n]= u[n − (l − )] M 2 Xl=0

G. Ducard 26 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Non-Causal Moving Average Filter

The impulse response of this filter is : M−1 1 M − 1 h[n]= δ n − (l − ) M  2  Xl=0

M − 1 l = 0 h[n] = 1/M for n = − 2 M − 3 l = 1 h[n] = 1/M for n = − 2 M − 5 l = 2 h[n] = 1/M for n = − 2 . . M − 3 l = M − 2 h[n] = 1/M for n = 2 M − 1 l = M − 1 h[n] = 1/M for n = − 2 G. Ducard 27 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Non-Causal Moving Average Filter

The impulse response of this filter is : 1 1 1 h = {..., 0, ,..., ,..., , 0,...}, M M M ↑

This filter includes past, current, and future inputs, with equal weighting.

h[n] 1 M n M−1 M−1 − 2 2

G. Ducard 28 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Non-Causal Moving Average Filter

The filter’s frequency response is given by

M−1 − 1 −jΩ(k− M 1 ) H(Ω) = e 2 M Xk=0 − jΩ( M 1 ) = e 2 HMA(Ω),

where HMA(Ω) is the frequency response of the causal MA filter.

Conclusion 1 This relationship shows that the frequency response of the non-causal MA filter is that of a causal MA filter with an M−1 added phase of Ω( 2 ). 2 Their magnitude response is the same.

G. Ducard 29 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Example: non-causal MA filter with M =3

u[n−1]+u[n]+u[n+1] The filter difference equation is y[n]= 3 . The filter’s frequency response is:

| 1

(Ω) 0.5 H |

2π π 3 π π 2 (Ω) 0 H − π ∠ 2 −π 0 2π π 3 Ω G. Ducard 30 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Example: non-causal MA filter with M =3

Obervations : Note that for Ω < 2π/3 the phase is zero: a very desirable property. However, the phase of the first lobe is 180◦. This results in signal inversion.

Proof Let {u[n]} = {(−1)n} and note that Ω= π. Applying this input to the filter gives the output:

(−1)n −u[n] y[n]= − = for all times n. 3 3 This is not desirable: Not only does the non-causal MA filter not remove the high-frequency signal, but it inverts it!

G. Ducard 31 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 32 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Non-Causal Weighted Moving Average Filter

Consider now the non-causal weighted moving average filter, with impulse response given by

∞ 1 h[n]= h˜[n] for all times n, where S = h˜[n], S k=X−∞

and where {h˜[n]} is given by

M+1 ˜ 2 h[n]

1 n M−1 M−1 − 2 2

G. Ducard 33 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations Non-Causal Weighted Moving Average Filter

Let us now have a look at the frequency response of a non-causal WMA filter for M = 5: 1 |

(Ω) 0.5 H | 0 2π 4π π 5 5 π π 2 (Ω) 0 H π ∠ − 2 −π 0 2π 4π π 5 5 Ω

G. Ducard 34 / 46 Finite Impulse Response Filters Moving Average (MA) Filter Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter Important Considerations

As expected, the non-causal WMA filter is a LP filter. Furthermore, the filter has zero-phase. This is a very nice property to have, and deserves further investigation. Consider a causal MA filter with M = 3 and transfer function 1+ z−1 + z−2 H(z)= 3 − and the corresponding anti-causal filter with transfer function H(z 1). We then have that

−1 −2 2 − 1+ z + z 1+ z + z H(z)H(z 1)= 3 3 1 − − − = (1 + z + z2 + z 1 +1+ z + z 2 + z 1 + 1) 9 1 − − = (3+2(z + z 1)+(z2 + z 2)), 9 which is a non-causal WMA filter with M = 5 coefficients. As seen in Lecture 7, the cascade of these systems will have zero phase. In general, if H(z) is the transfer function of a MA filter with M coefficients, − then H(z)H(z 1) is a non-causal WMA filter with 2M − 1 coefficients. G. Ducard 35 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 36 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Phase is Important

Phase is a measure of delay caused by the system. The negative ratio −∠H(Ω)/Ω is called the phase delay of a filter, which states how many samples a sinusoid at frequency Ω is delayed by the filter.

G. Ducard 37 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Phase is Important

Phase is a measure of delay caused by the system. The negative ratio −∠H(Ω)/Ω is called the phase delay of a filter, which states how many samples a sinusoid at frequency Ω is delayed by the filter. If ∠H(Ω) is linear in Ω, the filter is said to have , and the phase delay of the filter is constant: sinusoids at all frequencies Ω ∈ [−π,π] are shifted by the same number of samples.

G. Ducard 37 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Phase is Important

Phase is a measure of delay caused by the system. The negative ratio −∠H(Ω)/Ω is called the phase delay of a filter, which states how many samples a sinusoid at frequency Ω is delayed by the filter. If ∠H(Ω) is linear in Ω, the filter is said to have linear phase, and the phase delay of the filter is constant: sinusoids at all frequencies Ω ∈ [−π,π] are shifted by the same number of samples. A linear phase is good for some applications (e.g. audio) as it ensures no signal deformations; however, it is not always important for other applications, such as control.

G. Ducard 37 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 38 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Differentiation using FIR Filters

FIR filters can also be used to approximate the derivative of their input signal. To demonstrate this, let y(t) =u ˙(t). This can be approximated, for example,

u(t) − u(t − τ) causally by y(t) ≈ ; τ u(t + τ) − u(t) anti-causally by y(t) ≈ ; τ u(t + τ) − u(t − τ) non-causally by y(t) ≈ . 2τ

G. Ducard 39 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Differentiation using FIR Filters

If τ is the sampling period Ts, we have the following discrete-time approximations of the derivative: 1 Causal yC[n]= (u[n] − u[n − 1]); Ts 1 Anti-causal yA[n]= (u[n + 1] − u[n]); Ts 1 Non-causal yN[n]= (u[n + 1] − u[n − 1]). 2Ts

G. Ducard 40 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Differentiation using FIR Filters

Note that each LCCDE describes an LTI FIR system. We now calculate the frequency responses of the above systems.

Causal Anti-causal Non-causal −1 1 − z HC(z) = T s 1 −1 − Ω − 1 − e j HN(z) = (z z ) 2Ts HC(Ω) = HA(z) = zHC(z) T 1 s Ω 2 jΩ − −jΩ ej / HN(Ω) = (e e ) −jΩ/2 jΩ/2 − −jΩ/2 2T e e e  HA(Ω) = 2j sin Ω/2. s = Ts j sin Ω Ts = . − Ω 2 Ts e j / = 2j sin Ω/2. Ts

G. Ducard 41 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Differentiation using FIR Filters

π/Ts Continuous-time

| Anti-causal Causal (Ω) Non-causal H | 0 π π 2 π

(Ω) π 2 H ∠ 0 π π 0 2 Ω

G. Ducard 42 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 43 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Frequency-domain observations

jωt jωt 1 If u(t)= e and u(t) ←→ U(ω), then u˙(t)= jωe and u˙(t) ←→ jωU(ω): differentiation in the time domain is equivalent to multiplication by jω in the frequency domain.

G. Ducard 44 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Frequency-domain observations

jωt jωt 1 If u(t)= e and u(t) ←→ U(ω), then u˙(t)= jωe and u˙(t) ←→ jωU(ω): differentiation in the time domain is equivalent to multiplication by jω in the frequency domain. 2 Considering now the frequency response of the FIR systems, shown above, we note that all systems have the desired behavior near Ω = 0: a frequency response that resembles jω (with ω =Ω/Ts).

G. Ducard 44 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Frequency-domain observations

jωt jωt 1 If u(t)= e and u(t) ←→ U(ω), then u˙(t)= jωe and u˙(t) ←→ jωU(ω): differentiation in the time domain is equivalent to multiplication by jω in the frequency domain. 2 Considering now the frequency response of the FIR systems, shown above, we note that all systems have the desired behavior near Ω = 0: a frequency response that resembles jω (with ω =Ω/Ts). 3 Furthermore, both causal and anti-causal approximations have a frequency response with maximum at Ω= π, as desired.

G. Ducard 44 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Frequency-domain observations

jωt jωt 1 If u(t)= e and u(t) ←→ U(ω), then u˙(t)= jωe and u˙(t) ←→ jωU(ω): differentiation in the time domain is equivalent to multiplication by jω in the frequency domain. 2 Considering now the frequency response of the FIR systems, shown above, we note that all systems have the desired behavior near Ω = 0: a frequency response that resembles jω (with ω =Ω/Ts). 3 Furthermore, both causal and anti-causal approximations have a frequency response with maximum at Ω= π, as desired. 4 However, the non-causal, symmetric approximation has a frequency response with maximum at Ω= π/2: it is not a good approximation at high frequencies; for example, at Ω= π, HN(Ω) = 0.

G. Ducard 44 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Outline

1 Finite Impulse Response Filters Definition General Properties 2 Moving Average (MA) Filter MA filter as a simple low-pass filter Fast MA filter implementation Weighted Moving Average Filter 3 Non-Causal Moving Average Filter Non-Causal Moving Average Filter Non-Causal Weighted Moving Average Filter 4 Important Considerations Phase is Important Differentiation using FIR Filters Frequency-domain observations Higher derivatives G. Ducard 45 / 46 Finite Impulse Response Filters Phase is Important Moving Average (MA) Filter Differentiation using FIR Filters Non-Causal Moving Average Filter Frequency-domain observations Important Considerations Higher derivatives Higher derivatives

Generalization: to compute higher derivatives in a discrete manner, successive applications of the first derivative can be employed . For example : the derivative y(t) =u ¨(t) can be causally approximated by u˙(t) − u˙(t − τ) y(t) ≈ τ u(t) − u(t − τ) − u(t − τ)+ u(t − 2τ) ≈ . τ 2

Conclusion 1 y[n]= 2 (u[n] − 2u[n − 1] + u[n − 2]), if τ = Ts. Ts G. Ducard 46 / 46