Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Jimma Institute of Technology

School of Biomedical Engineering


Bio-signal Processing Lab.
Experiment 3

Filter Design and Implementation


Eden T. and Derartu D.

1
Aim of expt.

• To understand how to model a noise in MATLAB

• To understand the importance of modeling noise in filter

designing.

• To design appropriate filter for a given noise.

• To understand how to implement designed filters on a given

data.

2
Modeling Noise

• MATLAB has two functions for generating random


numbers, which can be added to signals to model noise.

1. Uniform random numbers

A = rand(m,n);

– generates an mxn array of random numbers from the


uniform distribution on the interval [0,1].

3
Modeling Noise

2. Gaussian random numbers

A = randn(m,n);

– generates an mxn array of random numbers from the


standard normal distribution with mean 0 and standard
deviation 1.

4
Adding Noise in to a signal

• Once the noise is generated, it can be added to a signal. That is:

noisy signal = signal + noise

– y1 = x + rand(size(x)) % uniform noise

– y2 = x + randn(size(x)) % Gaussian noise

Generate the following signal and create a noise signal.(show the plots).

1. X(n)= cos(0.4πn) +2z(n), 0<n<50 .

Where, z(n) is a Gaussian noise

5
Adding Noise in to a signal
n=0:0.1:50;

x= cos(0.04*pi*n);

subplot(1,2,1)

stem(n,x);

y= cos(0.04*pi*n)+ 0.2*(randn(size(x)));

subplot(1,2,2);

stem(n,y);

6
Filter Design

• A function called filter is available to solve difference


equations numerically, given the input and the difference
equation coefficients.

• In its simplest form this function is invoked by :

y=filter(b, a, x) , where :
b=[b0, b1, ..., bM]; a = [a0, a1, ..., aN];

are the coefficient arrays from the equation and x is the input
sequence
7
Filter Design
• The output y has the same length as input x.

• One must ensure that the coefficient a0 must not be zero.

• To compute and plot impulse response, MATLAB provides


the function “ impz’’.

>> n=0:6;

>> b=[b0,b1……,bn];a=[a0,a1,….an];

>> h=impz(b, a, n);

>> stem(n,h);
8
Example

• Given the following difference equation

y(n)−y(n−1)+0 .9y(n−2) = x(n); ∀n

1. Calculate and plot the impulse response h(n) at n=−20,...,100.

2. Is the system specified by h(n) stable? (hint: sum(abs(h))=

finite number, implies it is a stable system.)

9
Digital Filter

• Filter is a generic name that means a linear time-invariant


system designed for a specific job of frequency selection or
frequency discrimination.

• Hence discrete-time LTI systems are also called digital


filters.

• There are two types of digital filters (FIR and IIR).

10
Filter design and implementation

• Filter design is the process of creating the filter coefficients


to meet specific filtering requirements.

• Filter implementation involves choosing and applying a


particular filter structure to those coefficients.

• Only after both design and implementation have been


performed can data be filtered.

11
Finite duration Impulse Response

1. FIR filter: If the unit impulse response of a LTI system is

of finite duration, then the system is called a finite-duration

impulse response (or FIR) filter.

• In MATLAB FIR filters are represented either as impulse

response values {h(n)} or as difference equation coefficients

{bm} and {a0 =1 }.

12
Finite duration Impulse Response

• To implement FIR filters, we can use either the conv(x,h) function


or the filter(b,1,x) function.

• The difference in the outputs is that,

– the output sequence from the conv(x,h) function has a longer


length than both the x(n) and h(n) sequences.

– On the other hand, the output sequence from the filter(b,1,x)


function has exactly the same length as the input x(n) sequence.

• The use of the filter function is encouraged.

13
FIR filter design

14
FIR filter Examples
1. Design a 18th-order FIR band pass filter with
normalized passband 0.35 0.65.

• Solution

b = fir1(18,[0.35 0.65]);
freqz(b,1,512)

15
Infinite Duration Impulse Response

2. IIR filter: If the impulse response of an LTI system is of


infinite duration, then the system is called an infinite-duration
impulse response (or IIR) filter.

• In MATLAB, IIR filters are described by the difference


equation coefficients {bm} and {ak} and are implemented by
the filter(b,a,x) function.

16
IIR filter design

17
IIR Filter Design
Example 1:

1. For data sampled at 1000 Hz, design a 9th-order high pass


Butterworth IIR filter with cutoff frequency of 300 Hz,

Solution:
Cutoff frequency
order

[b,a] = butter(9,300/500,'high');

freqz(b,a,128,1000); Half of sampling frequency

18
IIR Filter Implementation
2. Apply the filter you designed for input
x=sin(2*pi*50*t)+sin(2*pi*70*t) of sampling
frequency 1000Hz and cutoff frequency of
300Hz.

19
IIR Filter Implementation
t= 0:100;

x=sin(2*pi*50*t)+sin(2*pi*70*t);

subplot(1,2,1);

plot(t,x);

title('orignal_signal');

[b,a] = butter(9,300/500,'high');

y=filter(b,a,x);

% filtering signal x with butterworth highpass %filter

subplot(1,2,2);plot(t,y);

title('filtered signal');
20
Homework
1. Design an 8th order chebyshev type II IIR filter for data sampled

at 1000 Hz, with cutoff frequency of 100 Hz, stopping ripple of

o.5dB, and show the frequency response.

2. What type of noises can be filtered with the filter you designed

and how ? Give an example with code. (brief descriptions and

code is a must).

21
Next class : ECG Signal processing

22

You might also like