N X N X N y N y N Y: Digital Filter Design Using MATLAB
N X N X N y N y N Y: Digital Filter Design Using MATLAB
x ( n) y ( n)
x(n − 1) y(n − 1)
[b0 b1 b2 b …] * x (n − 2) – [1 a a a …]* y (n − 2) =0
3 1 2 3 or B*X-A*Y=0
x(n − 3) y(n − 3)
.. . . . . . .
(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=[b0 b1 b2 b3 …]=[0.25 0.25 0.25 0.25]
A=[1 a1 a2 a3 …]=[1]
Note 1; For executing the commends follow, Signal Processing Toolbox is needed.
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 eq. 1. X is input signal to the filter
and y is output of the filter.
Example 2:simulate the filter in ex.1 with a input signal as follow:
time=[0:0.02:20];%Ts=0.02>>Fs=1/0.02=50 Hz
signal=cos(2*pi*0.1*time)+0.25*cos(2*pi*0.5*time)+ .1*cos(2*pi*2*time);
%0.1,0.5 and 2 Hz components
noise=randn(1,1001);
x=noise+signal;
signal without noise x=signal with noise
1.5 2
1.5
1
0.5
0.5
0 0
-0.5
-0.5
-1
-1
-1.5
-1.5 -2
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-1.5 -1.5
0 200 400 600 800 1000 1200 0 200 400 600 800 1000 1200
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 eq.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.
Ex.3; test the both filters in ex.2 by means of freqz. Suppose sampling
frequency is 50Hz. frequency responce of 4 length moving avrage filter
A= [1];
1
0.9
B=0.25*[1 1 1 1]; 0.8
[h1,w1]=freqz(B,A,10); 0.7
figure(1) 0.6
plot(w1,abs(h1),'r+:'); 0.5
0.4
A= [1]; 0.3
0.2
0.1
0
0 0.5 1 1.5 2 2.5 3
B=0.025*ones(40,1);
[h2,w2]=freqz(B,A);
[h3,w3]=freqz(B,A,1000,50);
frequency responce of 40 length moving avrage filter
1
0.9
frequency responce of 40 length moving avrage filter
0.8 1
0.7 0.9
0.6 0.8
0.5 0.7
0.4 0.6
0.3
0.5
0.2
0.4
0.1
0.3
0
0 0.5 1 1.5 2 2.5 3 3.5 0.2
0.1
0
0 5 10 15 20 25
FIR Filter
Parks-MacClellan optimal Fir filter design:
Using remez command: B=remez(n,f,a)
Where; n is the order of filter(number of coefficients is n+1), f and a
are frequency-amplitude characteristics.
Ex.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.
Note that the frequency vector is between 0 and 1 which correspond with
0 and Fs/2 Hz respectively. 1
n=20; 0.9
0.6
amplitude
B=remez(n,f,a) 0.5
0.4
A=[1]; % note that for FIR filter always A=1 0.3
[h1,w1]=freqz(B,A,100,50); 0.2
plot(w1,abs(h1),'r+:'); 0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
frequency responce of 20 order FIR filter frequency
1.4
1.2
B=
Columns 1 through 7
0.8
0.4
Columns 80.2through 14
0.1148 0.1344 0.1471 0.1515 0.1471 0.1344 0.1148
0.2 Columns 0.15
15 through 21
0
0.0908 0.0649
0.1
0.0399 0.0182 0.0019 -0.0078 -0.1085
0 5 10 15 20 25
0.05
0
For testing just excute y=filter(B,A,x); FIR filter b coefficients
-0.05
-0.1
-0.15
0 5 10 15 20 25
y=Output of 20 order FIR filter
2
1.5
0.5
-0.5
-1
-1.5
-2
0 200 400 600 800 1000 1200
1 1.2
1
0.5
0.8
0
0.6
-0.5
0.4
-1 0.2
0
-1.5 0 5 10 15 20 25
0 200 400 600 800 1000 1200
Ex.6; design a band pass filter to just pass 0.5 and 2 Hz component.
We choose n=200 and f=(1/25)*[0 0.4 0.45 2.1 2.2 25] then a=[0 0 1 1 0
0].
n=200; 1
B=remez(n,f,a); 0.7
A=[1]; 0.6
amplitude
[h1,w1]=freqz(B,A,100,50); 0.5
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
frequency
frequency responce of 200 order FIR filter
1.4
1.2
0.8
0.6
0.4
0.2
There is a problem here?!
N=201 or 199 will solve the Problem.
0
0 5 10 15 20 25
1.2
y=Output of 201 order band pass FIR filter
0.8
1
0.6
0.8
0.4
0.2 0.6
0
0.4
-0.2
0.2
-0.4
0
-0.6 0 5 10 15 20 25
0 200 400 600 800 1000 1200
dev=[0.05 0.1];Fs=50; n=
[n,f0,a0]=remezord(f,a,dev,Fs) 45 1.2
B=remez(n,f0,a0); A=[1]; f0 = 1
[h1,w1]=freqz(B,A,100,Fs); 0
0.1200
0.8
FIR filter b coefficients
0.14
0.12
0.1600 0.6
0.1
1.0000
0.4
0.08
0.2
a0 =
0.06
0.04
0.02
1 0
0 5 10 15 20 25
0
1
-0.02 0
-0.04
0 5 10 15 20 25 30 35 40 45 50
0
N = 11
ha2=[ha,ha]; >>>>>>>>>>> indStart = 7
N=max(size(ha)); indEnd = 17
indStart=(N+3)/2; B = Columns 1 through 11
0.0694 -0.0540 -0.1094 0.0474
indEnd=indStart+N-1; 0.3194 0.4545 0.3194 0.0474
B=ha2([indStart:indEnd]); -0.1094 -0.0540 0.0694
Hw=[1 1 1 0 0 0 0 0 0 1 1];
figure(5);stem(Hw);
ha=ifft(Hw);
figure(1);plot(ha);
ha2=[ha,ha]; function [B] = FIRDirect(Hw) >> B=FIRDirect([1 1 1 0 0 0 0 0 0 1 1]);
figure(2);plot(ha2); ha=ifft(Hw); >> plot(B)
N=max(size(ha)); ha2=[ha,ha]; >> B=FIRDirect([0 0 0 1 1 1 1 1 1 0 0]);
indStart=(N+3)/2; N=max(size(ha)); >> plot(B)
indEnd=indStart+N-1; indStart=(N+3)/2;
B=ha2([indStart:indEnd]); indEnd=indStart+N-1;
figure(3);plot(B); B=ha2([indStart:indEnd]);
IIR Filter
There are butter,cheby1,cheby2,ellip,maxflat, and yulewalk commands for
IIR filter design. Formats for them almost the same as follow:
[B,A]=command(n,r,w)
where, B and A are filter coefficients and n is the order of filter as
FIR filter. R is ripples (if applicable) and w is cutoff frequency (or
frequencies). For example Butterworth filter format is as follow:
[B,A]=butter(n,w)
Where B,A,n,w are described in general format. This format just
generates the low pass filter with a cutoff frequency w.
Ex.8. Design a Butterworth digital filter with the same characteristics
of example 4. As in the ex.4 n=20 and w=3/25(note that Fs/2=25)
Fs=50; frequency responce of 20 order butterworth filter
w=3/25; 1.4
n=20;
[B,A]=butter(n,w); 1.2
[h3,w3]=freqz(B,A,512,Fs);
1
2 1 0.8
H ( w) = 2N
w 0.6
1+
w
c 0.4
0.2
If we want to design a high
Pass filter we need to use 0
The following format: 0 5 10 15 20 25
[B,A]=butter(n,w,’ftype’)
frequency responce of 20 order butterworth filter
1.4
Ex.9. design high pass filter
With the same values in ex.8. 1.2
0.6
0.4
frequency responce of 20 order butterworth band pass filter
2.5
0.2
2 0
0 5 10 15 20 25
1.5
1
Ex.10, Design a band pass filter
With cutoff frequencies at 3
0.5
and 10 as in ex.9.
From the example w=(1/25)*{3 10]
0
0 5 10 15 20 25
The rest is the same as ex.9.
frequency responce of 2*20 order butterworth band stop filter
1.4 For a stop band , ‘stop’ will replace ftype
1.2 and order of the filter will become 2*n
Ex.11.change the filter in example 10 to
1
band stop filter.
0.8 The only change is:
[B,A]=butter(n,w,'stop');
0.6
The rest is the same as ex.10.
0.4
0 1.6
0 5 10 15 20 25
1.4
1.2
[B,A]=ellip(n,Rp,Rs,w,'ftype'); 0
0 5 10 15 20 25
suggested values for Rp and Rs are
0.5 and 20. Then by try and error
will come up with a better result.
3.5
3
Ex.13.Repeat example 11 using cheby1 filter.
The only change is:
2.5
[B,A]=cheby1(n,0.5,w,'stop'); 2
1.2
n=10;
R=0.01; 0.4
1.2
n=7; 0.6
[B,A]=ellip(n,0.5,20,w,'stop');
0.4
1.4
frequency responce of 2*12 order butterworth band stop filter
Ex.16: Repeat example 11 , and try to get a
1.2
better response. The only change is:
n=12;
1 [B,A]=butter(n,w,'stop');
Plot A and B coefficients.
0.8
B = 1.0e+004 * Columns 1 through 25
0.0000 -0.0001 0.0005 -0.0028 0.0123 -0.0418 0.1149
0.6
.2625 0.5068 -0.8366 1.1906 -1.4684 1.5742 -1.4684
1.1906 -0.8366 0.5068 -0.2625 0.1149 -0.0418 0.0123
0.4
-0.0028 0.0005 -0.0001 0.0000
A = 1.0e+004 * Columns 1 through 25
0.2
0.0001 -0.0013 0.0084 -0.0355 0.1104 -0.2688 0.5330
-0.8824 1.2405 -1.4991 1.5699 -1.4324 1.1420 -0.7964
0
0 5 10 15 20 25 0.4852 -0.2574 0.1183 -0.0467 0.0156 -0.0044 0.0010
-0.0002 0.0000 -0.0000 0.0000
1.5
1.5
1
1
0.5
0.5
0
0
-0.5 -0.5
-1 -1
-1.5 -1.5
0 5 10 15 20 25 0 5 10 15 20 25
Special Functions
FIR1 command uses window method to use n th order low pass FIR filter.
B=fir1(n,w,’ftype’,’windowtype’)
Where, n is the order of filter (number of coefficients is n+1), and w
is cutoff frequency (or frequencies). Default for ftype is low pass
filter. ‘high’ and ‘stop’ are other values that ftyp can have. Defult
for Windowtype is Hamming window other available window are Boxcar,
Hanning, Barlett, Blackman, Kaiser and chebwin.
Ex.17. Repeat the example 16 for 100 order filter with hamming window
using fir1 (defult is Hamming Window) command.
The only changes are:
n=100;
[B,A]=fir1(n,w,'stop'); 0.8
FIR filter B coefficients
0.7
frequency responce of 100 order fir1(defult is Hamming Window) band stop filter
1.4
0.6
1.2 0.5
1
0.4
0.3
0.8
0.2
0.6
0.1
0.4 0
0.2 -0.1
-0.2
0 0 20 40 60 80 100 120
0 5 10 15 20 25
Ex.18. Repeat the example 17 for 100 order filter with hamming window
using fir1 (defult is Hamming Window) command.
The only changes are:
[B,A]=fir1(n,w,'stop',Kaiser(n+1,4));
frequency responce of 100 order fir1 using Kaiser window with beta=4 band stop filter
1.4 FIR filter B coefficients
0.8
1.2 0.7
0.6
1
0.5
0.8
0.4
0.3
0.6
0.2
0.4
0.1
0.2 0
-0.1
0
0 5 10 15 20 25 -0.2
0 20 40 60 80 100 120
[z,p,kc]=ellipap(n,Rpass,Rstop)
Rpass,Rstop are in decibels
Butterworth analog low pass filter prototype.
[z,p,k]=buttap(n)
» [z,p,kc]=ellipap(2,1,2)
n is order of the filter z=
» [z,p,k]=buttap(2) 0 - 1.1188i
z = Zeroes 0 + 1.1188i
[] p=
p = Poles
-0.0962 - 1.0517i
-0.7071 + 0.7071i -0.0962 + 1.0517i
-0.7071 - 0.7071i kc =
k = Gain 0.7942
1
Chebyshev type II filter order selection.
[n,w]=cheb2ord(Wpass,Wstop,Rpass,Rstop) » [n,w]=cheb1ord(.3, .4 ,10, 60)
Rpass,Rstop are in decibels n=
8
» [n,w]=cheb2ord(.3, .4 ,1, 6)
n = w=
3 0.3000
w = » [n,w]=buttord(.3, .4 ,10, 60)
0.3507 n=
» [n,w]=cheb2ord(.3, .4 ,10, 60)
n =
17
8 w=
w = 0.2869
0.3833
» [A,B,C,D]=zp2ss(z,p,k) ∗
A = x =Ax+Bu
-0.1923 -1.0561 Y=Cx+Du
1.0561 0
B =
1 1
0
0.9
C =
-0.1923 0.1290 0.8
0.7
D = 0.6
1 0.5
0.4
w=kaiser(n,beta)
0.3
n is order of the window
» w=kaiser(100,4); 0.2
» plot(w) 0.1
0
0 10 20 30 40 50 60 70 80 90 100
dct(X)
Discrete cosine transform
» x=[1 1 0 0];
» Dx=dct(x)
Dx = Note that; project must be written in a
1.0000 0.9239 0 -0.3827 reasonable format. With at least
» x2=idct(Dx) introduction and stating the problem,
x2 = methodology and theory, results with
1.0000 1.0000 0 -0.0000
figures and tables, conclusion,
appendix (all MATLAB codes+…)