0% found this document useful (0 votes)
5 views

Module 3 - Digital Filters

The document provides an overview of digital filters, focusing on FIR (Finite Impulse Response) and IIR (Infinite Impulse Response) filters, including their definitions, characteristics, and design methods. It discusses the application of these filters in signal processing, with practical examples and Python code for implementation. Additionally, it compares FIR and IIR filters in terms of phase response and computational efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module 3 - Digital Filters

The document provides an overview of digital filters, focusing on FIR (Finite Impulse Response) and IIR (Infinite Impulse Response) filters, including their definitions, characteristics, and design methods. It discusses the application of these filters in signal processing, with practical examples and Python code for implementation. Additionally, it compares FIR and IIR filters in terms of phase response and computational efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Module 3: Digital filters

Hernán Darı́o Vargas Cardona, PhD

Digital Signal Processing (DSP)


Biomedical Engineering
Electronic Engineering
Pontificia Universidad Javeriana Cali
Content

Introduction

FIR filters

IIR filters

FIR vs IIR
Content

Introduction

FIR filters

IIR filters

FIR vs IIR
Digital filters I

Digital filters can be described by convolving the input signal with


the impulse response of the filter. They are divided into:
I FIR: finite impulse response.
I IIR: infinite impulse response.
Digital filters II
Additionally, the types of digital filters include: low-pass filter
(LPF), high-pass filter (HPF), band-pass filter (BPF), and
band-stop filter (BSF):

BSF

BPF
Content

Introduction

FIR filters

IIR filters

FIR vs IIR
FIR filters I

In the case of FIR filters, a sample of the output can be defined as


a linear combination of current and past input samples. This
relationship is expressed as:

y [n] = a0 x[n] + a1 x[n − 1] + a2 x[n − 2] + · · · + aN x[n − N]

This equation expresses that the current sample of the output y [n]
is equal to the sum of the current and past input samples, each
multiplied by a corresponding coefficient ai . The coefficients ai are
the filter coefficients and constitute the impulse response of the
system. By modifying these coefficients, the type of filter can be
altered.
FIR filters II
Let’s remember the example of module 1:

x[n] = {4, 3, −1, 2, 1} , h[n] = {a0 , a1 } = {0.5, 0.5}


n
X
y [n] = h[k]x[n − k] = a0 x[n] + a1 x[n − 1]
k=0

Solution:
y [0] = a0 x[0] = (4)(0.5) = 2
y [1] = a0 x[1] + a1 x[0] = (0.5)(3) + (0.5)(4) = 3.5
y [2] = a0 x[2] + a1 x[1] = (0.5)(−1) + (0.5)(3) = 1
y [3] = a0 x[3] + a1 x[2] = (0.5)(2) + (0.5)(−1) = 0.5
y [4] = a0 x[4] + a1 x[3] = (0.5)(1) + (0.5)(2) = 1.5
y [5] = a0 x[5] + a1 x[4] = (0.5)(0) + (0.5)(1) = 0.5
FIR filters III
Plotting all the signals, we have:
Input signal x[n] Impulse response h[n]
5 1

3
0.5

h[n]
x[n]

0
0

−1

−2 −0.5
−1 0 1 2 3 4 5 −0.5 0 0.5 1 1.5 2 2.5 3
n n
Output signal y[n]
4

3.5

2.5

2
y[n]

1.5

0.5

−0.5

−1
−1 0 1 2 3 4 5 6
n
FIR filters design I

The design of an FIR filter involves finding the values of h[n]


according to certain specifications: filter type, number of
coefficients N or order, cutoff frequency(ies) (ωc ), and sampling
frequency ωs . The most well-known methods are as follows:
I Windows.
I Frequency Sampling.
I Optimal Ripple.
I Least Squares Optimization.
Practical example - FIR filter I
We have the following signal sampled at fs = 1 KHz.

x(t) = sin(2πt) + 0.1 sin(2π60t)


Practical example - FIR filter II

We design a low-pass FIR filter (LPF) with cutoff frequency 20 Hz


and N = 100 to eliminate the 60 Hz component, and obtain the
impulse response h(t). The frequency response of the filter is:
Practical example - FIR filter III
Now we perform the convolution between x(t) and h(t) to obtain
the output signal y (t), whose graph is as follows:
Python code - FIR I
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import firwin, lfilter, freqz
fs=1000 # Sampling frequency
f1= 1 # Signal frequency
f2 = 60 # Noise frequency
duration=2 # seconds

# Generate a time vector:


t = np.linspace(0, duration, int(fs * duration))
# Sine signal with 60 Hz additive noise:
x=np.sin(2 * np.pi * f1 * t )+0.1*np.sin(2 * np.pi * f2 * t )

