Sie sind auf Seite 1von 6

American International University-Bangladesh

Department of Electrical and Electronic Engineering


Digital Signal Processing Lab


Experiment no. 5( Final term) : Digital Filter Design Using MATLAB


Objective:
In this experiment we will learn few digital filter design techniques


Introduction

I we have a digital filter described as:
y(n)=b
0
x(n)+b
1
x(n-1)+b
2
x(n-2)+b
3
x(n-3)+..-a
1
y(n-1)-a
2
y(n-2)+a
3
y(n-3)-..

Rewrite the equation:
b
0
x(n)+b
1
x(n-1)+b
2
x(n-2)+b
3
x(n-3)+- a
0
(=1) y(n)- a
1
y(n-1)-a
2
y(n-2)+a
3
y(n-3)-=0
Rewrite in matrix form:
[b
0
b
1
b
2
b
3
] *

....
) 3 (
) 2 (
) 1 (
) (
n x
n x
n x
n x
[1 a
1
a
2
a
3
]*

....
) 3 (
) 2 (
) 1 (
) (
n y
n y
n y
n y
=0 or B*X-A*Y=0 (eq.1)
Example 1:

A moving average filter with time window length of 4 is defined as:
y(n)=0.25*x(n)+ 0.25*x(n-1)+ 0.25*x(n-2)+ 0.25*x(n-3)
The A and B matrix for this filter are:
B=[b
0
b
1
b
2
b
3
]=[0.25 0.25 0.25 0.25]
A=[1 a
1
a
2
a
3
]=[1]

A real digital filter in market is designed in a way that you can change the A and B
coefficient by means of software. It means that you can implement any kind of filter by
changing A and B. Commend named filter is in MATLAB for propose of simulating a
digital filter.
y=filter(B,A,x)
Where A and B is the coefficients of the filter as in Equation 1. X is input signal to the
filter and y is output of the filter.


Example 2:

In this example we will generate three cosine signals and we will combine them to form a
signal that will be investigated in this experiment.

% This program will show the usages of digital filter
% generate three cosine signals and add them together
time=[0:0.02:20];%Ts=0.02>>Fs=1/0.02=50 Hz
signal1=cos(2*pi*0.1*time)
signal2=0.25*cos(2*pi*0.5*time)
signal3=.1*cos(2*pi*2*time);%0.1,0.5 and 2 Hz components
original_signal=signal1+signal2+signal3
subplot(211)
plot(time,original_signal)
title('original signal')
% generate random noise and addeded with the signal
noise=randn(1,1001);
x=noise+original_signal;
subplot(212)
plot(time,x)
title('signal with noise added')

The following plots will be generated by the MatLab code. One figure shows the original
signal and the other one shows the signal where noise has been added to the original
signal.
0 2 4 6 8 10 12 14 16 18 20
1.5
1
0.5
0
0.5
1
1.5
original signal
0 2 4 6 8 10 12 14 16 18 20
4
2
0
2
4
6
signal with noise added

Now, we should use a 4-point averaging filter defined by
) ].......( 3 [
4
1
] 2 [
4
1
] 1 [
4
1
] [
4
1
) ( A n x n x n x n x n y + + + =
A=[1];
B=0.25*[1 1 1 1];
y=filter(B,A,x);

Now, we should use 40-point averaging filter

A=[1];
B=0.25*[ones(1,40)];
y=filter(B,A,x);
subplot(224)
plot(time,y)
title('Filtered by 40-point averaging filter')


0 2 4 6 8 10 12 14 16 18 20
3
2
1
0
1
2
3
Filtered by 4point averaging filter
0 2 4 6 8 10 12 14 16 18 20
15
10
5
0
5
10
15
Filtered by 40point averaging filter



The figure shows the effects of using higher order filter. We also can test the signal for a
moving average filter of length of 40, by just changing B to B=0.025*ones(40,1);


Digital filters usually consist of A/D and D/A modules that enable it to work in analog
circuits. After implementing coefficients A and B to the filter we usually test the filter by
using a signal generator and oscilloscope. Command freqz in MATLAB do the
oscilloscope and signal generator job together.

[h,w]=freqz(B,A,n,Fs)

Where; A and B is the same in Equation 1 , n is number of points we want to see the
frequency response graph, Fs is sampling frequency. H is magnitude and w is angular
frequency respond to the h. Note that if we drop the n the default is 512 and if drop Fs the
default is pi=3.14.

Example 3:
Find the filter transfer functions for 4-point and 40-point averaging filter.

A= [ 1] ;
B=0. 25*[ 1 1 1 1] ;
[ h1, w1] =f r eqz( B, A, 10) ;
f i gur e( 1)
pl ot ( w1, abs( h1) , ' r +: ' ) ;

A= [ 1] ;
B=0. 025*ones( 40, 1) ;
[ h2, w2] =f r eqz( B, A) ;
[ h3, w3] =f r eqz( B, A, 1000, 50) ;



























FIR Filter Design

Par ks- MacCl el l an opt i mal Fi r f i l t er desi gn:
Usi ng r emez command:
B=remez(n,f,a)
Wher e; n i s t he or der of f i l t er ( number of coef f i ci ent s i s n+1) , f and a
ar e f r equency- ampl i t ude char act er i st i cs.

0 0.5 1 1.5 2 2.5 3
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
frequency responce of 4 length moving avrage filter
0 5 10 15 20 25
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
frequency responce of 40 length moving avrage filter

Example 4:
Design a low pass filter order 20 with a cut off frequency of 3 Hz , transient band of 1
Hz, and sampling frequency of 50Hz. And test it with the same signal in Ex.2.

Not e t hat t he f r equency vect or i s bet ween 0 and 1 whi ch cor r espond wi t h
0 and Fs/ 2 Hz r espect i vel y.
n=20;
f =( 1/ 25) *[ 0 3 4 25] ;
a=[ 1 1 0 0] ;
pl ot ( f , a)
B=r emez( n, f , a)
A=[ 1] ; %not e t hat f or FI R f i l t er al ways A=1
[ h1, w1] =f r eqz( B, A, 100, 50) ;
pl ot ( w1, abs( h1) , ' r +: ' ) ;

0 5 10 15 20 25
0
0.2
0.4
0.6
0.8
1
1.2
1.4
frequency responce of 20 order FIR filter

For t est i ng j ust excut e y=f i l t er ( B, A, x) ; FI R f i l t er b coef f i ci ent s

0 200 400 600 800 1000 1200
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
y=Output of 20 order FIR filter







Example 5 Do t he Exampl e 4 wi t h 200 or der FI R f i l t er .

The onl y change i s j ust n=200 t he r est of Codes ar e t he same.
Not e t hat t he del ay i n f i l t er ed si gnal .


0 5 10 15 20 25
0
0.2
0.4
0.6
0.8
1
1.2
1.4
Frequency response of 200 order filter



0 2 4 6 8 10 12 14 16 18 20
2.5
2
1.5
1
0.5
0
0.5
1
1.5
2
2.5
Filtered output by 200 order filter

Das könnte Ihnen auch gefallen