digital filter design using matlab · digital filter design using matlab dr. tony jacob department...

42
Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati April 11, 2015 Dr. Tony Jacob IIT Guwahati April 11, 2015 1 / 30

Upload: others

Post on 22-Mar-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Digital Filter Design using MATLAB

Dr. Tony Jacob

Department of Electronics and Electrical EngineeringIndian Institute of Technology Guwahati

April 11, 2015

Dr. Tony Jacob IIT Guwahati April 11, 2015 1 / 30

Page 2: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Outline of the Talk

Review of LTI Systems

Filter Specifications

Linear Phase FIR Filters

FIR Filter Design

Hands on MATLAB Session

Dr. Tony Jacob IIT Guwahati April 11, 2015 2 / 30

Page 3: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

What is an LTI System?

Dr. Tony Jacob IIT Guwahati April 11, 2015 3 / 30

Page 4: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Impulse Response Description: Convolution

Dr. Tony Jacob IIT Guwahati April 11, 2015 4 / 30

Page 5: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Transform Domain Description: Fourier Transform

Dr. Tony Jacob IIT Guwahati April 11, 2015 5 / 30

Page 6: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Transform Domain Description: Fourier Transform

Dr. Tony Jacob IIT Guwahati April 11, 2015 6 / 30

Page 7: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Transform Domain Description: z Transform

Dr. Tony Jacob IIT Guwahati April 11, 2015 7 / 30

Page 8: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Difference Equation Description

Dr. Tony Jacob IIT Guwahati April 11, 2015 8 / 30

Page 9: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Conversion between Descriptions

Dr. Tony Jacob IIT Guwahati April 11, 2015 9 / 30

Page 10: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Properties of LTI Systems

Dr. Tony Jacob IIT Guwahati April 11, 2015 10 / 30

Page 11: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

What is a Filter?

An LTI system processes a signal x [n] by amplifying or attenuatingthe sinusoids in its Fourier representation (DTFT) X (e jω) by thecomplex factor H(e jω).

This inspires the terminology that X (e jω) is filtered by H(e jω) toproduce Y (e jω).

Dr. Tony Jacob IIT Guwahati April 11, 2015 11 / 30

Page 12: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Ideal Filters

Categories of Ideal Filters

Ideal Low Pass Filters

Ideal High Pass Filters

Ideal Band Pass Filters

Ideal Band Stop Filters

Dr. Tony Jacob IIT Guwahati April 11, 2015 12 / 30

Page 13: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Low Pass Filter Specifications

Dr. Tony Jacob IIT Guwahati April 11, 2015 13 / 30

Page 14: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Low Pass Filter Specifications

Dr. Tony Jacob IIT Guwahati April 11, 2015 14 / 30

Page 15: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Low Pass Filter Specifications

Dr. Tony Jacob IIT Guwahati April 11, 2015 15 / 30

Page 16: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

FIR Filters: Linear Phase

Dr. Tony Jacob IIT Guwahati April 11, 2015 16 / 30

Page 17: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

FIR Filters: Linear Phase

Dr. Tony Jacob IIT Guwahati April 11, 2015 17 / 30

Page 18: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 18 / 30

Page 19: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 19 / 30

Page 20: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 20 / 30

Page 21: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 21 / 30

Page 22: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 22 / 30

Page 23: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 23 / 30

Page 24: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 24 / 30

Page 25: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 25 / 30

Page 26: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Design Method 1: Windowing

Dr. Tony Jacob IIT Guwahati April 11, 2015 26 / 30

Page 27: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Other Design Methods

Frequency Sampling

Optimal Equiripple Filters

Dr. Tony Jacob IIT Guwahati April 11, 2015 27 / 30

Page 28: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Exercises

Familiarize yourselves with the commands: boxcar, bartlett, hann,hamming, blackman, kaiser.

Design a digital FIR low pass filter with the following specifications:ωp = 0.2π, ωs = 0.3π, Rp = 0.25dB and As = 50dB. Try variouswindow functions for this pupose. Determine the impulse responseand provide a plot of the frequency response of the designed filter.