# Filter parameters:
order = 100 # Filter order
cf= 20 # cutoff frequency in Hz
cfn=cf/(fs/2) # Normalized cutoff frequency
window = ’hamming’ # Window
# Filter design using firwin:
coefs = firwin(order, cutoff=cfn, window=window, pass_zero=True)
Python code - FIR II
# Signal filtering:
f_x = lfilter(coefs, 1.0, x)
# Plot the noisy and filtered signal:
plt.figure(),
plt.plot(t, x)
plt.grid(True), plt.title("Noisy Signal")
plt.xlabel("Time (s)"), plt.ylabel("Amplitude")
plt.figure()
plt.plot(t, f_x)
plt.grid(True), plt.title("Filtered Signal")
plt.xlabel("Time (s)"), plt.ylabel("Amplitude")

# Compute the frequency response using freqz


w, h = freqz(coefs, worN=8000)
# Plot the frequency response:
plt.figure()
plt.plot((0.5 * w / np.pi)*fs/2, np.abs(h), ’b’)
plt.grid(True), plt.xlabel(’Frequency (Hz)’)
plt.ylabel(’Magnitude’),
plt.title(’Frequency response of the FIR filter’)
Content

Introduction

FIR filters

IIR filters

FIR vs IIR
IIR filters

These are filters whose impulse response has an infinite number of


coefficients. They are characterized by recursion, meaning that the
output signal is fed back into the input (feedback). This method
allows the implementation of filters with fewer data points (lower
order). The typical equation of an IIR filter is expressed as:

M
X N
X
y [n] = bk x(n − k) − ak y (n − k)
k=0 k=1
= b0 x[n] + b1 x[n − 1] + · · · + bN x[n − N]
−a1 y [n − 1] − a2 y [n − 2] − · · · − aM y [n − M]

This equation expresses that the output is a function of M + 1


input samples (current and past) and N previous output samples.
IIR filters design

Similar to the FIR case, the design of an IIR filter involves finding
the coefficients b and a according to specifications such as filter
type, orders N and M, cutoff frequency(ies) (ωn ), and sampling
frequency ωs . The methods employed are as follows:
I Analog Prototype Filters: Butterworth, Chebyshev.
I Derivatives Approximation.
I Impulse Invariance.
I Bilinear Transformation.
I Frequency Transformations: analog and digital Domain.
Practical example - IIR filter I
We have the following signal sampled at fs = 1 KHz.

x(t) = sin(2πt) + 0.1 sin(2π50t)


Practical example - IIR filter II

We design a low-pass IIR filter with a cutoff frequency of 20 Hz


and N = 5 to eliminate the 60 Hz component, and we obtain the
coefficients b and a . The frequency response of the filter is:
Practical example - IIR filter III
Now we filter x(t) with the coefficients b and a to obtain the
output signal y (t), and its graph is as follows:
Python code - IIR I
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter, freqz
fs=1000 # Sampling frequency
f1= 1 # Signal frequency
f2 = 60 # Noise frequency
duration=2 # seconds

# Generate a time vector:


t = np.linspace(0, duration, int(fs * duration))
# Sine signal with 60 Hz additive noise:
x=np.sin(2 * np.pi * f1 * t )+0.1*np.sin(2 * np.pi * f2 * t )

# Filter parameters:
order = 5 # Filter order
cf= 20 # cutoff frequency in Hz
cfn=cf/(fs/2) # Normalized cutoff frequency
# Design the IIR filter using butter:
b, a = butter(order, cfn, btype=’low’, analog=False, output=’ba’)
Python code - IIR II
# Signal filtering:
f_x = lfilter(b, a, x)
# Plot the noisy and filtered signal:
plt.figure(),
plt.plot(t, x)
plt.grid(True), plt.title("Noisy Signal")
plt.xlabel("Time (s)"), plt.ylabel("Amplitude")
plt.figure()
plt.plot(t, f_x)
plt.grid(True), plt.title("Filtered Signal")
plt.xlabel("Time (s)"), plt.ylabel("Amplitude")

# Compute the frequency response using freqz


w, h = freqz(b,a,worN=8000)
# Plot the frequency response:
plt.figure()
plt.plot((0.5 * w / np.pi)*fs/2, np.abs(h), ’b’)
plt.grid(True), plt.xlabel(’Frequency (Hz)’)
plt.ylabel(’Magnitude’),
plt.title(’Frequency response of the IIR filter’)
Content

Introduction

FIR filters

IIR filters

FIR vs IIR
FIR vs IIR

FIR filters generally provide a more linear phase response and do


not exhibit oscillations (they do not become unstable). On the
other hand, they require a large number of terms in the impulse
response, making them computationally more expensive.

Regarding IIR filters, they are effective with few coefficients, but
due to their feedback nature, they have a tendency to become
unstable.

You might also like