You are on page 1of 28

Filter Design Using MATLAB

IIR Filter Design


• MATLAB toolbox provides functions to create all types of classical IIR
filters in both the analog and digital domains and in lowpass,
highpass, bandpass, and bandstop configurations.
• For most filter types, you can also find the lowest filter order that fits
a given filter specification in terms of passband and stopband
attenuation, and transition width(s).
Toolbox Filters Methods and Available Functions
Filter Method Description Filter Functions
Analog Using the poles and zeros of a classical Complete design functions:
Prototyping lowpass prototype filter in the besself, butter, cheby1, cheby2, ellip
continuous (Laplace) domain, obtain a Order estimation functions:
digital filter through frequency buttord, cheb1ord, cheb2ord, ellipord
transformation and filter discretization. Lowpass analog prototype functions:
besselap, buttap, cheb1ap, cheb2ap, ellipap
Frequency transformation functions:
lp2bp, lp2bs, lp2hp, lp2lp
Filter discretization functions:
bilinear, impinvar
Direct Design Design digital filter directly in the yulewalk
discrete time-domain by approximating a
piecewise linear magnitude response.
Generalized Design lowpass Butterworth filters with maxflat
Butterworth more zeros than poles.
Design
Low pass Prototype Filter Design
• Design a low pass Butterworth filter to meet the following
specifications:
• Passband gain = 0.5dB
• Passband frequency edge = 1000Hz
• Stopband attenuation = 30dB
• Stopband frequency edge = 1500 Hz
• Sampling frequency = 5000 Hz
• Pass band frequency Wp=2 x 100/5000 = 0.4 rad/sec
• Stopband frequency Ws=2 x1500/5000 = 0.6 rad/sec
• Passband attenuation = - 0.5 dB
• Stopband attenuation = - 30 db
• Cutoff frequency = 0.4644
•N=8
Clear all
Wp= 0.4;
Ws = 0.6;
Ap = 0.5;
As = 30;
[N,wn]= buttord(Wp,Ws,Ap, As)
[z,p,k]=buttap(N)
[b,a]= butter(N,wn)
[z,p,k]=butter(N,wn)
[sos]=zp2sos(z,p,k)
buttord
• [N, Wn] = buttord(Wp, Ws, Ap, As)
• Returns the order N of the lowest order digital Butterworth filter.
• Wp and Ws are the passband and stopband edge frequencies,
normalized from 0 to 1 (where 1 corresponds to pi radians/sample).
For example,
• Lowpass: Wp = 0.1, Ws = 0.2
• Highpass: Wp = 0.2, Ws = 0.1
• Bandpass: Wp = [0.2 0.7], Ws = [0.1 0.8]
• Bandstop: Wp = [0.1 0.8], Ws = [0.2 0.7]
• buttord also returns Wn, the Butterworth natural frequency (or, the
"3 dB frequency").
[Z,P,K] = buttap(N)
• [Z,P,K] = buttap(N) returns the zeros, poles, and gain for an N-th order
normalized prototype Butterworth analog lowpass filter.
• The resulting filter has N poles around the unit circle in the left half
plane, and no zeros.

• [z,p,k]=buttap(9); % Butterworth filter prototype


• [num,den]=zp2tf(z,p,k); % Convert to transfer function form
• freqs(num,den) % Frequency response of analog filter
butter
• [B,A] = butter(N,Wn) designs an Nth order lowpass digital Butterworth
filter.
• Returns the filter coefficients in length N+1 vectors B (numerator) and A
(denominator).
• The coefficients are listed in descending powers of z.
• The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding to
half the sample rate.
• If Wn is a two-element vector, Wn = [W1 W2], butter returns an order 2N
bandpass filter with passband W1 < W < W2.
• [B,A] = butter(N,Wn,'high') designs a highpass filter.
• [B,A] = butter(N,Wn,'low') designs a lowpass filter.
• [B,A] = butter(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2].


• [Z,P,K] = butter(...), the zeros and poles are returned in length N
column vectors Z and P, and the gain in scalar K.
• [A,B,C,D] = butter(...), state-space matrices are returned.
• butter(N,Wn,'high','s') and butter(N,Wn,'stop','s') design analog
Butterworth filters.
Classic IIR Filter Design
The classic IIR filter design technique includes the following steps.
• Find an analog lowpass filter with cutoff frequency of 1 and translate
this prototype filter to the specified band configuration
• Transform the filter to the digital domain.
• Discretize the filter.
• The toolbox provides functions for each of these steps

Design Task Available functions