Familiarize yourselves with designfilt command.

Launch FDATool and FVTool and familiarize yourselves with theirfeatures.

Dr. Tony Jacob IIT Guwahati April 11, 2015 28 / 30

Page 29: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

References

1 Vinay K. Ingle and John G. Proakis, “Digital Signal Processing UsingMATLAB”, 3rd Ed., Cengage Learning, 2012.

2 Alan V. Oppenheim and Ronald W. Schafer, “Discrete-Time SignalProcessing”, 3rd Ed., Pearson Education, 2010.

3 John G. Proakis and Dmitris K. Manolakis, “Digital SignalProcessing: Principles, Algorithms, and Applications”, 4th Ed.,Prentice Hall, 2006.

4 Dmitris K. Manolakis and Vinay K. Ingle, “Applied Digital SignalProcessing”, Cambridge University Press, 2011.

Dr. Tony Jacob IIT Guwahati April 11, 2015 29 / 30

Page 30: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Online Courses

1 Alan V. Oppenheim, 6.007 Signals and Systems, MIT OCW.

2 Alan V. Oppenheim, 6.008 Digital Signal Processing, MIT OCW.

3 Alan V. Oppenheim and Thomas A. Baran, 6.341x Discrete-TimeSignal Processing, edX.

4 Paolo Prandoni and Martin Vetterli, Digital Signal Processing,Coursera.

Dr. Tony Jacob IIT Guwahati April 11, 2015 30 / 30

Page 31: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Questions?

Dr. Tony Jacob IIT Guwahati April 11, 2015 31 / 30

Page 32: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Discrete Fourier transform (DFT).

>> ts = 0.0001 ; >> fs = 1/ts ; >> N = 30001 ; >> T = (N-1)*ts ; >> t = 0:ts:T; >> x = 2*sind(360* 2000* t); >> noise = 1*sind(360* 4000* t); >> SIG = x + noise + 1; >> FFT = fftshift(fft(SIG)); % fft finds DFT using FFT algorithm. % fftshift shifts the DTF to the scale –pi to pi >> f = (-fs/2) : (fs/ (N-1)) : (fs/2); >> figure;plot (f, (abs(FFT)).^2) >> PSD = FFT.*conj(FFT); >> figure;plot (f, PSD) TRY >> psd(SIG)

Page 33: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Discrete Fourier transform (DFT).

>> ts = 0.0001 ; >> fs = 1/ts ; >> N = 30001 ; >> T = (N-1)*ts ; >> t = 0:ts:T; >> x = 2*sind(360* 2000* t); >> noise = 1*sind(360* 4000* t); >> SIG = x + noise + 1; >> FFT = fftshift(fft(SIG)); % fft finds DFT using FFT algorithm. % fftshift shifts the DTF to the scale –pi to pi >> f = (-fs/2) : (fs/ (N-1)) : (fs/2); >> figure;plot (f, (abs(FFT)).^2) >> PSD = FFT.*conj(FFT); >> figure;plot (f, PSD) TRY >> psd(SIG)

Page 34: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• IIR Filters >> [ b, a ] = butter( N , fc , 'low') % N = Order , fc = filter cutoff frequency Note: fc is the normalised frequency in the range 0 – 1. If sampling frequency is

Fs, 1 is mapped to Fs/2. Choose ‘low’ ‘high’ ‘stop’ ‘pass’ accordingly. For ‘stop’ or ‘pass’ , fc = [ fc1 , fc2 ] upper & lower cutoff frequencies. Eg; >> [b , a] = butter (10 , 0.5 ,'low'); % for a lowpass filter >> [b, a] = butter ( 16, [ 0.5 , 0.8] ,‘stop’ ) % for a band stop filter. >> [N, fc] = buttord( fp, fs, Rp, Rs) % For finding optimal filter order. Note: Rp, Rs are the pass & stop band ripples in dB and for LPF: fp = .1, fs = .2 HPF: fp = .2, fs = .1 BPF: fp = [.2 .7], fs = [.1 .8] BSF: fp = [.1 .8], fs = [.2 .7] The above written format must be followed for your design