Analog lowpass prototype buttap, cheb1ap, besselap, ellipap, cheb2ap
Frequency transformation lp2lp, lp2hp, lp2bp, lp2bs
Discretization bilinear, impinvar
Filter Design Functions
• You can easily create a filter of any order with a lowpass, highpass,
bandpass, or bandstop configuration using the filter design functions.

Filter Type Design Function


Butterworth [b,a] = butter(n,Wn,options)
[z,p,k] = butter(n,Wn,options)
[A,B,C,D] = butter(n,Wn,options)
Chebyshev Type I [b,a] = cheby1(n,Rp,Wn,options)
[z,p,k] = cheby1(n,Rp,Wn,options)
[A,B,C,D] = cheby1(n,Rp,Wn,options)
• By default, each of these functions returns a lowpass filter;
• you need to specify only the cutoff frequency that you want, Wn, (in
normalized units such that the Nyquist frequency is 1 Hz).
• For a highpass filter, append 'high' to the function's parameter list.
• For a bandpass or bandstop filter, specify Wn as a two-element vector
containing the passband edge frequencies and append 'stop' for the
bandstop configuration.
• [b,a] = butter(5,0.4); % Lowpass Butterworth
• [b,a] = cheby1(4,1,[0.4 0.7]); % Bandpass Chebyshev Type I
• [b,a] = cheby2(6,60,0.8,'high'); % Highpass Chebyshev Type II
• [b,a] = ellip(3,1,60,[0.4 0.7],'stop'); % Bandstop elliptic
• All filter design functions return a filter in the transfer function, zero-pole-
gain, or state-space linear system model representation, depending on how
many output arguments are present.
• Avoid using the transfer function form because numerical problems caused
by roundoff errors can occur.
• Use the zero-pole-gain form which you can convert to a second-order
section (SOS) form using zp2sos and then use the SOS form to analyze or
implement your filter
Designing IIR Filters to Frequency Domain Specifications

Filter Type Order Estimation Function


Butterworth [n,Wn] = buttord(Wp,Ws,Rp,Rs)
Chebyshev Type I [n,Wn] = cheb1ord(Wp,Ws,Rp,Rs)
Chebyshev Type II [n,Wn] = cheb2ord(Wp,Ws,Rp,Rs)
Elliptic [n,Wn] = ellipord(Wp,Ws,Rp,Rs)
• We want a bandpass filter with a passband from 1000 to 2000 Hz,
stopbands starting 500 Hz away on either side, a 10 kHz sampling
frequency, at most 1 dB of passband ripple, and at least 60 dB of
stopband attenuation.
• You can meet these specifications by using the butter function as
follows:
[n,Wn] = buttord([1000 2000]/5000,[500 2500]/5000,1,60)
[b,a] = butter(n,Wn);
Butterworth lowpass filter
• clear all
• ap = input('Enter the passband attenuation in dB:');
• as = input('Enter the stopband attenuation in dB:');
• fp = input('Enter the passband edge frequency in Hz:');
• fs = input('Enter the stopband edge frequency in Hz:');
• Fs = input('Enter the sampling frquency in Hz:');
• % frequency normalization
• wp = 2*fp/Fs;
• ws = 2*fs/Fs;
• [n,wn]= buttord(wp,ws,ap,as);% minimal order butterworth filter
• [b, a]= butter(n,wn);% coeffecients of desired filter
• [h,w]=freqz(b,a);% frequency response
Butterworth Highpass filter
• clear all
• ap = input('Enter the passband attenuation in dB:');
• as = input('Enter the stopband attenuation in dB:');
• fp = input('Enter the passband edge frequency in Hz:');
• fs = input('Enter the stopband edge frequency in Hz:');
• Fs = input('Enter the sampling frquency in Hz:');
• % frequency normalization
• wp = 2*fp/Fs;
• ws = 2*fs/Fs;
• [n,wn]= buttord(wp,ws,ap,as);% minimal order butterworth filter
• [b, a]= butter(n,wn,'high');% coeffecients of desired filter
• [h,w]=freqz(b,a);% frequency response
Butterworth Bandstop filter
• clear all
• ap = 0.4; as = 50; ws=[0.4 0.6]; wp = [0.3 0.7];
• [n,wn]= buttord(wp,ws,ap,as); [b, a]= butter(n,wn,'stop');
• [h,w]=freqz(b,a);
• % plotting of frequency response
• subplot(2,1,1); plot(w/pi, 20*log10(abs(h)));
• xlabel('Normalized frequency')
• ylabel('gain in dB')
• title('magnitude Response')
• subplot(2,1,2);
• plot(w/pi, angle(h));
• xlabel('Normalized frequency')
• ylabel('Phase anglein in radians')
• title('Phase Response')
Digital Butterworth Lowpass Filter
• clear all
• Ap = input('Enter the Gain at passband edge frequency:');
• As = input('Enter the Gain at stopband edge frequency:');
• pef_d = input('Enter the passband edge digital frequency:');
• sef_d = input('Enter the stopband edge digital frequency:');
• T = input('Enter the sampling tme:');
• % computation of attenuation in dB
• ap = -20*log10(Ap); as = -20*log10(As);
• %frequency warping
• pef_a= pef_d/T; sef_a=sef_d/T;
• [N,cf]= buttord(pef_a,sef_a,ap,as,'s');% order and cutoff frequency
• [bn, an]= butter(N,1,'s');% Normalized transfer function
• disp('Normalized transfer function is')
• Hsn = tf(bn,an)
• [b, a]= butter(N,cf,'s');% Normalized transfer function
… contd
• disp('Unnormalized transfer function is')
• Hs = tf(b,a)
• [n,d]=impinvar(b,a,1/T); % Digital transformation
• disp('Digital transfer function is')
• Hz = tf(n,d,T)
• w = 0:pi/16:pi;
• disp('Frequency response is')
• Hw = freqz(n,d,w);% frequency response
• Hw_mag= abs(Hw)
• % plotting of frequency response
• subplot(2,1,1); plot(w/pi, Hw_mag); xlabel('Normalized frequency') ylabel('gain in dB')
• title('magnitude Response')
• subplot(2,1,2); plot(w/pi, angle(Hw)); xlabel('Normalized frequency')
• ylabel('Phase anglein in radians')
• title('Phase Response')
Design of FIR filters
Window based design
• B = fir1(N,Wn) designs an Nth order lowpass FIR digital filter and returns
the filter coefficients in length N+1 vector B.
• The cut-off frequency Wn must be between 0 < Wn < 1.0, with 1.0
corresponding to half the sample rate.

• B = fir1(N,Wn,'high') designs an Nth order highpass filter.


• B = fir1(N,Wn,'low') to design a lowpass filter.

• If Wn is a two-element vector, Wn = [W1 W2], fir1 returns an order N
bandpass filter with passband W1 < W < W2.
• You can also specify B = fir1(N,Wn,'bandpass').
• If Wn = [W1 W2], B = fir1(N,Wn,'stop') will design a bandstop filter.
• If Wn is a multi-element vector, Wn = [W1 W2 W3 W4 W5 ... WN],
fir1 returns an order N multiband filter with bands
0 < W < W1, W1 < W < W2, ..., WN < W < 1.
• B = fir1(N,Wn,'DC-1') makes the first band a passband.
• B = fir1(N,Wn,'DC-0') makes the first band a stopband.

• B = fir1(N,Wn,WIN) designs an N-th order FIR filter using


• the N+1 length vector WIN to window the impulse response.
• If empty or omitted, fir1 uses a Hamming window of length N+1.
Window functions
• window Window function gateway. window(@WNAME,N) returns an N-point window of type specified by the function handle @WNAME in a column
vector.
• @WNAME can be any valid window function name, for example:
• @bartlett - Bartlett window.
• @barthannwin - Modified Bartlett-Hanning window.
• @blackman - Blackman window.
• @blackmanharris - Minimum 4-term Blackman-Harris window.
• @bohmanwin - Bohman window.
• @chebwin - Chebyshev window.
• @flattopwin - Flat Top window.
• @gausswin - Gaussian window.
• @hamming - Hamming window.
• @hann - Hann window.
• @kaiser - Kaiser window.
• @nuttallwin - Nuttall defined minimum 4-term Blackman-Harris window.
• @parzenwin - Parzen (de la Valle-Poussin) window.
• @rectwin - Rectangular window.
• @taylorwin - Taylor window.
• @tukeywin - Tukey window.
• @triang - Triangular window.
Example
• % Program for designing and implementing the low pass FIR filter using
• % window functions
• clear all
• wc = input('enter the pass band edge frequency in radians:');% example wc
= 0.2*pi
• N = input('Enter the length of the filter:');
• hd = zeros(1,N);
• a = (N-1)/2;
• hna = wc/pi;
• k = 1:1:((N-1)/2);
• n = k - 1-((N-1)/2);
• hd(k) = (sin(wc*n))./(pi*n);% Desired response for low pass filter
• w_rec(k) = 1;
• hn(k) = hd(k);% if the window function is rectangular window, otherwise
• %hd(k) should be multiplied by the window function given.
• hn = [hn hna]
• w =0:pi/16:pi
• Hw1 = hna*exp(-j*w*a);
• Hw2 = 0;
• for m =1:1:a
• Hw3 = hn(m)* ((exp(j*w*(1-m)))+(exp(-j*w*(1-m+2*a))));
• Hw2 = Hw2+Hw3;
• end
• Hw = Hw2 +Hw1
• H_mag = abs(Hw)
• plot(w/pi,H_mag,'k');
• grid;
• title('Magnitude response of LPF FIR filter')
• xlabel('Normalized frequency,\omega/\pi')
• ylabel('Magnitude')
Ideal(desired) impulse response functions for different filters
Filter Ideal(desired) frequency Response Ideal(desired) impulse response
Lowpass sin 𝜔𝑐 (𝑛 − 𝜏)
𝑒 −𝑗𝜏𝜔 ; −𝜔𝑐 ≤ 𝜔 ≤ 𝜔𝑐 ℎ𝑑 𝑛 = 𝑓𝑜𝑟 𝑛 ≠ 𝜏
𝐻𝑑 𝜔 = ൞ 0; −𝜋 ≤ 𝜔 ≤ −𝜔𝑐 𝜋(𝑛 − 𝜏)
0; 𝜔𝑐 ≤ 𝜔 ≤ 𝜋 𝜔𝑐
ℎ𝑑 𝑛 = 𝑓𝑜𝑟 𝑛 = 𝜏
𝜋
Highpass 𝑒 −𝑗𝜏𝜔 ; −𝜋 ≤ 𝜔 ≤ −𝜔𝑐 sin 𝜔𝑐 (𝑛 − 𝜏)
ℎ𝑑 𝑛 = 𝑓𝑜𝑟 𝑛 ≠ 𝜏
𝐻𝑑 𝜔 = ൞ 𝑒 −𝑗𝜏𝜔 ; 𝜔𝑐 ≤ 𝜔 ≤ 𝜋 𝜋(𝑛 − 𝜏)
0; − 𝜔𝑐 ≤ 𝜔 ≤ 𝜔𝑐 𝜔𝑐
ℎ𝑑 𝑛 = 1 − 𝑓𝑜𝑟 𝑛 = 𝜏
𝜋
Bandpass 𝑒 −𝑗𝜏𝜔 ; −𝜔𝑐2 ≤ 𝜔 ≤ 𝜔𝑐1 sin 𝜔𝑐2 (𝑛 − 𝜏) − sin 𝜔𝑐1 (𝑛 − 𝜏)
ℎ𝑑 𝑛 = 𝑓𝑜𝑟 𝑛 ≠ 𝜏
𝑒 −𝑗𝜏𝜔 ; 𝜔𝑐1 ≤ 𝜔 < 𝜔𝑐2 𝜋(𝑛 − 𝜏)
𝐻𝑑 𝜔 = 0; −𝜋 ≤ 𝜔 < −𝜔𝑐2 1
ℎ𝑑 𝑛 = 𝑓𝑜𝑟 𝑛 = 𝜏
0; −𝜔𝑐1 < 𝜔 < 𝜔𝑐1 𝜋
0; 𝜔𝑐2 < 𝜔 ≤ 𝜋
Bandstop 𝑒 −𝑗𝜏𝜔 ; −𝜋 ≤ 𝜔 ≤ −𝜔𝑐2 ℎ𝑑 𝑛
sin 𝜔𝑐2 𝑛 − 𝜏 + sin 𝜋(𝑛 − 𝜏) − sin 𝜔𝑐1 (𝑛 − 𝜏)
𝑒 −𝑗𝜏𝜔 ; −𝜔𝑐1 ≤ 𝜔 < 𝜔𝑐1 = 𝑓𝑜𝑟 𝑛 ≠ 𝜏
𝐻𝑑 𝜔 = 𝑒 −𝑗𝜏𝜔 ; 𝜔𝑐2 < 𝜔 < 𝜋 𝜋(𝑛 − 𝜏)
0; −𝜔𝑐2 ≤ 𝜔 ≤ −𝜔𝑐1 𝜔𝑐2 − 𝜔𝑐1
ℎ𝑑 𝑛 = 1 − 𝑓𝑜𝑟 𝑛 = 𝜏
0; 𝜔𝑐1 < 𝜔 < 𝜔𝑐2 𝜋

You might also like