Page 35: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• IIR Filters Use cheby1, cheby2, ellip functions instead of ‘butter’ to meet your various

design requirements. >> Y = filter ( b, a, X); % to filter the data X ; Y receives the filtered data. a and b are digital filter coeffs generated by any of the

previously discussed filter functions. >> freqz ( b , a ) % plots Z domain frequency response >> [ H,W ] = freqz (b, a, N) % gives response in H and N point freq scale in W. >> freqs (b , a ) % plots S domain frequency response >> [ H,W ] = freqs (b, a, N) % gives response in H and N freq point scale in W. TRY >> fvtool ( b, a )

Page 36: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• FIR Filters • In FIR, it is possible to design filters of linear phase . That is group delay is a

constant. • Types of FIR filters.

Page 37: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• FIR Filters • How to find b0 , b1, b2 ,….. bN using Matlab ? >> b = fir1( N , Wn , ‘low’) % for an FIR lowpass filter Note: use ‘high’ , ‘stop’ , ‘bandpass’ as per requirement. N must be even for fir1( . ) . Matlab will show an error otherwise. Wn is the normalised frequency in range 0 – 1; 1 maps to Fs/2. Wn must be of the form [ W1 W2] for stop and pass filters. >>Y = filter ( b, 1, X); % to filter the data X ; Y receives the filtered data. ‘b’ is the digital filter coeffs generated by fir1 (.)

since there are no poles, ‘a’ is taken as 1.

Page 38: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• FIR Filters Choosing Windows in the design of FIR filters >> b = fir1( N , Wn , ‘low’, window) % specify the required window . Matlab uses hamming as the default window Note: Window along with its parameters need to be specified. Matlab provides the following windows bartlett - Bartlett window. barthannwin- Modified Bartlett-Hanning window. blackman- Blackman window. blackmanharris- Min 4-term Blackman-Harris bohmanwin- Bohman window. chebwin - Chebyshev window. flattopwin - Flat Top window. gausswin- Gaussian window. hamming - Hamming window. hann - Hann window. kaiser - Kaiser window. triang- Triangular window. parzenwin - Parzen window. rectwin - Rectangular window. taylorwin- Taylor window. tukeywin - Tukey window. nuttallwin- Nuttall defined min 4-term Blackman-Harris

Page 39: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• FIR Filters Choosing Windows in the design of FIR filters >> b = fir1( N , Wn , ‘low’, kaiser ( N+1, 4) ) % here beta of kaiser = 4. How to specify a window of your design ? How to caliberate one ? >> wintool % allows you to design your own windows. % also you can tune the available matlab windows. Note: The length of the window must be chosen carefully to match the length of

the filter ; other wise Matlab will give an error. >> b = fir1( N , Wn , ‘low’, yourWindow) % ‘yourWindow’ must be available

in the Workspace.

Page 40: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• FIR Filters Design FIR filters with desired frequency response >> b = fir2( N , F, A ) % F is the frequency sample points. % A is the corresponding amplitude at those freqs Note: vector in F must be normalised; first and last samples of the vector must be

0 and 1 respectively. For example : >> F = [0 : 1: 21] ; >> F = F ./ 21; % this is done for normalizing the vector F >> A = [0 : 1 : 10 ] ; >> A = [A, fliplr ( A ) ] ; >> b = fir2 (100 , F, A) ; >> fvtool ( b, 1 )

Page 41: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Filters

• Filters design tool for instant designing >> fdatool % allows user to quickly design or analyse a filter Note: Options are available for choosing all types of filters in both IIR and FIR categories. The designed filters can be exported to the workspace in various forms. For easy usage , the design can be ported as filter objects.

Page 42: Digital Filter Design using MATLAB · Digital Filter Design using MATLAB Dr. Tony Jacob Department of Electronics and Electrical Engineering Indian Institute of Technology Guwahati

Let’s filter some real time data using the filters designed in Matlab.